Skip to content
This repository was archived by the owner on Feb 4, 2019. It is now read-only.

Commit 0a30a34

Browse files
committed
Add an example to create/delete/list Google Cloud Storage buckets.
1 parent 46f452d commit 0a30a34

3 files changed

Lines changed: 240 additions & 0 deletions

File tree

google/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,10 @@
5050
<artifactId>google-compute-engine</artifactId>
5151
<version>${jclouds.version}</version>
5252
</dependency>
53+
<dependency>
54+
<groupId>org.apache.jclouds.labs</groupId>
55+
<artifactId>google-cloud-storage</artifactId>
56+
<version>1.8.0</version>
57+
</dependency>
5358
</dependencies>
5459
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.jclouds.examples.google.cloudstorage;
20+
21+
/**
22+
* Constants used by the Google Cloud Storage Examples.
23+
*/
24+
public interface Constants {
25+
String PROVIDER = System.getProperty("provider.cs", "google-cloud-storage");
26+
String ZONE = System.getProperty("zone", "europe-west1-a");
27+
28+
String NAME = "jclouds-example";
29+
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.jclouds.examples.google.cloudstorage;
20+
21+
import com.google.common.io.Closeables;
22+
import com.google.common.io.Files;
23+
24+
import org.jclouds.ContextBuilder;
25+
import org.jclouds.googlecloudstorage.GoogleCloudStorageApi;
26+
import org.jclouds.googlecloudstorage.domain.Bucket;
27+
import org.jclouds.googlecloudstorage.domain.BucketTemplate;
28+
29+
import java.io.Closeable;
30+
import java.io.File;
31+
import java.io.IOException;
32+
import java.nio.charset.Charset;
33+
34+
import static org.jclouds.examples.google.cloudstorage.Constants.PROVIDER;
35+
36+
/**
37+
* This example manages buckets of Google Cloud Storage.
38+
*/
39+
public class ManageBuckets implements Closeable {
40+
private final GoogleCloudStorageApi cloudStorageApi;
41+
42+
/**
43+
* To get a service account and its private key see [TODO: write some
44+
* documentation on the website and put a link to it]
45+
*
46+
* The first argument (args[0]) is your service account email address
47+
* (https://developers.google.com/console/help/new/#serviceaccounts).
48+
* The second argument (args[1]) is a path to your service account private key PEM file without a password. It is
49+
* used for server-to-server interactions (https://developers.google.com/console/help/new/#serviceaccounts).
50+
* The key is not transmitted anywhere.
51+
* The third argument (args[2]) is the command you want to perform: "create", "delete", or "list".
52+
* The fourth argument (args[3]) is the project name
53+
* (see https://developers.google.com/storage/docs/signup#activate).
54+
* This argument is skipped for command "delete".
55+
* The fifth argument (args[4]) is the name of the bucket that you want to create or delete.
56+
* This argument is skipped for command list.
57+
* NOTE: Bucket names must be unique across the entire Google Cloud Storage namespace
58+
* (https://developers.google.com/storage/docs/bucketnaming#requirements).
59+
*
60+
* Examples:
61+
*
62+
* java org.jclouds.examples.google.cloudstorage.ManageBuckets \
63+
64+
* /home/planetnik/Work/Cloud/OSS/certificate/gcp-oss.pem \
65+
* create \
66+
* myprojectname \
67+
* planetnikbucketname
68+
*
69+
* java org.jclouds.examples.google.cloudstorage.ManageBuckets \
70+
71+
* /home/planetnik/Work/Cloud/OSS/certificate/gcp-oss.pem \
72+
* delete \
73+
* planetnikbucketname
74+
*
75+
* java org.jclouds.examples.google.cloudstorage.ManageBuckets \
76+
77+
* /home/planetnik/Work/Cloud/OSS/certificate/gcp-oss.pem \
78+
* list \
79+
* myprojectname
80+
*/
81+
public static void main(final String[] args) {
82+
String serviceAccountEmailAddress = args[0];
83+
String serviceAccountKey = null;
84+
try {
85+
serviceAccountKey = Files.toString(new File(args[1]), Charset.defaultCharset());
86+
} catch (IOException e) {
87+
System.err.println("Cannot open service account private key PEM file: " + args[1] + "\n" + e.getMessage());
88+
System.exit(1);
89+
}
90+
String command = args[2];
91+
String projectName = null;
92+
String bucketName = null;
93+
if (command.equals("create")) {
94+
projectName = args[3];
95+
bucketName = args[4];
96+
} else if (command.equals("delete")) {
97+
bucketName = args[3];
98+
if (args.length >= 5) {
99+
System.err.println("Command 'delete' require only one additional parameter (bucketName).");
100+
System.exit(1);
101+
}
102+
} else if (command.equals("list")) {
103+
projectName = args[3];
104+
if (args.length >= 5) {
105+
System.err.println("Command 'list' require only one additional parameters (projectName).");
106+
System.exit(1);
107+
}
108+
} else {
109+
System.err.println("Unknown command: " + command);
110+
System.exit(1);
111+
}
112+
ManageBuckets manageBuckets = new ManageBuckets(serviceAccountEmailAddress, serviceAccountKey);
113+
114+
if (command.equals("create")) {
115+
manageBuckets.createBucket(projectName, bucketName);
116+
} else if (command.equals("delete")) {
117+
manageBuckets.deleteBucket(bucketName);
118+
} else if (command.equals("list")) {
119+
manageBuckets.listBuckets(projectName);
120+
} else {
121+
try {
122+
manageBuckets.close();
123+
} catch (IOException e) {
124+
e.printStackTrace();
125+
}
126+
throw new RuntimeException("Should never happen.");
127+
}
128+
129+
try {
130+
manageBuckets.close();
131+
} catch (IOException e) {
132+
e.printStackTrace();
133+
System.exit(1);
134+
}
135+
}
136+
137+
public ManageBuckets(final String serviceAccountEmailAddress, final String serviceAccountKey) {
138+
cloudStorageApi = ContextBuilder.newBuilder(PROVIDER)
139+
.credentials(serviceAccountEmailAddress, serviceAccountKey)
140+
.buildApi(GoogleCloudStorageApi.class);
141+
/*
142+
// This works fine:
143+
// System.out.println("result: " + cloudStorageApi.getBucketApi().listBucket("googpl-gcp-oss-7").size());
144+
///
145+
cloudStorageApi.getBucketApi().createBucket("googpl-gcp-oss-7", new BucketTemplate().name("marekws"));
146+
System.out.println("result: " + cloudStorageApi.getBucketApi().listBucket("googpl-gcp-oss-7").size());
147+
//
148+
*/
149+
}
150+
151+
/**
152+
* Creates a bucket in a given project.
153+
* @param projectName Name of the project in which the bucket should be created.
154+
* @param bucketName Name of the bucket to create.
155+
*/
156+
public final void createBucket(final String projectName, final String bucketName) {
157+
Bucket bucket = null;
158+
try {
159+
bucket = cloudStorageApi.getBucketApi().createBucket(projectName, new BucketTemplate().name(bucketName));
160+
} catch (RuntimeException e) {
161+
System.err.println("Creating bucket " + bucketName + " failed.\n" + e.getMessage());
162+
System.exit(1);
163+
}
164+
if (bucket != null) {
165+
System.out.print("Bucket " + bucket.getName() + " successfully created in project " + projectName + " .");
166+
} else {
167+
System.err.println("Creating bucket " + bucketName + "failed.");
168+
System.exit(1);
169+
}
170+
}
171+
172+
/**
173+
* Deletes a bucket.
174+
* @param bucketName Name of the bucket to delete.
175+
*/
176+
public final void deleteBucket(final String bucketName) {
177+
try {
178+
cloudStorageApi.getBucketApi().deleteBucket(bucketName);
179+
System.out.print("Bucket " + bucketName + " successfully deleted.");
180+
} catch (RuntimeException e) {
181+
System.err.println("Deleting bucket " + bucketName + " failed.\n" + e.getMessage());
182+
System.exit(1);
183+
}
184+
}
185+
186+
/**
187+
* Lists all buckets in a given project.
188+
* @param projectName Name of the project in which the buckets should be listed.
189+
*/
190+
public final void listBuckets(final String projectName) {
191+
System.out.println("List of buckets for project " + projectName + ":");
192+
for (Bucket bucket : cloudStorageApi.getBucketApi().listBucket(projectName)) {
193+
System.out.println("* " + bucket.getName());
194+
}
195+
}
196+
197+
/**
198+
* Always close your service when you're done with it.
199+
*
200+
* Note that closing quietly like this is not necessary in Java 7.
201+
* You would use try-with-resources in the main method instead.
202+
*/
203+
public final void close() throws IOException {
204+
Closeables.close(cloudStorageApi, true);
205+
}
206+
}

0 commit comments

Comments
 (0)