com.google.cloud.storage.StorageOptions Java Examples

The following examples show how to use com.google.cloud.storage.StorageOptions. 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: AbstractGCSProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected StorageOptions getServiceOptions(ProcessContext context, GoogleCredentials credentials) {
    final String projectId = context.getProperty(PROJECT_ID).evaluateAttributeExpressions().getValue();
    final Integer retryCount = context.getProperty(RETRY_COUNT).asInteger();

    StorageOptions.Builder storageOptionsBuilder = StorageOptions.newBuilder()
            .setCredentials(credentials)
            .setRetrySettings(RetrySettings.newBuilder()
                    .setMaxAttempts(retryCount)
                    .build());

    if (projectId != null && !projectId.isEmpty()) {
        storageOptionsBuilder.setProjectId(projectId);
    }

    return  storageOptionsBuilder.setTransportOptions(getTransportOptions(context)).build();
}
 
Example #2
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of removing a retention policy on a bucket */
public Bucket removeRetentionPolicy(String bucketName)
    throws StorageException, IllegalArgumentException {
  // [START storage_remove_retention_policy]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.RETENTION_POLICY));
  if (bucket.retentionPolicyIsLocked() != null && bucket.retentionPolicyIsLocked()) {
    throw new IllegalArgumentException(
        "Unable to remove retention period as retention policy is locked.");
  }

  Bucket bucketWithoutRetentionPolicy =
      bucket.toBuilder().setRetentionPeriod(null).build().update();

  System.out.println("Retention period for " + bucketName + " has been removed");
  // [END storage_remove_retention_policy]
  return bucketWithoutRetentionPolicy;
}
 
Example #3
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to release a temporary hold for a blob */
public Blob releaseTemporaryHold(String bucketName, String blobName) throws StorageException {
  // [START storage_release_temporary_hold]
  // 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 blob, e.g. "my-blob"
  // String blobName = "my-blob";

  BlobId blobId = BlobId.of(bucketName, blobName);
  Blob blob = storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(false).build());

  System.out.println("Temporary hold was released for " + blobName);
  // [END storage_release_temporary_hold]
  return blob;
}
 
Example #4
Source File: DownloadEncryptedObject.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void downloadEncryptedObject(
    String projectId,
    String bucketName,
    String objectName,
    Path destFilePath,
    String decryptionKey)
    throws IOException {

  // The ID of your GCP project
  // String projectId = "your-project-id";

  // The ID of your GCS bucket
  // String bucketName = "your-unique-bucket-name";

  // The ID of your GCS object
  // String objectName = "your-object-name";

  // The path to which the file should be downloaded
  // Path destFilePath = Paths.get("/local/path/to/file.txt");

  // The Base64 encoded decryption key, which should be the same key originally used to encrypt
  // the object
  // String decryptionKey = "TIbv/fjexq+VmtXzAlc63J4z5kFmWJ6NdAPQulQBT7g=";

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

  Blob blob = storage.get(bucketName, objectName);
  blob.downloadTo(destFilePath, Blob.BlobSourceOption.decryptionKey(decryptionKey));

  System.out.println(
      "Downloaded object "
          + objectName
          + " from bucket name "
          + bucketName
          + " to "
          + destFilePath
          + " using customer-supplied encryption key");
}
 
Example #5
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to set a temporary hold for a blob */
public Blob setTemporaryHold(String bucketName, String blobName) throws StorageException {
  // [START storage_set_temporary_hold]
  // 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 blob, e.g. "my-blob"
  // String blobName = "my-blob";

  BlobId blobId = BlobId.of(bucketName, blobName);
  Blob blob = storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(true).build());

  System.out.println("Temporary hold was set for " + blobName);
  // [END storage_set_temporary_hold]
  return blob;
}
 
Example #6
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to get default event-based hold for a bucket */
public Bucket getDefaultEventBasedHold(String bucketName) throws StorageException {
  // [START storage_get_default_event_based_hold]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket =
      storage.get(bucketName, BucketGetOption.fields(BucketField.DEFAULT_EVENT_BASED_HOLD));

  if (bucket.getDefaultEventBasedHold() != null && bucket.getDefaultEventBasedHold()) {
    System.out.println("Default event-based hold is enabled for " + bucketName);
  } else {
    System.out.println("Default event-based hold is not enabled for " + bucketName);
  }
  // [END storage_get_default_event_based_hold]
  return bucket;
}
 
