|
24 | 24 | import com.google.common.collect.Iterables; |
25 | 25 | import com.google.common.collect.Lists; |
26 | 26 | import com.google.common.collect.Sets; |
| 27 | +import com.google.gcloud.AuthCredentials; |
27 | 28 | import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; |
28 | 29 | import com.google.gcloud.Page; |
29 | 30 | import com.google.gcloud.ReadChannel; |
@@ -1476,23 +1477,48 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx |
1476 | 1477 | WriteChannel writer(BlobInfo blobInfo, BlobWriteOption... options); |
1477 | 1478 |
|
1478 | 1479 | /** |
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)}. |
1485 | 1494 | * |
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: |
1487 | 1497 | * <pre> {@code |
1488 | 1498 | * service.signUrl(BlobInfo.builder("bucket", "name").build(), 14, TimeUnit.DAYS); |
1489 | 1499 | * }</pre> |
1490 | 1500 | * |
| 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 | + * |
1491 | 1509 | * @param blobInfo the blob associated with the signed URL |
1492 | 1510 | * @param duration time until the signed URL expires, expressed in {@code unit}. The finest |
1493 | 1511 | * granularity supported is 1 second, finer granularities will be truncated |
1494 | 1512 | * @param unit time unit of the {@code duration} parameter |
1495 | 1513 | * @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} |
1496 | 1522 | * @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed-URLs</a> |
1497 | 1523 | */ |
1498 | 1524 | URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOption... options); |
|
0 commit comments