-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1c0e2d6
Humble beginnings
b409123
Add new s3 sdk sample to module list
17d03aa
Fix lint issues
25462fd
Fix hanging resources and change client var name
b87b81a
Update README.md
a0ae8ca
Rename files to match other languages and update README
f216aa6
Merge branch 'master' into s3-sdk
da1b896
Fix incorrect class and update environment variable
43132f2
Addressing review comments
2dadc30
Remove unnecessary variable
fe1d618
Update var name
0c4fdbd
Merge branch 'master' into s3-sdk
frankyn 6d62133
Remove main and exec instructions from README
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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] | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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