Skip to content

Commit d42ffac

Browse files
authored
storage: add list GCS objects in Java S3 SDK (#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
1 parent 848fad5 commit d42ffac

File tree

4 files changed

+182
-9
lines changed

4 files changed

+182
-9
lines changed

storage/s3-sdk/src/main/java/ListGcsBuckets.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@
2525
import java.util.List;
2626

2727
public class ListGcsBuckets {
28-
public static List<Bucket> listGcsBuckets(String googleAccessKeyId,
28+
public static void listGcsBuckets(String googleAccessKeyId,
2929
String googleAccessKeySecret) {
30+
31+
// String googleAccessKeyId = "your-google-access-key-id";
32+
// String googleAccessKeySecret = "your-google-access-key-secret";
33+
3034
// Create a BasicAWSCredentials using Cloud Storage HMAC credentials.
31-
BasicAWSCredentials googleCreds = new BasicAWSCredentials(googleAccessKeyId,
35+
BasicAWSCredentials googleCreds = new BasicAWSCredentials(googleAccessKeyId,
3236
googleAccessKeySecret);
3337

3438
// Create a new client and do the following:
3539
// 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint.
3640
// 2. Use Cloud Storage HMAC Credentials.
37-
AmazonS3 interopClient =
38-
AmazonS3ClientBuilder.standard()
41+
AmazonS3 interopClient = AmazonS3ClientBuilder.standard()
3942
.withEndpointConfiguration(
4043
new AwsClientBuilder.EndpointConfiguration(
4144
"https://storage.googleapis.com", "auto"))
@@ -53,8 +56,6 @@ public static List<Bucket> listGcsBuckets(String googleAccessKeyId,
5356

5457
// Explicitly clean up client resources.
5558
interopClient.shutdown();
56-
57-
return buckets;
5859
}
5960
// [END storage_s3_sdk_list_buckets]
6061
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2019 Google LLC
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+
18+
// [START storage_s3_sdk_list_objects]
19+
import com.amazonaws.auth.AWSStaticCredentialsProvider;
20+
import com.amazonaws.auth.BasicAWSCredentials;
21+
import com.amazonaws.client.builder.AwsClientBuilder;
22+
23+
import com.amazonaws.services.s3.AmazonS3;
24+
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
25+
import com.amazonaws.services.s3.model.ObjectListing;
26+
import com.amazonaws.services.s3.model.S3ObjectSummary;
27+
28+
import java.util.List;
29+
30+
public class ListGcsObjects {
31+
public static void listGcsObjects(String googleAccessKeyId,
32+
String googleAccessKeySecret, String bucketName) {
33+
34+
// String googleAccessKeyId = "your-google-access-key-id";
35+
// String googleAccessKeySecret = "your-google-access-key-secret";
36+
// String bucketName = "bucket-name";
37+
38+
// Create a BasicAWSCredentials using Cloud Storage HMAC credentials.
39+
BasicAWSCredentials googleCreds = new BasicAWSCredentials(googleAccessKeyId,
40+
googleAccessKeySecret);
41+
42+
// Create a new client and do the following:
43+
// 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint.
44+
// 2. Use Cloud Storage HMAC Credentials.
45+
AmazonS3 interopClient = AmazonS3ClientBuilder.standard()
46+
.withEndpointConfiguration(
47+
new AwsClientBuilder.EndpointConfiguration(
48+
"https://storage.googleapis.com", "auto"))
49+
.withCredentials(new AWSStaticCredentialsProvider(googleCreds))
50+
.build();
51+
52+
// Call GCS to list current objects
53+
ObjectListing objects = interopClient.listObjects(bucketName);
54+
55+
// Print objects names
56+
System.out.println("Objects:");
57+
for (S3ObjectSummary object : objects.getObjectSummaries()) {
58+
System.out.println(object.getKey());
59+
}
60+
61+
// Explicitly clean up client resources.
62+
interopClient.shutdown();
63+
}
64+
}
65+
// [END storage_s3_sdk_list_objects]

storage/s3-sdk/src/test/java/ListGcsBucketsTest.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,56 @@
1414
* limitations under the License.
1515
*/
1616

17-
import static com.google.common.truth.Truth.assertThat;
17+
import static junit.framework.TestCase.assertNotNull;
18+
import static org.junit.Assert.assertThat;
1819

1920
import com.amazonaws.services.s3.model.Bucket;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
2024
import java.util.List;
25+
import org.hamcrest.CoreMatchers;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.BeforeClass;
2129
import org.junit.Test;
2230

2331
public class ListGcsBucketsTest {
2432
private static final String BUCKET = System.getenv("GOOGLE_CLOUD_PROJECT_S3_SDK");
2533
private static final String KEY_ID = System.getenv("STORAGE_HMAC_ACCESS_KEY_ID");
2634
private static final String SECRET_KEY = System.getenv("STORAGE_HMAC_ACCESS_SECRET_KEY");
35+
private ByteArrayOutputStream bout;
36+
37+
private static void requireEnvVar(String varName) {
38+
assertNotNull(
39+
System.getenv(varName),
40+
"Environment variable '%s' is required to perform these tests.".format(varName)
41+
);
42+
}
43+
44+
@BeforeClass
45+
public static void checkRequirements() {
46+
requireEnvVar("GOOGLE_CLOUD_PROJECT_S3_SDK");
47+
requireEnvVar("STORAGE_HMAC_ACCESS_KEY_ID");
48+
requireEnvVar("STORAGE_HMAC_ACCESS_SECRET_KEY");
49+
}
50+
51+
@Before
52+
public void beforeTest() {
53+
bout = new ByteArrayOutputStream();
54+
System.setOut(new PrintStream(bout));
55+
}
56+
57+
@After
58+
public void tearDown() {
59+
System.setOut(null);
60+
bout.reset();
61+
}
2762

2863
@Test
2964
public void testListBucket() throws Exception {
30-
List<Bucket> buckets = ListGcsBuckets.listGcsBuckets(KEY_ID, SECRET_KEY);
31-
assertThat(buckets.toString()).contains(BUCKET);
65+
ListGcsBuckets.listGcsBuckets(KEY_ID, SECRET_KEY);
66+
String output = bout.toString();
67+
assertThat(output, CoreMatchers.containsString("Buckets:"));
3268
}
3369
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2019 Google LLC
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 junit.framework.TestCase.assertNotNull;
18+
import static org.junit.Assert.assertThat;
19+
20+
import com.amazonaws.services.s3.model.Bucket;
21+
import com.amazonaws.services.s3.model.ObjectListing;
22+
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.List;
26+
import org.hamcrest.CoreMatchers;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
33+
public class ListGcsObjectsTest {
34+
private static final String BUCKET = System.getenv("GOOGLE_CLOUD_PROJECT_S3_SDK");
35+
private static final String KEY_ID = System.getenv("STORAGE_HMAC_ACCESS_KEY_ID");
36+
private static final String SECRET_KEY = System.getenv("STORAGE_HMAC_ACCESS_SECRET_KEY");
37+
private ByteArrayOutputStream bout;
38+
39+
private static void requireEnvVar(String varName) {
40+
assertNotNull(
41+
System.getenv(varName),
42+
"Environment variable '%s' is required to perform these tests.".format(varName)
43+
);
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("GOOGLE_CLOUD_PROJECT_S3_SDK");
49+
requireEnvVar("STORAGE_HMAC_ACCESS_KEY_ID");
50+
requireEnvVar("STORAGE_HMAC_ACCESS_SECRET_KEY");
51+
}
52+
53+
@Before
54+
public void beforeTest() {
55+
bout = new ByteArrayOutputStream();
56+
System.setOut(new PrintStream(bout));
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
System.setOut(null);
62+
bout.reset();
63+
}
64+
65+
@Test
66+
public void testListObjects() throws Exception {
67+
ListGcsObjects.listGcsObjects(KEY_ID, SECRET_KEY, BUCKET);
68+
String output = bout.toString();
69+
assertThat(output, CoreMatchers.containsString("Objects:"));
70+
}
71+
}

0 commit comments

Comments
 (0)