Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}