com.google.cloud.storage.Bucket Java Examples

The following examples show how to use com.google.cloud.storage.Bucket. 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: 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 #2
Source File: EnableLifecycleManagement.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void enableLifecycleManagement(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";

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

  // See the LifecycleRule documentation for additional info on what you can do with lifecycle
  // management rules. This one deletes objects that are over 100 days old.
  // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/BucketInfo.LifecycleRule.html
  bucket
      .toBuilder()
      .setLifecycleRules(
          ImmutableList.of(
              new LifecycleRule(
                  LifecycleRule.LifecycleAction.newDeleteAction(),
                  LifecycleRule.LifecycleCondition.newBuilder().setAge(100).build())))
      .build()
      .update();

  System.out.println("Lifecycle management was enabled and configured for bucket " + bucketName);
}
 
Example #3
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 #4
Source File: GoogleStorageTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpecifyBucketCorrect() {

	Bucket mockedBucket = mock(Bucket.class);
	when(this.mockStorage.get("test-spring")).thenReturn(mockedBucket);
	when(mockedBucket.exists()).thenReturn(true);
	when(mockedBucket.getName()).thenReturn("test-spring");

	GoogleStorageLocation location = GoogleStorageLocation.forBucket("test-spring");
	GoogleStorageResource googleStorageResource = new GoogleStorageResource(
			this.mockStorage, location, false);

	Assert.assertTrue(googleStorageResource.isBucket());
	Assert.assertEquals("test-spring", googleStorageResource.getBucketName());
	Assert.assertEquals("test-spring", googleStorageResource.getBucket().getName());
	Assert.assertTrue(googleStorageResource.exists());
}
 
Example #5
Source File: ITBucketSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequesterPays() throws Exception {
  EnableRequesterPays.enableRequesterPays(PROJECT_ID, BUCKET);
  Bucket bucket = storage.get(BUCKET);
  assertTrue(bucket.requesterPays());
  String projectId = ServiceOptions.getDefaultProjectId();
  String blobName = "test-create-empty-blob-requester-pays";
  byte[] content = {0xD, 0xE, 0xA, 0xD};
  Blob remoteBlob =
      bucket.create(blobName, content, Bucket.BlobTargetOption.userProject(projectId));
  assertNotNull(remoteBlob);
  DownloadRequesterPaysObject.downloadRequesterPaysObject(
      projectId, BUCKET, blobName, Paths.get(blobName));
  byte[] readBytes = Files.readAllBytes(Paths.get(blobName));
  assertArrayEquals(content, readBytes);
  DisableRequesterPays.disableRequesterPays(PROJECT_ID, BUCKET);
  assertFalse(storage.get(BUCKET).requesterPays());
}
 
Example #6
Source File: FirebaseStorageSnippets.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
public void initializeAppForStorage() throws IOException {
  // [START init_admin_sdk_for_storage]
  FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");

  FirebaseOptions options = new FirebaseOptions.Builder()
      .setCredentials(GoogleCredentials.fromStream(serviceAccount))
      .setStorageBucket("<BUCKET_NAME>.appspot.com")
      .build();
  FirebaseApp.initializeApp(options);

  Bucket bucket = StorageClient.getInstance().bucket();

  // 'bucket' is an object defined in the google-cloud-storage Java library.
  // See http://googlecloudplatform.github.io/google-cloud-java/latest/apidocs/com/google/cloud/storage/Bucket.html
  // for more details.
  // [END init_admin_sdk_for_storage]
  System.out.println("Retrieved bucket: " + bucket.getName());
}
 
Example #7
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of setting a retention policy on a bucket */
public Bucket setRetentionPolicy(String bucketName, Long retentionPeriod)
    throws StorageException {
  // [START storage_set_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";

  // The retention period for objects in bucket
  // Long retentionPeriod = 3600L; // 1 hour in seconds

  Bucket bucketWithRetentionPolicy =
      storage.update(
          BucketInfo.newBuilder(bucketName).setRetentionPeriod(retentionPeriod).build());

  System.out.println(
      "Retention period for "
          + bucketName
          + " is now "
          + bucketWithRetentionPolicy.getRetentionPeriod());
  // [END storage_set_retention_policy]
  return bucketWithRetentionPolicy;
}
 
