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

Commit 6d742a4

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

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed

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: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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+
public static void main(final String[] args) {
61+
String serviceAccountEmailAddress = args[0];
62+
String serviceAccountKey = null;
63+
try {
64+
serviceAccountKey = Files.toString(new File(args[1]), Charset.defaultCharset());
65+
} catch (IOException e) {
66+
System.err.println("Cannot open service account private key PEM file: " + args[1] + "\n" + e.getMessage());
67+
System.exit(1);
68+
}
69+
String command = args[2];
70+
String projectName = null;
71+
String bucketName = null;
72+
if (command.equals("create")) {
73+
projectName = args[3];
74+
bucketName = args[4];
75+
} else if (command.equals("delete")) {
76+
bucketName = args[3];
77+
if (args.length >= 5) {
78+
System.err.println("Command 'delete' require only one additional parameter (bucketName).");
79+
System.exit(1);
80+
}
81+
} else if (command.equals("list")) {
82+
projectName = args[3];
83+
if (args.length >= 5) {
84+
System.err.println("Command 'list' require only one additional parameters (projectName).");
85+
System.exit(1);
86+
}
87+
} else {
88+
System.err.println("Unknown command: " + command);
89+
System.exit(1);
90+
}
91+
ManageBuckets manageBuckets = new ManageBuckets(serviceAccountEmailAddress, serviceAccountKey);
92+
93+
if (command.equals("create")) {
94+
manageBuckets.createBucket(projectName, bucketName);
95+
} else if (command.equals("delete")) {
96+
manageBuckets.deleteBucket(bucketName);
97+
} else if (command.equals("list")) {
98+
manageBuckets.listBuckets(projectName);
99+
} else {
100+
try {
101+
manageBuckets.close();
102+
} catch (IOException e) {
103+
e.printStackTrace();
104+
}
105+
throw new RuntimeException("Should never happen.");
106+
}
107+
108+
try {
109+
manageBuckets.close();
110+
} catch (IOException e) {
111+
e.printStackTrace();
112+
System.exit(1);
113+
}
114+
}
115+
116+
public ManageBuckets(final String serviceAccountEmailAddress, final String serviceAccountKey) {
117+
cloudStorageApi = ContextBuilder.newBuilder(PROVIDER)
118+
.credentials(serviceAccountEmailAddress, serviceAccountKey)
119+
.buildApi(GoogleCloudStorageApi.class);
120+
/*
121+
// This works fine:
122+
// System.out.println("result: " + cloudStorageApi.getBucketApi().listBucket("googpl-gcp-oss-7").size());
123+
///
124+
cloudStorageApi.getBucketApi().createBucket("googpl-gcp-oss-7", new BucketTemplate().name("marekws"));
125+
System.out.println("result: " + cloudStorageApi.getBucketApi().listBucket("googpl-gcp-oss-7").size());
126+
//
127+
*/
128+
}
129+
130+
/**
131+
* Creates a bucket in a given project.
132+
* @param projectName Name of the project in which the bucket should be created.
133+
* @param bucketName Name of the bucket to create.
134+
*/
135+
public final void createBucket(final String projectName, final String bucketName) {
136+
Bucket bucket = null;
137+
try {
138+
bucket = cloudStorageApi.getBucketApi().createBucket(projectName, new BucketTemplate().name(bucketName));
139+
} catch (RuntimeException e) {
140+
System.err.println("Creating bucket " + bucketName + " failed.\n" + e.getMessage());
141+
System.exit(1);
142+
}
143+
if (bucket != null) {
144+
System.out.print("Bucket " + bucket.getName() + " successfully created in project " + projectName + " .");
145+
} else {
146+
System.err.println("Creating bucket " + bucketName + "failed.");
147+
System.exit(1);
148+
}
149+
}
150+
151+
/**
152+
* Deletes a bucket.
153+
* @param bucketName Name of the bucket to delete.
154+
*/
155+
public final void deleteBucket(final String bucketName) {
156+
try {
157+
cloudStorageApi.getBucketApi().deleteBucket(bucketName);
158+
System.out.print("Bucket " + bucketName + " successfully deleted.");
159+
} catch (RuntimeException e) {
160+
System.err.println("Deleting bucket " + bucketName + " failed.\n" + e.getMessage());
161+
System.exit(1);
162+
}
163+
}
164+
165+
/**
166+
* Lists all buckets in a given project.
167+
* @param projectName Name of the project in which the buckets should be listed.
168+
*/
169+
public final void listBuckets(final String projectName) {
170+
System.out.println("List of buckets for project " + projectName + ":");
171+
for (Bucket bucket : cloudStorageApi.getBucketApi().listBucket(projectName)) {
172+
System.out.println("* " + bucket.getName());
173+
}
174+
}
175+
176+
/**
177+
* Always close your service when you're done with it.
178+
*
179+
* Note that closing quietly like this is not necessary in Java 7.
180+
* You would use try-with-resources in the main method instead.
181+
*/
182+
public final void close() throws IOException {
183+
Closeables.close(cloudStorageApi, true);
184+
}
185+
}

0 commit comments

Comments
 (0)