diff --git a/README.md b/README.md index 1ffef84c0..c9fcb38d0 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/ | Sample | Source Code | Try it | | --------------------------- | --------------------------------- | ------ | +| Configure Retries | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java) | | Quickstart Sample | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/QuickstartSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/QuickstartSample.java) | diff --git a/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java b/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java new file mode 100644 index 000000000..6c031e540 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/ConfigureRetries.java @@ -0,0 +1,67 @@ +/* + * Copyright 2021 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. + */ + +package com.example.storage; +// [START storage_configure_retries] + +import com.google.api.gax.retrying.RetrySettings; +import com.google.cloud.storage.BlobId; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import com.google.cloud.storage.StorageRetryStrategy; +import org.threeten.bp.Duration; + +public final class ConfigureRetries { + public static void main(String[] args) { + String bucketName = "my-bucket"; + String blobName = "blob/to/delete"; + deleteBlob(bucketName, blobName); + } + + static void deleteBlob(String bucketName, String blobName) { + // Update the retry settings + RetrySettings retrySettings = + StorageOptions.getDefaultRetrySettings() + .toBuilder() + // to set the max number of attempts to 10 + .setMaxAttempts(10) + // to set the backoff multiplier to 3.0 + .setRetryDelayMultiplier(3.0) + // to set the max duration of all attempts to 5 minutes + .setTotalTimeout(Duration.ofMinutes(5)) + .build(); + + StorageOptions alwaysRetryStorageOptions = + StorageOptions.newBuilder() + // Configure our options so all requests will be retried even if they are + // non-idempotent. + .setStorageRetryStrategy(StorageRetryStrategy.getUniformStorageRetryStrategy()) + // provide the previously configured retrySettings + .setRetrySettings(retrySettings) + .build(); + + // Instantiate a client with our options + Storage storage = alwaysRetryStorageOptions.getService(); + + // Delete the blob + BlobId blobId = BlobId.of(bucketName, blobName); + boolean success = storage.delete(blobId); + + System.out.printf( + "Deletion of Blob %s completed %s.%n", blobId, success ? "successfully" : "unsuccessfully"); + } +} +// [END storage_configure_retries] diff --git a/samples/snippets/src/test/java/com/example/storage/ConfigureRetriesTest.java b/samples/snippets/src/test/java/com/example/storage/ConfigureRetriesTest.java new file mode 100644 index 000000000..76ae1e38c --- /dev/null +++ b/samples/snippets/src/test/java/com/example/storage/ConfigureRetriesTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2021 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. + */ + +package com.example.storage; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import com.google.cloud.storage.testing.RemoteStorageHelper; +import com.google.cloud.testing.junit4.StdOutCaptureRule; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public final class ConfigureRetriesTest { + @Rule public StdOutCaptureRule stdOut = new StdOutCaptureRule(); + + private String bucketName; + private Storage storage; + private String blobName; + + private Blob blob; + + @Before + public void setUp() { + blobName = "blob"; + bucketName = RemoteStorageHelper.generateBucketName(); + storage = StorageOptions.getDefaultInstance().getService(); + storage.create(BucketInfo.of(bucketName)); + blob = storage.create(BlobInfo.newBuilder(bucketName, blobName).build()); + } + + @After + public void tearDown() { + if (blob != null && blob.exists()) { + blob.delete(); + } + storage.delete(bucketName); + } + + @Test + public void testConfigureRetries() { + ConfigureRetries.deleteBlob(bucketName, blobName); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("Deletion"); + assertThat(stdOut.getCapturedOutputAsUtf8String()).contains("successfully"); + assertThat(stdOut.getCapturedOutputAsUtf8String()).doesNotContain("unsuccessfully"); + } +}