Skip to content

Commit 3b083a6

Browse files
committed
Merge pull request #743 from mziccard/signurl-javadoc
Add more detailed javadoc to Blob and Storage signUrl
2 parents 646f3c9 + 4e0248f commit 3b083a6

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.google.api.services.storage.model.StorageObject;
2424
import com.google.common.base.Function;
25+
import com.google.gcloud.AuthCredentials;
2526
import com.google.gcloud.ReadChannel;
2627
import com.google.gcloud.WriteChannel;
2728
import com.google.gcloud.storage.Storage.BlobTargetOption;
@@ -453,16 +454,48 @@ public WriteChannel writer(BlobWriteOption... options) {
453454
}
454455

455456
/**
456-
* Generates a signed URL for this blob. If you want to allow access to for a fixed amount of time
457-
* for this blob, you can use this method to generate a URL that is only valid within a certain
458-
* time period. This is particularly useful if you don't want publicly accessible blobs, but don't
459-
* want to require users to explicitly log in.
457+
* Generates a signed URL for this blob. If you want to allow access for a fixed amount of time to
458+
* this blob, you can use this method to generate a URL that is only valid within a certain time
459+
* period. This is particularly useful if you don't want publicly accessible blobs, but also don't
460+
* want to require users to explicitly log in. Signing a URL requires a service account and its
461+
* associated private key. If a {@link AuthCredentials.ServiceAccountAuthCredentials} was passed
462+
* to {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials
463+
* are being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then
464+
* {@code signUrl} will use that service account and associated key to sign the URL. If the
465+
* credentials passed to {@link StorageOptions} do not expose a private key (this is the case for
466+
* App Engine credentials, Compute Engine credentials and Google Cloud SDK credentials) then
467+
* {@code signUrl} will throw an {@link IllegalArgumentException} unless a service account with
468+
* associated key is passed using the {@code SignUrlOption.serviceAccount()} option. The service
469+
* account and private key passed with {@code SignUrlOption.serviceAccount()} have priority over
470+
* any credentials set with {@link StorageOptions.Builder#authCredentials(AuthCredentials)}.
471+
*
472+
* <p>Example usage of creating a signed URL that is valid for 2 weeks, using the default
473+
* credentials for signing the URL:
474+
* <pre> {@code
475+
* blob.signUrl(14, TimeUnit.DAYS);
476+
* }</pre>
477+
*
478+
* <p>Example usage of creating a signed URL passing the {@code SignUrlOption.serviceAccount()}
479+
* option, that will be used for signing the URL:
480+
* <pre> {@code
481+
* blob.signUrl(14, TimeUnit.DAYS, SignUrlOption.serviceAccount(
482+
* AuthCredentials.createForJson(new FileInputStream("/path/to/key.json"))));
483+
* }</pre>
460484
*
461485
* @param duration time until the signed URL expires, expressed in {@code unit}. The finer
462486
* granularity supported is 1 second, finer granularities will be truncated
463487
* @param unit time unit of the {@code duration} parameter
464488
* @param options optional URL signing options
465489
* @return a signed URL for this bucket and the specified options
490+
* @throws IllegalArgumentException if
491+
* {@link SignUrlOption#serviceAccount(AuthCredentials.ServiceAccountAuthCredentials)} was not
492+
* used and no service account was provided to {@link StorageOptions}
493+
* @throws IllegalArgumentException if the key associated to the provided service account is
494+
* invalid
495+
* @throws IllegalArgumentException if {@link SignUrlOption#withMd5()} option is used and
496+
* {@link #md5()} is {@code null}
497+
* @throws IllegalArgumentException if {@link SignUrlOption#withContentType()} option is used and
498+
* {@link #contentType()} is {@code null}
466499
* @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed-URLs</a>
467500
*/
468501
public URL signUrl(long duration, TimeUnit unit, SignUrlOption... options) {

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.common.collect.Iterables;
2525
import com.google.common.collect.Lists;
2626
import com.google.common.collect.Sets;
27+
import com.google.gcloud.AuthCredentials;
2728
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
2829
import com.google.gcloud.Page;
2930
import com.google.gcloud.ReadChannel;
@@ -1476,23 +1477,48 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx
14761477
WriteChannel writer(BlobInfo blobInfo, BlobWriteOption... options);
14771478

14781479
/**
1479-
* Generates a signed URL for a blob.
1480-
* If you have a blob that you want to allow access to for a fixed
1481-
* amount of time, you can use this method to generate a URL that
1482-
* is only valid within a certain time period.
1483-
* This is particularly useful if you don't want publicly
1484-
* accessible blobs, but don't want to require users to explicitly log in.
1480+
* Generates a signed URL for a blob. If you have a blob that you want to allow access to for a
1481+
* fixed amount of time, you can use this method to generate a URL that is only valid within a
1482+
* certain time period. This is particularly useful if you don't want publicly accessible blobs,
1483+
* but also don't want to require users to explicitly log in. Signing a URL requires a service
1484+
* account and its associated private key. If a {@link ServiceAccountAuthCredentials} was passed
1485+
* to {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials
1486+
* are being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then
1487+
* {@code signUrl} will use that service account and associated key to sign the URL. If the
1488+
* credentials passed to {@link StorageOptions} do not expose a private key (this is the case for
1489+
* App Engine credentials, Compute Engine credentials and Google Cloud SDK credentials) then
1490+
* {@code signUrl} will throw an {@link IllegalArgumentException} unless a service account with
1491+
* associated key is passed using the {@code SignUrlOption.serviceAccount()} option. The service
1492+
* account and private key passed with {@code SignUrlOption.serviceAccount()} have priority over
1493+
* any credentials set with {@link StorageOptions.Builder#authCredentials(AuthCredentials)}.
14851494
*
1486-
* <p>Example usage of creating a signed URL that is valid for 2 weeks:
1495+
* <p>Example usage of creating a signed URL that is valid for 2 weeks, using the default
1496+
* credentials for signing the URL:
14871497
* <pre> {@code
14881498
* service.signUrl(BlobInfo.builder("bucket", "name").build(), 14, TimeUnit.DAYS);
14891499
* }</pre>
14901500
*
1501+
* <p>Example usage of creating a signed URL passing the {@code SignUrlOption.serviceAccount()}
1502+
* option, that will be used for signing the URL:
1503+
* <pre> {@code
1504+
* service.signUrl(BlobInfo.builder("bucket", "name").build(), 14, TimeUnit.DAYS,
1505+
* SignUrlOption.serviceAccount(
1506+
* AuthCredentials.createForJson(new FileInputStream("/path/to/key.json"))));
1507+
* }</pre>
1508+
*
14911509
* @param blobInfo the blob associated with the signed URL
14921510
* @param duration time until the signed URL expires, expressed in {@code unit}. The finest
14931511
* granularity supported is 1 second, finer granularities will be truncated
14941512
* @param unit time unit of the {@code duration} parameter
14951513
* @param options optional URL signing options
1514+
* @throws IllegalArgumentException if {@code SignUrlOption.serviceAccount()} was not used and no
1515+
* service account was provided to {@link StorageOptions}
1516+
* @throws IllegalArgumentException if the key associated to the provided service account is
1517+
* invalid
1518+
* @throws IllegalArgumentException if {@code SignUrlOption.withMd5()} option is used and
1519+
* {@code blobInfo.md5()} is {@code null}
1520+
* @throws IllegalArgumentException if {@code SignUrlOption.withContentType()} option is used and
1521+
* {@code blobInfo.contentType()} is {@code null}
14961522
* @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed-URLs</a>
14971523
*/
14981524
URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOption... options);

0 commit comments

Comments
 (0)