Example #7
Source File: GcsPinotFS.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@Override
public void init(Configuration config) {
  LOGGER.info("Configs are: {}, {}",
          PROJECT_ID,
          config.getString(PROJECT_ID));

  checkArgument(!isNullOrEmpty(config.getString(PROJECT_ID)));
  checkArgument(!isNullOrEmpty(config.getString(GCP_KEY)));
  String projectId = config.getString(PROJECT_ID);
  String gcpKey = config.getString(GCP_KEY);
  try {
    storage = StorageOptions.newBuilder()
        .setProjectId(projectId)
        .setCredentials(GoogleCredentials.fromStream(Files.newInputStream(Paths.get(gcpKey))))
        .build()
        .getService();
  } catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}
 
Example #8
Source File: GoogleCloudStorage.java    From metastore with Apache License 2.0 6 votes vote down vote up
public void init(RegistryInfo registryInfo, Map<String, String> config, String extension) {
  this.storage = StorageOptions.getDefaultInstance().getService();
  String project = ServiceOptions.getDefaultProjectId();

  if (config.get("project") == null && project == null) {
    throw new RuntimeException("project variable not set");
  }
  if (config.get("bucket") == null) {
    throw new RuntimeException("bucket variable not set");
  }
  if (config.get("path") == null) {
    throw new RuntimeException("path variable not set");
  }

  if (config.get("project") != null) {
    this.project = config.get("project");
  }
  this.bucket = config.get("bucket");
  if (config.get("path").endsWith("/")) {
    this.fileName = config.get("path") + registryInfo.getName() + "." + extension;
  } else {
    this.fileName = config.get("path") + "/" + registryInfo.getName() + "." + extension;
  }
}
 
Example #9
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to disable default event-based hold for a bucket */
public Bucket disableDefaultEventBasedHold(String bucketName) throws StorageException {
  // [START storage_disable_default_event_based_hold]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket =
      storage.update(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(false).build());

  System.out.println("Default event-based hold was disabled for " + bucketName);
  // [END storage_disable_default_event_based_hold]
  return bucket;
}
 
Example #10
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to release event-based hold for a blob */
public Blob releaseEventBasedHold(String bucketName, String blobName) throws StorageException {
  // [START storage_release_event_based_hold]
  // 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 blob, e.g. "my-blob"
  // String blobName = "my-blob";

  BlobId blobId = BlobId.of(bucketName, blobName);
  Blob blob = storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(false).build());

  System.out.println("Event-based hold was released for " + blobName);
  // [END storage_release_event_based_hold]
  return blob;
}
 
Example #11
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to set event-based hold for a blob */
public Blob setEventBasedHold(String bucketName, String blobName) throws StorageException {
  // [START storage_set_event_based_hold]
  // 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 blob, e.g. "my-blob"
  // String blobName = "my-blob";

  BlobId blobId = BlobId.of(bucketName, blobName);
  Blob blob = storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(true).build());

  System.out.println("Event-based hold was set for " + blobName);
  // [END storage_set_event_based_hold]
  return blob;
}
 
Example #12
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to get a bucket's retention policy */
public Bucket getRetentionPolicy(String bucketName) throws StorageException {
  // [START storage_get_retention_policy]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.RETENTION_POLICY));

  System.out.println("Retention Policy for " + bucketName);
  System.out.println("Retention Period: " + bucket.getRetentionPeriod());
  if (bucket.retentionPolicyIsLocked() != null && bucket.retentionPolicyIsLocked()) {
    System.out.println("Retention Policy is locked");
  }
  if (bucket.getRetentionEffectiveTime() != null) {
    System.out.println("Effective Time: " + new Date(bucket.getRetentionEffectiveTime()));
  }
  // [END storage_get_retention_policy]
  return bucket;
}
 