Example #8
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 #9
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 #10
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 #11
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 #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 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 #13
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 #14
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to disable uniform bucket-level access for a bucket */
public Bucket disableUniformBucketLevelAccess(String bucketName) throws StorageException {
  // [START storage_disable_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(false)
          .build();
  Bucket bucket =
      storage.update(
          BucketInfo.newBuilder(bucketName).setIamConfiguration(iamConfiguration).build());

  System.out.println("Uniform bucket-level access was disabled for " + bucketName);
  // [END storage_disable_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 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 #16
Source File: StorageExample.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the ACL according to the provided {@code params}, using the {@code storage} service. If
 * {@code params.x()} returns a complete blob identity, the {@code params.y()} ACL is added to
 * the blob. If {@code params.x().name()} is empty, the {@code params.y()} ACL is added to the
 * bucket identified by {@code params.x().bucket()}.
 */
@Override
public void run(Storage storage, Tuple<BlobId, Acl> params) {
  BlobId blobId = params.x();
  Acl acl = params.y();
  if (blobId.getName().isEmpty()) {
    Bucket bucket = storage.get(blobId.getBucket());
    if (bucket == null) {
      System.out.printf("Bucket %s does not exist%n", blobId.getBucket());
      return;
    }
    acl = bucket.createAcl(acl);
    System.out.printf("Added ACL %s to bucket %s%n", acl, blobId.getBucket());
  } else {
    Blob blob = storage.get(blobId);
    if (blob == null) {
      System.out.printf("Blob %s does not exist%n", blobId);
      return;
    }
    acl = blob.createAcl(acl);
    System.out.printf("Added ACL %s to blob %s%n", acl, blobId);
  }
}
 
Example #17
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 #18
Source File: ITStorageSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testLockRetentionPolicy() {
  String tempBucket = RemoteStorageHelper.generateBucketName();
  Bucket bucket = storageSnippets.createBucket(tempBucket);
  assertNotNull(bucket);
  bucket = storageSnippets.setRetentionPolicy(tempBucket, RETENTION_PERIOD);
  assertEquals(bucket.getRetentionPeriod(), RETENTION_PERIOD);
  bucket = storageSnippets.lockRetentionPolicy(tempBucket);
  assertTrue(bucket.retentionPolicyIsLocked());
}
 
Example #19
Source File: ITBucketSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testChangeDefaultStorageClass() {
  Bucket remoteBucket = storage.get(BUCKET);
  assertEquals("STANDARD", remoteBucket.getStorageClass().name());
  ChangeDefaultStorageClass.changeDefaultStorageClass(PROJECT_ID, BUCKET);
  remoteBucket = storage.get(BUCKET);
  assertEquals("COLDLINE", remoteBucket.getStorageClass().name());
}
 
Example #20
Source File: RemoveBucketDefaultKMSKey.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void removeBucketDefaultKmsKey(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";

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

  System.out.println("Default KMS key was removed from " + bucketName);
}
 
Example #21
Source File: CreateAndListBucketsAndBlobs.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) {
  // Create a service object
  // Credentials are inferred from the environment.
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // Create a bucket
  String bucketName = "my_unique_bucket"; // Change this to something unique
  Bucket bucket = storage.create(BucketInfo.of(bucketName));

  // Upload a blob to the newly created bucket
  Blob blob = bucket.create("my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain");

  // Read the blob content from the server
  String blobContent = new String(blob.getContent(), UTF_8);

  // List all your buckets
  System.out.println("My buckets:");
  for (Bucket currentBucket : storage.list().iterateAll()) {
    System.out.println(currentBucket);
  }

  // List the blobs in a particular bucket
  System.out.println("My blobs:");
  for (Blob currentBlob : bucket.list().iterateAll()) {
    System.out.println(currentBlob);
  }
}
 
