-
Notifications
You must be signed in to change notification settings - Fork 2.9k
storage: add list GCS objects in Java S3 SDK #1351
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e3eb08f
Add list GCS objects in Java with workaround
5ed30b8
Add a comment on "encoding-type"
d8e295d
Fix lint issues
35a191f
Remove workaround from sample
adb7023
Merge branch 'master' into s3-sdk-list-objects
frankyn a93bfb6
added new sample format test expectation and lint
87a0b1e
Merge branch 'master' into s3-sdk-list-objects
frankyn 74286f1
Update commented variables
1b55338
Merge branch 's3-sdk-list-objects' of github.com:GoogleCloudPlatform/…
8944531
Merge branch 'master' into s3-sdk-list-objects
frankyn 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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_objects] | ||
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.ObjectListing; | ||
import com.amazonaws.services.s3.model.S3ObjectSummary; | ||
|
||
import java.util.List; | ||
|
||
public class ListGcsObjects { | ||
public static void listGcsObjects(String googleAccessKeyId, | ||
String googleAccessKeySecret, String bucketName) { | ||
kurtisvg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// String googleAccessKeyId = "your-google-access-key-id"; | ||
// String googleAccessKeySecret = "your-google-access-key-secret"; | ||
// String bucketName = "bucket-name"; | ||
|
||
// 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 = AmazonS3ClientBuilder.standard() | ||
.withEndpointConfiguration( | ||
new AwsClientBuilder.EndpointConfiguration( | ||
"https://storage.googleapis.com", "auto")) | ||
.withCredentials(new AWSStaticCredentialsProvider(googleCreds)) | ||
.build(); | ||
|
||
// Call GCS to list current objects | ||
ObjectListing objects = interopClient.listObjects(bucketName); | ||
|
||
// Print objects names | ||
System.out.println("Objects:"); | ||
for (S3ObjectSummary object : objects.getObjectSummaries()) { | ||
System.out.println(object.getKey()); | ||
} | ||
|
||
// Explicitly clean up client resources. | ||
interopClient.shutdown(); | ||
} | ||
} | ||
// [END storage_s3_sdk_list_objects] |
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,71 @@ | ||
/* | ||
* 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 junit.framework.TestCase.assertNotNull; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import com.amazonaws.services.s3.model.Bucket; | ||
import com.amazonaws.services.s3.model.ObjectListing; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.List; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
|
||
public class ListGcsObjectsTest { | ||
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"); | ||
private ByteArrayOutputStream bout; | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull( | ||
System.getenv(varName), | ||
"Environment variable '%s' is required to perform these tests.".format(varName) | ||
); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_CLOUD_PROJECT_S3_SDK"); | ||
requireEnvVar("STORAGE_HMAC_ACCESS_KEY_ID"); | ||
requireEnvVar("STORAGE_HMAC_ACCESS_SECRET_KEY"); | ||
} | ||
|
||
@Before | ||
public void beforeTest() { | ||
bout = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(bout)); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
System.setOut(null); | ||
bout.reset(); | ||
} | ||
|
||
@Test | ||
public void testListObjects() throws Exception { | ||
ListGcsObjects.listGcsObjects(KEY_ID, SECRET_KEY, BUCKET); | ||
String output = bout.toString(); | ||
assertThat(output, CoreMatchers.containsString("Objects:")); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.