Example #13
Source File: DownloadRequesterPaysObject.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void downloadRequesterPaysObject(
    String projectId, String bucketName, String objectName, Path destFilePath) {
  // The project ID to bill
  // String projectId = "my-billable-project-id";

  // The ID of your GCS bucket
  // String bucketName = "your-unique-bucket-name";

  // The ID of your GCS object
  // String objectName = "your-object-name";

  // The path to which the file should be downloaded
  // Path destFilePath = Paths.get("/local/path/to/file.txt");

  Storage storage = StorageOptions.getDefaultInstance().getService();
  Blob blob = storage.get(BlobId.of(bucketName, objectName));
  blob.downloadTo(destFilePath, Blob.BlobSourceOption.userProject(projectId));

  System.out.println(
      "Object " + objectName + " downloaded to " + destFilePath + " and billed to " + projectId);
}
 
Example #14
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to enable uniform bucket-level access for a bucket */
public Bucket enableUniformBucketLevelAccess(String bucketName) throws StorageException {
  // [START storage_enable_uniform_bucket_level_access]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  BucketInfo.IamConfiguration iamConfiguration =
      BucketInfo.IamConfiguration.newBuilder().setIsUniformBucketLevelAccessEnabled(true).build();
  Bucket bucket =
      storage.update(
          BucketInfo.newBuilder(bucketName).setIamConfiguration(iamConfiguration).build());

  System.out.println("Uniform bucket-level access was enabled for " + bucketName);
  // [END storage_enable_uniform_bucket_level_access]
  return bucket;
}
 
Example #15
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to enable default event-based hold for a bucket */
public Bucket enableDefaultEventBasedHold(String bucketName) throws StorageException {
  // [START storage_enable_default_event_based_hold]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket =
      storage.update(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(true).build());

  System.out.println("Default event-based hold was enabled for " + bucketName);
  // [END storage_enable_default_event_based_hold]
  return bucket;
}
 
Example #16
Source File: ChangeDefaultStorageClass.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void changeDefaultStorageClass(String projectId, String bucketName) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

  // The ID of your GCS bucket
  // String bucketName = "your-unique-bucket-name";

  // See the StorageClass documentation for other valid storage classes:
  // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
  StorageClass storageClass = StorageClass.COLDLINE;

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  Bucket bucket = storage.get(bucketName);
  bucket = bucket.toBuilder().setStorageClass(storageClass).build().update();

  System.out.println(
      "Default storage class for bucket "
          + bucketName
          + " has been set to "
          + bucket.getStorageClass());
}
 
Example #17
Source File: StreamingAnnotationToStorageIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamingAnnotationToStorage() {
  String gcsUri = String.format("gs://%s/%s", PROJECT_ID, OUTPUT_PREFIX);
  StreamingAnnotationToStorage.streamingAnnotationToStorage("resources/cat.mp4", gcsUri);
  String got = bout.toString();

  assertThat(got).contains(String.format("Storage Uri: %s", gcsUri));

  Storage storage = StorageOptions.getDefaultInstance().getService();

  Page<Blob> blobs =
      storage.list(
          PROJECT_ID,
          BlobListOption.currentDirectory(),
          BlobListOption.prefix(OUTPUT_PREFIX + "/"));

  deleteDirectory(storage, blobs);
}
 
Example #18
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of setting a default KMS key on a bucket. */
public Bucket setDefaultKmsKey(String bucketName, String kmsKeyName) throws StorageException {
  // [START storage_set_bucket_default_kms_key]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of the existing bucket to set a default KMS key for, e.g. "my-bucket"
  // String bucketName = "my-bucket"

  // The name of the KMS-key to use as a default
  // Key names are provided in the following format:
  // 'projects/<PROJECT>/locations/<LOCATION>/keyRings/<RING_NAME>/cryptoKeys/<KEY_NAME>'
  // String kmsKeyName = ""

  BucketInfo bucketInfo =
      BucketInfo.newBuilder(bucketName).setDefaultKmsKeyName(kmsKeyName).build();

  Bucket bucket = storage.update(bucketInfo);

  System.out.println("Default KMS Key Name: " + bucket.getDefaultKmsKeyName());
  // [END storage_set_bucket_default_kms_key]
  return bucket;
}
 
