Skip to content

Commit e09e199

Browse files
authored
chore: samples overhaul (#3109)
* Centralize environment variables resolution in new Env utility class * Leverage StorageITRunner and associated fixtures * Condense PubSubNotification test classes into one class that can all share a topic * Replace usages of the word file with blob (GCS doesn't have files, and our library always presents Blob as its name for a gcs object) * Use TemporaryBucket for tests which depend on specific bucket state. 1. Avoids contention between tests trying to change bucket state 2. Avoids the "too many bucket modifications" error from gcs due to bucket mutations 3. Makes it possible, for running tests in parallel in the future * Update samples to apply try-with-resource to TransferManager and Storage instances * Avoid using syntax classes in tests (use storage.x(blob) instead of blob.x()) * Add ITCleanupOldBucketsTest
1 parent a6ab7c5 commit e09e199

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1128
-1059
lines changed

samples/install-without-bom/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,32 @@
6969
<version>1.139.4</version>
7070
<scope>test</scope>
7171
</dependency>
72+
<dependency>
73+
<groupId>com.google.cloud</groupId>
74+
<artifactId>google-cloud-kms</artifactId>
75+
<version>2.66.0</version>
76+
<scope>test</scope>
77+
</dependency>
7278
<dependency>
7379
<groupId>com.google.cloud</groupId>
7480
<artifactId>google-cloud-storage</artifactId>
7581
<version>2.52.3</version>
7682
<classifier>tests</classifier>
7783
<scope>test</scope>
7884
</dependency>
85+
<!-- tests jars don't have transitive dependencies automatically resolved -->
86+
<dependency>
87+
<groupId>org.slf4j</groupId>
88+
<artifactId>jul-to-slf4j</artifactId>
89+
<version>2.0.17</version>
90+
<scope>test</scope>
91+
</dependency>
92+
<dependency>
93+
<groupId>ch.qos.logback</groupId>
94+
<artifactId>logback-classic</artifactId>
95+
<version>1.3.15</version>
96+
<scope>test</scope>
97+
</dependency>
7998
<dependency>
8099
<groupId>com.google.cloud.opentelemetry</groupId>
81100
<artifactId>exporter-trace</artifactId>

samples/snapshot/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,32 @@
6161
<version>1.139.4</version>
6262
<scope>test</scope>
6363
</dependency>
64+
<dependency>
65+
<groupId>com.google.cloud</groupId>
66+
<artifactId>google-cloud-kms</artifactId>
67+
<version>2.66.0</version>
68+
<scope>test</scope>
69+
</dependency>
6470
<dependency>
6571
<groupId>com.google.cloud</groupId>
6672
<artifactId>google-cloud-storage</artifactId>
6773
<version>2.52.4-SNAPSHOT</version><!-- {x-version-update:google-cloud-storage:current} -->
6874
<classifier>tests</classifier>
6975
<scope>test</scope>
7076
</dependency>
77+
<!-- tests jars don't have transitive dependencies automatically resolved -->
78+
<dependency>
79+
<groupId>org.slf4j</groupId>
80+
<artifactId>jul-to-slf4j</artifactId>
81+
<version>2.0.17</version>
82+
<scope>test</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>ch.qos.logback</groupId>
86+
<artifactId>logback-classic</artifactId>
87+
<version>1.3.15</version>
88+
<scope>test</scope>
89+
</dependency>
7190
<dependency>
7291
<groupId>com.google.cloud.opentelemetry</groupId>
7392
<artifactId>exporter-trace</artifactId>

samples/snippets/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@
7979
<version>1.139.4</version>
8080
<scope>test</scope>
8181
</dependency>
82+
<dependency>
83+
<groupId>com.google.cloud</groupId>
84+
<artifactId>google-cloud-kms</artifactId>
85+
<version>2.66.0</version>
86+
<scope>test</scope>
87+
</dependency>
8288
<dependency>
8389
<groupId>com.google.cloud.opentelemetry</groupId>
8490
<artifactId>exporter-trace</artifactId>
@@ -97,6 +103,19 @@
97103
<classifier>tests</classifier>
98104
<scope>test</scope>
99105
</dependency>
106+
<!-- tests jars don't have transitive dependencies automatically resolved -->
107+
<dependency>
108+
<groupId>org.slf4j</groupId>
109+
<artifactId>jul-to-slf4j</artifactId>
110+
<version>2.0.17</version>
111+
<scope>test</scope>
112+
</dependency>
113+
<dependency>
114+
<groupId>ch.qos.logback</groupId>
115+
<artifactId>logback-classic</artifactId>
116+
<version>1.3.15</version>
117+
<scope>test</scope>
118+
</dependency>
100119
<!-- [START storage_install_with_bom] -->
101120
</dependencies>
102121
<!-- [END storage_install_with_bom] -->

samples/snippets/src/main/java/com/example/storage/QuickstartStorageControlSample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static void main(String... args) throws Exception {
3434
.setName(StorageLayoutName.format("_", bucketName))
3535
.build();
3636
StorageLayout response = storageControlClient.getStorageLayout(request);
37-
System.out.printf("Performed getStorageLayout request for %s", response.getName());
37+
System.out.printf("Performed getStorageLayout request for %s%n", response.getName());
3838
}
3939
}
4040
}

