Skip to content

storage: S3 SDK sample to list buckets #1343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .kokoro/tests/diff_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ gradle -v
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-acct.json
export GOOGLE_CLOUD_PROJECT=java-docs-samples-testing
source ${KOKORO_GFILE_DIR}/aws-secrets.sh
source ${KOKORO_GFILE_DIR}/storage-hmac-credentials.sh
source ${KOKORO_GFILE_DIR}/dlp_secrets.txt
# Activate service account
gcloud auth activate-service-account\
Expand Down
1 change: 1 addition & 0 deletions .kokoro/tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ gradle -v
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-acct.json
export GOOGLE_CLOUD_PROJECT=java-docs-samples-testing
source ${KOKORO_GFILE_DIR}/aws-secrets.sh
source ${KOKORO_GFILE_DIR}/storage-hmac-credentials.sh
source ${KOKORO_GFILE_DIR}/dlp_secrets.txt
# Activate service account
gcloud auth activate-service-account\
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@

<module>storage/cloud-client</module>
<module>storage/json-api</module>
<module>storage/s3-sdk</module>
<module>storage/storage-transfer</module>
<module>storage/xml-api/cmdline-sample</module>
<module>storage/xml-api/serviceaccount-appengine-sample</module>
Expand Down
61 changes: 61 additions & 0 deletions storage/s3-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Using Google Cloud Storage (GCS) with the S3 SDK

[Google Cloud Storage][1] features APIs that allows developers to store and access arbitrarily-large
objects. The [GCS XML API][5] provides support for AWS S3 API users that use S3 SDKs.
Learn more about [Migrating to GCS][6].

## Prerequisites

Install [Maven](http://maven.apache.org/).

## Setup

1. Clone this repo.

```
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
```

1. Change into this directory:

```
cd java-docs-samples/storage/s3-sdk
```

1. Build this project from this directory:

```
mvn package
```

1. Get your [Interoperable Storage Access Keys][3] and set the following environment variables:

## Test Sample

1. Set the following environment variable with the default project for Interoperable Storage Access Keys.

* GOOGLE_CLOUD_PROJECT_S3_SDK=[GOOGLE_PROJECT_ID]
* STORAGE_HMAC_ACCESS_KEY_ID=[ACCESS_KEY_ID]
* STORAGE_HMAC_ACCESS_SECRET_KEY=[ACCESS_SECRET_KEY]

1. Run test using the following Maven command:

```
mvn verify
```

## Products
- [Google Cloud Storage][2]

## Language
- [Java][2]

## Dependencies
- [AWS S3 Java SDK][4]

[1]: https://cloud.google.com/storage
[2]: https://java.com
[3]: https://cloud.google.com/storage/docs/migrating#keys
[4]: https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3
[5]: https://cloud.google.com/storage/docs/xml-api/overview
[6]: https://cloud.google.com/storage/docs/migrating
57 changes: 57 additions & 0 deletions storage/s3-sdk/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!--
Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally these 4 lines lead the pom

<groupId>com.google.apis-samples</groupId>
<artifactId>storage-s3-interop-api</artifactId>
<version>1</version>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.10</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.445</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.36</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
61 changes: 61 additions & 0 deletions storage/s3-sdk/src/main/java/ListGcsBuckets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START storage_s3_sdk_list_buckets]
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;

import java.util.List;

public class ListGcsBuckets {
public static List<Bucket> listGcsBuckets(String googleAccessKeyId,
String googleAccessKeySecret) {
// Create a BasicAWSCredentials using Cloud Storage HMAC credentials.
BasicAWSCredentials googleCreds = new BasicAWSCredentials(googleAccessKeyId,
googleAccessKeySecret);

// Create a new client and do the following:
// 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint.
// 2. Use Cloud Storage HMAC Credentials.
AmazonS3 interopClient =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it might have some style violations - I would try to run google-java-format or mvn checkstyle:check to verify.

AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
"https://storage.googleapis.com", "auto"))
.withCredentials(new AWSStaticCredentialsProvider(googleCreds))
.build();

// Call GCS to list current buckets
List<Bucket> buckets = interopClient.listBuckets();

// Print bucket names
System.out.println("Buckets:");
for (Bucket bucket : buckets) {
System.out.println(bucket.getName());
}

// Explicitly clean up client resources.
interopClient.shutdown();

return buckets;
}
// [END storage_s3_sdk_list_buckets]
}

33 changes: 33 additions & 0 deletions storage/s3-sdk/src/test/java/ListGcsBucketsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import static com.google.common.truth.Truth.assertThat;

import com.amazonaws.services.s3.model.Bucket;
import java.util.List;
import org.junit.Test;

public class ListGcsBucketsTest {
private static final String BUCKET = System.getenv("GOOGLE_CLOUD_PROJECT_S3_SDK");
private static final String KEY_ID = System.getenv("STORAGE_HMAC_ACCESS_KEY_ID");
private static final String SECRET_KEY = System.getenv("STORAGE_HMAC_ACCESS_SECRET_KEY");

@Test
public void testListBucket() throws Exception {
List<Bucket> buckets = ListGcsBuckets.listGcsBuckets(KEY_ID, SECRET_KEY);
assertThat(buckets.toString()).contains(BUCKET);
}
}