Java Code Examples for com.google.cloud.storage.Storage#signUrl()

The following examples show how to use com.google.cloud.storage.Storage#signUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenerateV4GetObjectSignedUrl.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/**
 * Signing a URL requires Credentials which implement ServiceAccountSigner. These can be set
 * explicitly using the Storage.SignUrlOption.signWith(ServiceAccountSigner) option. If you don't,
 * you could also pass a service account signer to StorageOptions, i.e.
 * StorageOptions().newBuilder().setCredentials(ServiceAccountSignerCredentials). In this example,
 * neither of these options are used, which means the following code only works when the
 * credentials are defined via the environment variable GOOGLE_APPLICATION_CREDENTIALS, and those
 * credentials are authorized to sign a URL. See the documentation for Storage.signUrl for more
 * details.
 */
public static void generateV4GetObjectSignedUrl(
    String projectId, String bucketName, String objectName) throws StorageException {
  // String projectId = "my-project-id";
  // String bucketName = "my-bucket";
  // String objectName = "my-object";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

  // Define resource
  BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build();

  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 + "'");
}
 
Example 2
Source File: BucketUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Take a GCS path and return a signed url to the same resource which allows unauthenticated users to access the file.
 * @param path String representing a GCS path
 * @param hoursToLive how long in hours the url will remain valid
 * @return A signed url which provides access to the bucket location over http allowing unauthenticated users to access it
 */
public static String createSignedUrlToGcsObject(String path, final long hoursToLive) {
    final Storage storage = StorageOptions.getDefaultInstance().getService();
    final BlobInfo info = BlobInfo.newBuilder(getBucket(path), getPathWithoutBucket(path)).build();
    final URL signedUrl = storage.signUrl(info, hoursToLive, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.GET));
    return signedUrl.toString();
}
 
Example 3
Source File: GenerateV4PutObjectSignedUrl.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/**
 * Signing a URL requires Credentials which implement ServiceAccountSigner. These can be set
 * explicitly using the Storage.SignUrlOption.signWith(ServiceAccountSigner) option. If you don't,
 * you could also pass a service account signer to StorageOptions, i.e.
 * StorageOptions().newBuilder().setCredentials(ServiceAccountSignerCredentials). In this example,
 * neither of these options are used, which means the following code only works when the
 * credentials are defined via the environment variable GOOGLE_APPLICATION_CREDENTIALS, and those
 * credentials are authorized to sign a URL. See the documentation for Storage.signUrl for more
 * details.
 */
public static void generateV4GPutObjectSignedUrl(
    String projectId, String bucketName, String objectName) throws StorageException {
  // String projectId = "my-project-id";
  // String bucketName = "my-bucket";
  // String objectName = "my-object";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

  // Define Resource
  BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build();

  // Generate Signed URL
  Map<String, String> 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
          + "'");
}