samples/snippets/src/main/java/com/example/storage/object/AddFileOwner.java renamed to samples/snippets/src/main/java/com/example/storage/object/AddBlobOwner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import com.google.cloud.storage.Storage;
2727
import com.google.cloud.storage.StorageOptions;
2828

29-
public class AddFileOwner {
29+
public class AddBlobOwner {
3030

31-
public static void addFileOwner(
31+
public static void addBlobOwner(
3232
String projectId, String bucketName, String userEmail, String blobName) {
3333
// The ID of your GCP project
3434
// String projectId = "your-project-id";
@@ -50,7 +50,7 @@ public static void addFileOwner(
5050
System.out.println(
5151
"Added user "
5252
+ userEmail
53-
+ " as an owner on file "
53+
+ " as an owner on blob "
5454
+ blobName
5555
+ " in bucket "
5656
+ bucketName);

samples/snippets/src/main/java/com/example/storage/object/BatchSetObjectMetadata.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@
1919
// [START storage_batch_request]
2020
import com.google.api.gax.paging.Page;
2121
import com.google.cloud.storage.Blob;
22+
import com.google.cloud.storage.BlobInfo;
2223
import com.google.cloud.storage.Storage;
2324
import com.google.cloud.storage.StorageBatch;
25+
import com.google.cloud.storage.StorageBatchResult;
26+
import com.google.cloud.storage.StorageException;
2427
import com.google.cloud.storage.StorageOptions;
2528
import java.util.HashMap;
29+
import java.util.List;
2630
import java.util.Map;
31+
import java.util.Objects;
32+
import java.util.stream.Collectors;
2733

2834
public class BatchSetObjectMetadata {
2935
public static void batchSetObjectMetadata(
30-
String projectId, String bucketName, String directoryPrefix) {
36+
String projectId, String bucketName, String pathPrefix) {
3137
// The ID of your GCP project
3238
// String projectId = "your-project-id";
3339

@@ -36,32 +42,56 @@ public static void batchSetObjectMetadata(
3642

3743
// The directory prefix. All objects in the bucket with this prefix will have their metadata
3844
// updated
39-
// String directoryPrefix = "yourDirectory/";
45+
// String pathPrefix = "yourPath/";
4046

4147
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
4248
Map<String, String> newMetadata = new HashMap<>();
4349
newMetadata.put("keyToAddOrUpdate", "value");
4450
Page<Blob> blobs =
4551
storage.list(
4652
bucketName,
47-
Storage.BlobListOption.prefix(directoryPrefix),
48-
Storage.BlobListOption.currentDirectory());
53+
Storage.BlobListOption.prefix(pathPrefix),
54+
Storage.BlobListOption.delimiter("/"));
4955
StorageBatch batchRequest = storage.batch();
5056

5157
// Add all blobs with the given prefix to the batch request
52-
for (Blob blob : blobs.iterateAll()) {
53-
batchRequest.update(blob.toBuilder().setMetadata(newMetadata).build());
54-
}
58+
List<StorageBatchResult<Blob>> batchResults =
59+
blobs
60+
.streamAll()
61+
.map(blob -> batchRequest.update(blob.toBuilder().setMetadata(newMetadata).build()))
62+
.collect(Collectors.toList());
5563

5664
// Execute the batch request
5765
batchRequest.submit();
66+
List<StorageException> failures =
67+
batchResults.stream()
68+
.map(
69+
r -> {
70+
try {
71+
BlobInfo blob = r.get();
72+
return null;
73+
} catch (StorageException e) {
74+
return e;
75+
}
76+
})
77+
.filter(Objects::nonNull)
78+
.collect(Collectors.toList());
5879

5980
System.out.println(
60-
"All blobs in bucket "
81+
(batchResults.size() - failures.size())
82+
+ " blobs in bucket "
6183
+ bucketName
6284
+ " with prefix '"
63-
+ directoryPrefix
64-
+ "' had their metadata updated.");
85+
+ pathPrefix
86+
+ "' had their metadata updated successfully.");
87+
88+
if (!failures.isEmpty()) {
89+
System.out.println("While processing, there were " + failures.size() + " failures");
90+
91+
for (StorageException failure : failures) {
92+
failure.printStackTrace(System.out);
93+
}
94+
}
6595
}
6696
}
6797
// [END storage_batch_request]

samples/snippets/src/main/java/com/example/storage/object/DownloadByteRange.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static void downloadByteRange(
6666
ByteStreams.copy(from, to);
6767

6868
System.out.printf(
69-
"%s downloaded to %s from byte %d to byte %d",
69+
"%s downloaded to %s from byte %d to byte %d%n",
7070
blobId.toGsUtilUri(), destFileName, startByte, endBytes);
7171
}
7272
}

samples/snippets/src/main/java/com/example/storage/object/DownloadObject.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919
// [START storage_download_file]
2020

21-
import com.google.cloud.storage.Blob;
2221
import com.google.cloud.storage.BlobId;
2322
import com.google.cloud.storage.Storage;
2423
import com.google.cloud.storage.StorageOptions;
2524
import java.nio.file.Paths;
2625

2726
public class DownloadObject {
2827
public static void downloadObject(
29-
String projectId, String bucketName, String objectName, String destFilePath) {
28+
String projectId, String bucketName, String objectName, String destFilePath)
29+
throws Exception {
3030
// The ID of your GCP project
3131
// String projectId = "your-project-id";
3232

@@ -39,18 +39,19 @@ public static void downloadObject(
3939
// The path to which the file should be downloaded
4040
// String destFilePath = "/local/path/to/file.txt";
4141

42-
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
42+
StorageOptions storageOptions = StorageOptions.newBuilder().setProjectId(projectId).build();
43+
try (Storage storage = storageOptions.getService()) {
4344

44-
Blob blob = storage.get(BlobId.of(bucketName, objectName));
45-
blob.downloadTo(Paths.get(destFilePath));
45+
storage.downloadTo(BlobId.of(bucketName, objectName), Paths.get(destFilePath));
4646

47-
System.out.println(
48-
"Downloaded object "
49-
+ objectName
50-
+ " from bucket name "
51-
+ bucketName
52-
+ " to "
53-
+ destFilePath);
47+
System.out.println(
48+
"Downloaded object "
49+
+ objectName
50+
+ " from bucket name "
51+
+ bucketName
52+
+ " to "
53+
+ destFilePath);
54+
}
5455
}
5556
}
5657
// [END storage_download_file]

samples/snippets/src/main/java/com/example/storage/object/DownloadPublicObject.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
// [START storage_download_public_file]
2020

21-
import com.google.cloud.storage.Blob;
2221
import com.google.cloud.storage.BlobId;
2322
import com.google.cloud.storage.Storage;
2423
import com.google.cloud.storage.StorageOptions;
@@ -39,8 +38,7 @@ public static void downloadPublicObject(
3938
// Instantiate an anonymous Google Cloud Storage client, which can only access public files
4039
Storage storage = StorageOptions.getUnauthenticatedInstance().getService();
4140

42-
Blob blob = storage.get(BlobId.of(bucketName, publicObjectName));
43-
blob.downloadTo(destFilePath);
41+
storage.downloadTo(BlobId.of(bucketName, publicObjectName), destFilePath);
4442

4543
System.out.println(
4644
"Downloaded public object "

samples/snippets/src/main/java/com/example/storage/object/PrintFileAcl.java renamed to samples/snippets/src/main/java/com/example/storage/object/PrintBlobAcl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import com.google.cloud.storage.StorageOptions;
2626
import java.util.List;
2727

28-
public class PrintFileAcl {
28+
public class PrintBlobAcl {
2929

30-
public static void printFileAcl(String bucketName, String blobName) {
30+
public static void printBlobAcl(String bucketName, String blobName) {
3131

3232
// The ID to give your GCS bucket
3333
// String bucketName = "your-unique-bucket-name";

samples/snippets/src/main/java/com/example/storage/object/PrintFileAclForUser.java renamed to samples/snippets/src/main/java/com/example/storage/object/PrintBlobAclForUser.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
import com.google.cloud.storage.Storage;
2626
import com.google.cloud.storage.StorageOptions;
2727

28-
public class PrintFileAclForUser {
28+
public class PrintBlobAclForUser {
2929

30-
public static void printFileAclForUser(String bucketName, String blobName, String userEmail) {
30+
public static void printBlobAclForUser(String bucketName, String blobName, String userEmail)
31+
throws Exception {
3132

3233
// The ID to give your GCS bucket
3334
// String bucketName = "your-unique-bucket-name";
@@ -38,14 +39,15 @@ public static void printFileAclForUser(String bucketName, String blobName, Strin
3839
// The email of the user whose acl is being retrieved.
3940
// String userEmail = "[email protected]"
4041

41-
Storage storage = StorageOptions.newBuilder().build().getService();
42-
Blob blob = storage.get(BlobId.of(bucketName, blobName));
43-
Acl blobAcl = blob.getAcl(new User(userEmail));
44-
if (blobAcl != null) {
45-
String userRole = blobAcl.getRole().name();
46-
System.out.println("User " + userEmail + " has role " + userRole);
47-
} else {
48-
System.out.println("User " + userEmail + " not found");
42+
try (Storage storage = StorageOptions.newBuilder().build().getService()) {
43+
Blob blob = storage.get(BlobId.of(bucketName, blobName));
44+
Acl blobAcl = blob.getAcl(new User(userEmail));
45+
if (blobAcl != null) {
46+
String userRole = blobAcl.getRole().name();
47+
System.out.println("User " + userEmail + " has role " + userRole);
48+
} else {
49+
System.out.println("User " + userEmail + " not found");
50+
}
4951
}
5052
}
5153
}

samples/snippets/src/main/java/com/example/storage/object/RemoveFileOwner.java renamed to samples/snippets/src/main/java/com/example/storage/object/RemoveBlobOwner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import com.google.cloud.storage.Storage;
2525
import com.google.cloud.storage.StorageOptions;
2626

27-
public class RemoveFileOwner {
27+
public class RemoveBlobOwner {
2828

29-
public static void removeFileOwner(
29+
public static void removeBlobOwner(
3030
String projectId, String bucketName, String userEmail, String blobName) {
3131
// The ID of your GCP project
3232
// String projectId = "your-project-id";

samples/snippets/src/main/java/com/example/storage/object/SetObjectMetadata.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.google.cloud.storage.Blob;
2222
import com.google.cloud.storage.BlobId;
23+
import com.google.cloud.storage.BlobInfo;
2324
import com.google.cloud.storage.Storage;
2425
import com.google.cloud.storage.StorageOptions;
2526
import java.util.HashMap;
@@ -53,7 +54,8 @@ public static void setObjectMetadata(String projectId, String bucketName, String
5354

5455
// Does an upsert operation, if the key already exists it's replaced by the new value, otherwise
5556
// it's added.
56-
blob.toBuilder().setMetadata(newMetadata).build().update(precondition);
57+
BlobInfo pendingUpdate = blob.toBuilder().setMetadata(newMetadata).build();
58+
storage.update(pendingUpdate, precondition);
5759

5860
System.out.println(
5961
"Updated custom metadata for object " + objectName + " in bucket " + bucketName);

0 commit comments

Comments
 (0)