forked from GoogleCloudPlatform/java-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
storage: add list GCS objects in Java S3 SDK (GoogleCloudPlatform#1351)
* Add list GCS objects in Java with workaround * Add a comment on "encoding-type" * Fix lint issues * Remove workaround from sample * added new sample format test expectation and lint * Update commented variables
- Loading branch information
Showing
4 changed files
with
182 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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) { | ||
|
||
// 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 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 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:")); | ||
} | ||
} |