Skip to content

Commit 1c0e2d6

Browse files
author
Frank Natividad
committed
Humble beginnings
1 parent 95970e1 commit 1c0e2d6

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed

storage/s3-sdk/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Google Cloud Storage (GCS) and the Google Java API Client library
2+
3+
Google Cloud Storage Service features a REST-based API that allows developers to store and access arbitrarily-large objects. These sample Java applications demonstrate how to access the Google Cloud Storage JSON API using the Google Java API Client Libraries. For more information, read the [Google Cloud Storage JSON API Overview][1].
4+
5+
## Quickstart
6+
7+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/).
8+
9+
1. Setup the gcloud tool.
10+
11+
```
12+
gcloud init
13+
```
14+
15+
1. Clone this repo.
16+
17+
```
18+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
19+
```
20+
21+
1. Install [Maven](http://maven.apache.org/).
22+
23+
1. Build this project from this directory:
24+
25+
```
26+
mvn package
27+
```
28+
29+
1. Run one of the sample apps by specifying its class name and a bucket name:
30+
31+
```
32+
mvn exec:java -Dexec.mainClass=StorageSample \
33+
-Dexec.args="ABucketName"
34+
```
35+
36+
Note that if it's been a while, you may need to login with gcloud.
37+
38+
```
39+
gcloud auth application-default login
40+
```
41+
42+
## Products
43+
- [Google Cloud Storage][2]
44+
45+
## Language
46+
- [Java][3]
47+
48+
## Dependencies
49+
- [Google APIs Client Library for Java][4]
50+
51+
[1]: https://cloud.google.com/storage/docs/json_api
52+
[2]: https://cloud.google.com/storage
53+
[3]: https://java.com
54+
[4]: http://code.google.com/p/google-api-java-client/
55+

storage/s3-sdk/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!--
2+
Copyright 2016 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<parent>
18+
<artifactId>doc-samples</artifactId>
19+
<groupId>com.google.cloud</groupId>
20+
<version>1.0.0</version>
21+
<relativePath>../..</relativePath>
22+
</parent>
23+
24+
<modelVersion>4.0.0</modelVersion>
25+
<groupId>com.google.apis-samples</groupId>
26+
<artifactId>storage-s3-interop-api</artifactId>
27+
<version>1</version>
28+
29+
<properties>
30+
<maven.compiler.target>1.8</maven.compiler.target>
31+
<maven.compiler.source>1.8</maven.compiler.source>
32+
</properties>
33+
34+
<build>
35+
<finalName>${project.artifactId}-${project.version}</finalName>
36+
</build>
37+
<dependencies>
38+
<dependency>
39+
<groupId>com.amazonaws</groupId>
40+
<artifactId>aws-java-sdk-s3</artifactId>
41+
<version>1.11.445</version>
42+
</dependency>
43+
<!-- Test Dependencies -->
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<scope>test</scope>
48+
<version>4.12</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.truth</groupId>
52+
<artifactId>truth</artifactId>
53+
<version>0.36</version>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
/**
16+
* """This sample shows how to list Google Cloud Storage (GCS) buckets using the AWS S3 SDK with the
17+
* GCS interoperable XML API.
18+
*
19+
* <p>GCS Credentials are passed in using the following environment variables:
20+
*
21+
* <ul>
22+
* <li>one
23+
* <ul>
24+
* <li>AWS_ACCESS_KEY_ID
25+
* <li>AWS_SECRET_ACCESS_KEY
26+
* </ul>
27+
* </ul>
28+
*
29+
* <p>Learn how to get GCS interoperable credentials at
30+
* https://cloud.google.com/storage/docs/migrating#keys.
31+
*/
32+
33+
// [START storage_s3_sdk_list_buckets]
34+
import com.amazonaws.client.builder.AwsClientBuilder;
35+
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
36+
import com.amazonaws.services.s3.AmazonS3;
37+
import com.amazonaws.services.s3.model.AmazonS3Exception;
38+
import com.amazonaws.services.s3.model.Bucket;
39+
import java.util.List;
40+
41+
public class S3SDK {
42+
public static List<Bucket> listGCSBuckets() {
43+
// Change the endpoint_url to use the Google Cloud Storage XML API endpoint.
44+
AmazonS3 s3 =
45+
AmazonS3ClientBuilder.standard()
46+
.withEndpointConfiguration(
47+
new AwsClientBuilder.EndpointConfiguration(
48+
"https://storage.googleapis.com", "auto"))
49+
.build();
50+
51+
// Call GCS to list current buckets
52+
List<Bucket> buckets = s3.listBuckets();
53+
54+
// Print bucket names
55+
System.out.println("Buckets:");
56+
for (Bucket bucket : buckets) {
57+
System.out.println(bucket.getName());
58+
}
59+
return buckets;
60+
}
61+
}
62+
// [END storage_s3_sdk_list_buckets]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
19+
import com.amazonaws.services.s3.model.Bucket;
20+
import org.junit.Test;
21+
22+
import java.util.List;
23+
24+
public class S3SDKTest {
25+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
26+
private static final String BUCKET = PROJECT_ID;
27+
28+
@Test
29+
public void testListBucket() throws Exception {
30+
List<Bucket> listing = S3SDK.listGCSBuckets();
31+
assertThat(listing.toString()).contains(BUCKET);
32+
}
33+
}

0 commit comments

Comments
 (0)