Example #19
Source File: BucketUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static StorageOptions.Builder setGenerousTimeouts(StorageOptions.Builder builder) {
    return builder
        .setTransportOptions(HttpTransportOptions.newBuilder()
            .setConnectTimeout(120000)
            .setReadTimeout(120000)
            .build())
        .setRetrySettings(RetrySettings.newBuilder()
            .setMaxAttempts(15)
            .setMaxRetryDelay(Duration.ofMillis(256_000L))
            .setTotalTimeout(Duration.ofMillis(4000_000L))
            .setInitialRetryDelay(Duration.ofMillis(1000L))
            .setRetryDelayMultiplier(2.0)
            .setInitialRpcTimeout(Duration.ofMillis(180_000L))
            .setRpcTimeoutMultiplier(1.0)
            .setMaxRpcTimeout(Duration.ofMillis(180_000L))
            .build());
}
 
Example #20
Source File: AbstractGCSTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testStorageOptionsConfiguration() throws Exception {
    reset(storage);
    final TestRunner runner = buildNewRunner(getProcessor());

    final AbstractGCSProcessor processor = getProcessor();
    final GoogleCredentials mockCredentials = mock(GoogleCredentials.class);

    final StorageOptions options = processor.getServiceOptions(runner.getProcessContext(),
            mockCredentials);

    assertEquals("Project IDs should match",
            PROJECT_ID, options.getProjectId());

    assertEquals("Retry counts should match",
            RETRIES.intValue(), options.getRetrySettings().getMaxAttempts());

    assertSame("Credentials should be configured correctly",
            mockCredentials, options.getCredentials());
}
 
Example #21
Source File: GcsRepositoryAutoConfiguration.java    From hawkbit-extensions with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * GSC storage service
 * 
 * @param gcsProperties
 *            properties to initialize GCS storage service
 * @return the {@link Storage} if no other {@link Storage} bean is registered.
 */
@Bean
@ConditionalOnMissingBean
public Storage gcsStorage(final GcsRepositoryProperties gcsProperties) {
    if (gcsProperties.getCredentialsLocation() != null) {
        try {
            Credentials credentials = GoogleCredentials
                    .fromStream(gcsProperties.getCredentialsLocation().getInputStream());
            return StorageOptions.newBuilder().setCredentials(credentials)
                    .setProjectId(gcsProperties.getProjectId()).build().getService();
        } catch (IOException e) {
            throw new GcpInitialisationFailedException(e);
        }
    } else {
        return StorageOptions.getDefaultInstance().getService();
    }
}
 
Example #22
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to get uniform bucket-level access metadata for a bucket */
public Bucket getUniformBucketLevelAccess(String bucketName) throws StorageException {
  // [START storage_get_uniform_bucket_level_access]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.IAMCONFIGURATION));
  BucketInfo.IamConfiguration iamConfiguration = bucket.getIamConfiguration();

  Boolean enabled = iamConfiguration.isUniformBucketLevelAccessEnabled();
  Date lockedTime = new Date(iamConfiguration.getUniformBucketLevelAccessLockedTime());

  if (enabled != null && enabled) {
    System.out.println("Uniform bucket-level access is enabled for " + bucketName);
    System.out.println("Bucket will be locked on " + lockedTime);
  } else {
    System.out.println("Uniform bucket-level access is disabled for " + bucketName);
  }
  // [END storage_get_uniform_bucket_level_access]
  return bucket;
}
 
Example #23
Source File: GoogleStorageUtils.java    From picard with MIT License 6 votes vote down vote up
private static StorageOptions.Builder setGenerousTimeouts(StorageOptions.Builder builder) {
    return builder
            .setTransportOptions(HttpTransportOptions.newBuilder()
                    .setConnectTimeout(120_000)
                    .setReadTimeout(120_000)
                    .build())
            .setRetrySettings(RetrySettings.newBuilder()
                    .setMaxAttempts(15)
                    .setMaxRetryDelay(Duration.ofMillis(256_000L))
                    .setTotalTimeout(Duration.ofMillis(4000_000L))
                    .setInitialRetryDelay(Duration.ofMillis(1000L))
                    .setRetryDelayMultiplier(2.0)
                    .setInitialRpcTimeout(Duration.ofMillis(180_000L))
                    .setRpcTimeoutMultiplier(1.0)
                    .setMaxRpcTimeout(Duration.ofMillis(180_000L))
                    .build());
}
 