Example #22
Source File: SetBucketWebsiteInfo.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void setBucketWesbiteInfo(
    String projectId, String bucketName, String indexPage, String notFoundPage) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

  // The ID of your static website bucket
  // String bucketName = "www.example.com";

  // The index page for a static website bucket
  // String indexPage = "index.html";

  // The 404 page for a static website bucket
  // String notFoundPage = "404.html";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  Bucket bucket = storage.get(bucketName);
  bucket.toBuilder().setIndexPage(indexPage).setNotFoundPage(notFoundPage).build().update();

  System.out.println(
      "Static website bucket "
          + bucketName
          + " is set up to use "
          + indexPage
          + " as the index page and "
          + notFoundPage
          + " as the 404 page");
}
 
Example #23
Source File: ITStorageSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testUniformBucketLevelAccess() {
  String tempBucket = RemoteStorageHelper.generateBucketName();
  Bucket bucket = storageSnippets.createBucket(tempBucket);
  assertNotNull(bucket);
  bucket = storageSnippets.enableUniformBucketLevelAccess(tempBucket);
  assertTrue(bucket.getIamConfiguration().isUniformBucketLevelAccessEnabled());
  assertNotNull(bucket.getIamConfiguration().getUniformBucketLevelAccessLockedTime());
  bucket = storageSnippets.getUniformBucketLevelAccess(tempBucket);
  assertTrue(bucket.getIamConfiguration().isUniformBucketLevelAccessEnabled());
  assertNotNull(bucket.getIamConfiguration().getUniformBucketLevelAccessLockedTime());
  bucket = storageSnippets.disableUniformBucketLevelAccess(tempBucket);
  assertFalse(bucket.getIamConfiguration().isUniformBucketLevelAccessEnabled());
}
 
Example #24
Source File: EnableBucketVersioning.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void enableBucketVersioning(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";

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

  System.out.println("Versioning is now enabled for bucket " + bucketName);
}
 
Example #25
Source File: DeleteBucket.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void deleteBucket(String projectId, String bucketName) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

  // The ID of the bucket to delete
  // String bucketName = "your-unique-bucket-name";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  Bucket bucket = storage.get(bucketName);
  bucket.delete();

  System.out.println("Bucket " + bucket.getName() + " was deleted");
}
 
Example #26
Source File: DisableBucketVersioning.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void disableBucketVersioning(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";

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

  System.out.println("Versioning is now disabled for bucket " + bucketName);
}
 
Example #27
Source File: DisableRequesterPays.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void disableRequesterPays(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";

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

  System.out.println("Requester pays disabled for bucket " + bucketName);
}
 
Example #28
Source File: EnableRequesterPays.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void enableRequesterPays(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";

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

  System.out.println("Requester pays enabled for bucket " + bucketName);
}
 
Example #29
Source File: ListBuckets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void listBuckets(String projectId) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  Page<Bucket> buckets = storage.list();

  for (Bucket bucket : buckets.iterateAll()) {
    System.out.println(bucket.getName());
  }
}
 
Example #30
Source File: AddBucketLabel.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void addBucketLabel(
    String projectId, String bucketName, String labelKey, String labelValue) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

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

  // The key of the label to add
  // String labelKey = "label-key-to-add";

  // The value of the label to add
  // String labelValue = "label-value-to-add";

  Map<String, String> labelsToAdd = new HashMap<>();
  labelsToAdd.put(labelKey, labelValue);

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  Bucket bucket = storage.get(bucketName);
  Map<String, String> labels = bucket.getLabels();
  if (labels == null) {
    labels = labelsToAdd;
  } else {
    labels.putAll(labelsToAdd);
  }
  bucket.toBuilder().setLabels(labels).build().update();

  System.out.println(
      "Added label " + labelKey + " with value " + labelValue + " to bucket " + bucketName + ".");
}