From ed25ac4ea21ad6996e625042931ed40df2a8b208 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 27 Mar 2019 12:28:58 -0700 Subject: [PATCH] Add V4 samples (#4753) --- .../storage/snippets/StorageSnippets.java | 43 +++++++++++++++++++ .../storage/snippets/ITStorageSnippets.java | 26 +++++++++++ 2 files changed, 69 insertions(+) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java index 015c2d785f89..a3be11591123 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java @@ -1474,4 +1474,47 @@ public Bucket getBucketPolicyOnly(String bucketName) throws StorageException { // [END storage_get_bucket_policy_only] return bucket; } + + /** Example of how to generate a GET V4 Signed URL */ + public URL generateV4GetObjectSignedUrl(String bucketName, String objectName) throws StorageException { + // [START storage_generate_signed_url_v4] + // Instantiate a Google Cloud Storage client + Storage storage = StorageOptions.getDefaultInstance().getService(); + + // The name of a bucket, e.g. "my-bucket" + // String bucketName = "my-bucket"; + + // The name of an object, e.g. "my-object" + // String objectName = "my-object"; + + BlobInfo blobinfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build(); + URL url = storage.signUrl(blobinfo, 7, TimeUnit.DAYS, Storage.SignUrlOption.withV4Signature()); + + System.out.println("Generated GET signed URL:"); + System.out.println(url); + // [END storage_generate_signed_url_v4] + return url; + } + + /** Example of how to generate a PUT V4 Signed URL */ + public URL generateV4GPutbjectSignedUrl(String bucketName, String objectName) throws StorageException { + // [START storage_generate_upload_signed_url_v4] + // Instantiate a Google Cloud Storage client + Storage storage = StorageOptions.getDefaultInstance().getService(); + + // The name of a bucket, e.g. "my-bucket" + // String bucketName = "my-bucket"; + + // The name of a new object to upload, e.g. "my-object" + // String objectName = "my-object"; + + BlobInfo blobinfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build(); + URL url = storage.signUrl(blobinfo, 7, TimeUnit.DAYS, Storage.SignUrlOption.httpMethod(HttpMethod.PUT), + Storage.SignUrlOption.withV4Signature()); + + System.out.println("Generated PUT signed URL:"); + System.out.println(url); + // [END storage_generate_upload_signed_url_v4] + return url; + } } diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java index 3d44c94ad34a..fceba38277f6 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java @@ -45,8 +45,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; +import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; +import javax.net.ssl.HttpsURLConnection; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Date; @@ -581,4 +583,28 @@ public void testBucketPolicyOnly() { bucket = storageSnippets.disableBucketPolicyOnly(tempBucket); assertFalse(bucket.getIamConfiguration().isBucketPolicyOnlyEnabled()); } + + @Test + public void testV4SignedURLs() throws IOException{ + String tempBucket = RemoteStorageHelper.generateBucketName(); + Bucket bucket = storageSnippets.createBucket(tempBucket); + assertNotNull(bucket); + String tempObject = "test-upload-signed-url-object"; + URL uploadUrl = storageSnippets.generateV4GPutbjectSignedUrl(tempBucket, tempObject); + HttpsURLConnection connection = (HttpsURLConnection)uploadUrl.openConnection(); + connection.setRequestMethod("PUT"); + connection.setDoOutput(true); + byte[] write = new byte[BLOB_BYTE_CONTENT.length]; + try (OutputStream out = connection.getOutputStream()) { + out.write(BLOB_BYTE_CONTENT); + assertEquals(connection.getResponseCode(), 200); + } + URL downloadUrl = storageSnippets.generateV4GetObjectSignedUrl(tempBucket, tempObject); + connection = (HttpsURLConnection)downloadUrl.openConnection(); + byte[] readBytes = new byte[BLOB_BYTE_CONTENT.length]; + try (InputStream responseStream = connection.getInputStream()) { + assertEquals(BLOB_BYTE_CONTENT.length, responseStream.read(readBytes)); + assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); + } + } }