From 3b5bb7e28f15d96c37deb2778fbe67a111597047 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 27 Mar 2019 12:22:53 -0700 Subject: [PATCH 1/3] Add V4 samples --- .../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); + } + } } From a2c35d4006517d027083359eaa0a0147ac1b1af6 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 27 Mar 2019 12:58:42 -0700 Subject: [PATCH 2/3] Match C++ samples --- .../storage/snippets/StorageSnippets.java | 15 +++++++++++++-- .../storage/snippets/ITStorageSnippets.java | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) 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 a3be11591123..aff27c512ab6 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 @@ -1488,10 +1488,12 @@ public URL generateV4GetObjectSignedUrl(String bucketName, String objectName) th // String objectName = "my-object"; BlobInfo blobinfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build(); - URL url = storage.signUrl(blobinfo, 7, TimeUnit.DAYS, Storage.SignUrlOption.withV4Signature()); + URL url = storage.signUrl(blobinfo, 15, TimeUnit.MINUTES, Storage.SignUrlOption.withV4Signature()); System.out.println("Generated GET signed URL:"); System.out.println(url); + System.out.println("You can use this URL with any user agent, for example:"); + System.out.println("curl '" + url + "'"); // [END storage_generate_signed_url_v4] return url; } @@ -1508,12 +1510,21 @@ public URL generateV4GPutbjectSignedUrl(String bucketName, String objectName) th // The name of a new object to upload, e.g. "my-object" // String objectName = "my-object"; + // Define Resource BlobInfo blobinfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build(); - URL url = storage.signUrl(blobinfo, 7, TimeUnit.DAYS, Storage.SignUrlOption.httpMethod(HttpMethod.PUT), + + // Generate Signed URL + Map extensionHeaders = new HashMap<>(); + extensionHeaders.put("Content-Type", "application/octet-stream"); + + URL url = storage.signUrl(blobinfo, 15, TimeUnit.MINUTES, Storage.SignUrlOption.httpMethod(HttpMethod.PUT), + Storage.SignUrlOption.withExtHeaders(extensionHeaders), Storage.SignUrlOption.withV4Signature()); System.out.println("Generated PUT signed URL:"); System.out.println(url); + System.out.println("You can use this URL with any user agent, for example:"); + System.out.println("curl -X PUT -H 'Content-Type: application/octet-stream'--upload-file my-file '" + 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 fceba38277f6..12629b634404 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 @@ -594,6 +594,7 @@ public void testV4SignedURLs() throws IOException{ HttpsURLConnection connection = (HttpsURLConnection)uploadUrl.openConnection(); connection.setRequestMethod("PUT"); connection.setDoOutput(true); + connection.setRequestProperty("Content-Type", "application/octet-stream"); byte[] write = new byte[BLOB_BYTE_CONTENT.length]; try (OutputStream out = connection.getOutputStream()) { out.write(BLOB_BYTE_CONTENT); From 280bd70bb2d69197080f6eee934dbb56dbbcbdff Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 27 Mar 2019 15:15:13 -0700 Subject: [PATCH 3/3] Add missing import --- .../google/cloud/examples/storage/snippets/StorageSnippets.java | 1 + 1 file changed, 1 insertion(+) 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 2ad2df7f0369..71357c2d61b3 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 @@ -39,6 +39,7 @@ import com.google.cloud.storage.Bucket; import com.google.cloud.storage.BucketInfo; import com.google.cloud.storage.CopyWriter; +import com.google.cloud.storage.HttpMethod; import com.google.cloud.storage.Storage; import com.google.cloud.storage.Storage.BlobGetOption; import com.google.cloud.storage.Storage.BlobListOption;