return storage.signUrl(this, duration, unit, options);
/** * Example of creating a signed URL that is valid for 2 weeks, using the default credentials for * signing the URL. */ // [TARGET signUrl(BlobInfo, long, TimeUnit, SignUrlOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public URL signUrl(String bucketName, String blobName) { // [START signUrl] URL signedUrl = storage.signUrl(BlobInfo.newBuilder(bucketName, blobName).build(), 14, TimeUnit.DAYS); // [END signUrl] return signedUrl; }
/** * Example of creating a signed URL passing the {@link * SignUrlOption#signWith(ServiceAccountSigner)} option, that will be used for signing the URL. */ // [TARGET signUrl(BlobInfo, long, TimeUnit, SignUrlOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "/path/to/key.json"] public URL signUrlWithSigner(String bucketName, String blobName, String keyPath) throws IOException { // [START signUrlWithSigner] URL signedUrl = storage.signUrl( BlobInfo.newBuilder(bucketName, blobName).build(), 14, TimeUnit.DAYS, SignUrlOption.signWith( ServiceAccountCredentials.fromStream(new FileInputStream(keyPath)))); // [END signUrlWithSigner] return signedUrl; }
@Test public void testSignUrl() throws Exception { initializeExpectedBlob(2); URL url = new URL("http://localhost:123/bla"); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.signUrl(expectedBlob, 100, TimeUnit.SECONDS)).andReturn(url); replay(storage); initializeBlob(); assertEquals(url, blob.signUrl(100, TimeUnit.SECONDS)); }
storage = options.toBuilder().setCredentials(credentials).build().getService(); URL url = storage.signUrl( BLOB_INFO1, 14,
new ServiceAccountCredentials(null, ACCOUNT, privateKey, null, null); storage = options.toBuilder().setCredentials(credentials).build().getService(); URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS); String stringUrl = url.toString(); String expectedUrl =
storage = options.toBuilder().setCredentials(credentials).build().getService(); URL url = storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS); String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName); String stringUrl = url.toString();
storage.signUrl( BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14,
storage = options.toBuilder().setCredentials(credentials).build().getService(); URL url = storage.signUrl( BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14,
String blobName = "/a" + specialChar + "b"; URL url = storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS); String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName).replace("?", "%3F");
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS); String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName); String stringUrl = url.toString();
String blobName = "/a" + specialChar + "b"; URL url = storage.signUrl( BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14,
extHeaders.put("x-goog-meta-owner", "myself"); URL url = storage.signUrl( BLOB_INFO1, 14,
storage = options.toBuilder().setCredentials(credentials).build().getService(); URL url = storage.signUrl( BLOB_INFO1, 14,
extHeaders.put("x-goog-meta-owner", "myself"); URL url = storage.signUrl( BLOB_INFO1, 14,
storage = options.toBuilder().setCredentials(credentials).build().getService(); URL url = storage.signUrl( BLOB_INFO1, 14,
@Test public void testGetSignedUrl() throws IOException { if (storage.getOptions().getCredentials() != null) { assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner); } String blobName = "test-get-signed-url-blob/with/slashes/and?special=!#$&'()*+,:;=?@[]"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); URL url = storage.signUrl(blob, 1, TimeUnit.HOURS); URLConnection connection = url.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); } }
@Test public void testPostSignedUrl() throws IOException { if (storage.getOptions().getCredentials() != null) { assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner); } String blobName = "test-post-signed-url-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); assertNotNull(storage.create(blob)); URL url = storage.signUrl(blob, 1, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.POST)); URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.connect(); Blob remoteBlob = storage.get(BUCKET, blobName); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); }
/** * Creates a signed URL to an object, if it exists. This method will fail if this storage * resource was not created using service account credentials. * @param timeUnit the time unit used to determine how long the URL is valid. * @param timePeriods the number of periods to determine how long the URL is valid. * @param options specifies additional options for signing URLs * @return the URL if the object exists, and null if it does not. * @throws IllegalStateException if the resource reference is to a bucket, and not a blob. * @throws IOException if there are errors in accessing Google Storage */ public URL createSignedUrl(TimeUnit timeUnit, long timePeriods, Storage.SignUrlOption... options) throws IOException { if (LOGGER.isWarnEnabled()) { if (!exists()) { LOGGER.warn("Creating signed URL for non-existing GCS object " + this.location); } } return this.storage.signUrl( BlobInfo.newBuilder(getBlobId()).build(), timePeriods, timeUnit, options); }
/** * Creates a signed URL to an object, if it exists. This method will fail if this storage * resource was not created using service account credentials. * @param timeUnit the time unit used to determine how long the URL is valid. * @param timePeriods the number of periods to determine how long the URL is valid. * @param options specifies additional options for signing URLs * @return the URL if the object exists, and null if it does not. * @throws IllegalStateException if the resource reference is to a bucket, and not a blob. * @throws IOException if there are errors in accessing Google Storage */ public URL createSignedUrl(TimeUnit timeUnit, long timePeriods, Storage.SignUrlOption... options) throws IOException { if (LOGGER.isWarnEnabled()) { if (!exists()) { LOGGER.warn("Creating signed URL for non-existing GCS object " + this.location); } } return this.storage.signUrl( BlobInfo.newBuilder(getBlobId()).build(), timePeriods, timeUnit, options); }