Example #24
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to lock a bucket retention policy */
public Bucket lockRetentionPolicy(String bucketName) throws StorageException {
  // [START storage_lock_retention_policy]
  // Instantiate a Google Cloud Storage client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name of a bucket, e.g. "my-bucket"
  // String bucketName = "my-bucket";

  Bucket bucket =
      storage.get(bucketName, Storage.BucketGetOption.fields(BucketField.METAGENERATION));
  Bucket lockedBucket =
      bucket.lockRetentionPolicy(Storage.BucketTargetOption.metagenerationMatch());

  System.out.println("Retention period for " + bucketName + " is now locked");
  System.out.println(
      "Retention policy effective as of " + new Date(lockedBucket.getRetentionEffectiveTime()));
  // [END storage_lock_retention_policy]
  return lockedBucket;
}
 
Example #25
Source File: ImportProductSetsIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() throws IOException {
  ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_1);
  ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
  Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService();
  // Delete the created blob
  storage.delete(blob.getBlobId());
  System.setOut(null);
}
 
Example #26
Source File: BatchTranslateTextWithModelTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static final void cleanUpBucket() {
  Storage storage = StorageOptions.getDefaultInstance().getService();
  Page<Blob> blobs =
      storage.list(
          PROJECT_ID,
          Storage.BlobListOption.currentDirectory(),
          Storage.BlobListOption.prefix(PREFIX));

  deleteDirectory(storage, blobs);
}
 
Example #27
Source File: RotateObjectEncryptionKey.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void rotateObjectEncryptionKey(
    String projectId,
    String bucketName,
    String objectName,
    String oldEncryptionKey,
    String newEncryptionKey) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

  // The ID of your GCS bucket
  // String bucketName = "your-unique-bucket-name";

  // The ID of your GCS object
  // String objectName = "your-object-name";

  // The Base64 encoded AES-256 encryption key originally used to encrypt the object. See the
  // documentation
  // on Customer-Supplied Encryption keys for more info:
  // https://cloud.google.com/storage/docs/encryption/using-customer-supplied-keys
  // String oldEncryptionKey = "TIbv/fjexq+VmtXzAlc63J4z5kFmWJ6NdAPQulQBT7g="

  // The new encryption key to use
  // String newEncryptionKey = "0mMWhFvQOdS4AmxRpo8SJxXn5MjFhbz7DkKBUdUIef8="

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  BlobId blobId = BlobId.of(bucketName, objectName);
  // You can't change an object's encryption key directly, the only way is to overwrite the object
  Storage.CopyRequest request =
      Storage.CopyRequest.newBuilder()
          .setSource(blobId)
          .setSourceOptions(Storage.BlobSourceOption.decryptionKey(oldEncryptionKey))
          .setTarget(blobId, Storage.BlobTargetOption.encryptionKey(newEncryptionKey))
          .build();
  storage.copy(request);

  System.out.println(
      "Rotated encryption key for object " + objectName + "in bucket " + bucketName);
}
 
Example #28
Source File: UseExplicitCredentials.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
  // Create a file system for the bucket using the service account credentials
  // saved in the file below.
  String myCredentials = "/path/to/my/key.json";
  CloudStorageFileSystem fs =
      CloudStorageFileSystem.forBucket(
          "mybucket",
          CloudStorageConfiguration.DEFAULT,
          StorageOptions.newBuilder()
              .setCredentials(
                  ServiceAccountCredentials.fromStream(new FileInputStream(myCredentials)))
              .build());
  // Can now read and write to the bucket using fs
  // (see e.g. ReadAllLines for an example).
}
 
Example #29
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
          + "'");
}
 
Example #30
Source File: AsyncBatchAnnotateImagesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() {
  System.setOut(null);

  Storage storage = StorageOptions.getDefaultInstance().getService();

  Page<Blob> blobs =
      storage.list(
          PROJECT_ID,
          Storage.BlobListOption.currentDirectory(),
          Storage.BlobListOption.prefix(PREFIX));
  for (Blob blob : blobs.iterateAll()) {
    blob.delete();
  }
}