/** 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 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; }
@Test public void testReload() throws Exception { initializeExpectedBucket(5); BucketInfo updatedInfo = BUCKET_INFO.toBuilder().setNotFoundPage("p").build(); Bucket expectedUpdatedBucket = new Bucket(serviceMockReturnsOptions, new BucketInfo.BuilderImpl(updatedInfo)); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(updatedInfo.getName())).andReturn(expectedUpdatedBucket); replay(storage); initializeBucket(); Bucket updatedBucket = bucket.reload(); assertEquals(expectedUpdatedBucket, updatedBucket); }
@Test public void testAttemptObjectDeleteWithRetentionPolicy() throws ExecutionException, InterruptedException { String bucketName = RemoteStorageHelper.generateBucketName(); Bucket remoteBucket = storage.create( BucketInfo.newBuilder(bucketName).setRetentionPeriod(RETENTION_PERIOD).build()); assertEquals(RETENTION_PERIOD, remoteBucket.getRetentionPeriod()); String blobName = "test-create-with-retention-policy"; BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build(); Blob remoteBlob = storage.create(blobInfo); assertNotNull(remoteBlob.getRetentionExpirationTime()); try { remoteBlob.delete(); fail("Expected failure on delete from retentionPolicy"); } catch (StorageException ex) { // expected } finally { Thread.sleep(RETENTION_PERIOD_IN_MILLISECONDS); RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS); } }
/** 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; }
@Test public void testUpdateBucketDefaultKmsKeyName() throws ExecutionException, InterruptedException { String bucketName = RemoteStorageHelper.generateBucketName(); Bucket remoteBucket = storage.create( BucketInfo.newBuilder(bucketName) .setDefaultKmsKeyName(kmsKeyOneResourcePath) .setLocation(KMS_KEY_RING_LOCATION) .build()); try { assertEquals(kmsKeyOneResourcePath, remoteBucket.getDefaultKmsKeyName()); Bucket updatedBucket = remoteBucket.toBuilder().setDefaultKmsKeyName(kmsKeyTwoResourcePath).build().update(); assertEquals(kmsKeyTwoResourcePath, updatedBucket.getDefaultKmsKeyName()); } finally { RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS); } }
/** 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; }
@BeforeClass public static void beforeClass() throws IOException { // loads the credentials from local disk as par README RemoteStorageHelper gcsHelper = RemoteStorageHelper.create(); storageOptions = gcsHelper.getOptions(); project = storageOptions.getProjectId(); storage = storageOptions.getService(); // create and populate test bucket storage.create(BucketInfo.of(BUCKET)); fillFile(storage, BUCKET, SML_FILE, SML_SIZE); fillFile(storage, BUCKET, BIG_FILE, BIG_SIZE); BucketInfo requesterPaysBucket = BucketInfo.newBuilder(REQUESTER_PAYS_BUCKET).setRequesterPays(true).build(); storage.create(requesterPaysBucket); fillRequesterPaysFile(storage, SML_FILE, SML_SIZE); }
/** Example of creating a bucket with storage class and location. */ // [TARGET create(BucketInfo, BucketTargetOption...)] // [VARIABLE "my_unique_bucket"] public Bucket createBucketWithStorageClassAndLocation(String bucketName) { // [START createBucketWithStorageClassAndLocation] Bucket bucket = storage.create( BucketInfo.newBuilder(bucketName) // See here for possible values: http://g.co/cloud/storage/docs/storage-classes .setStorageClass(StorageClass.COLDLINE) // Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr .setLocation("asia") .build()); // [END createBucketWithStorageClassAndLocation] return bucket; }
@BeforeClass public static void beforeClass() throws IOException { remoteStorageHelper = RemoteStorageHelper.create(); storage = remoteStorageHelper.getOptions().getService(); storage.create( BucketInfo.newBuilder(BUCKET) .setLocation("us") .setLifecycleRules( ImmutableList.of( new LifecycleRule( LifecycleAction.newDeleteAction(), LifecycleCondition.newBuilder().setAge(1).build()))) .build()); // Prepare KMS KeyRing for CMEK tests prepareKmsKeys(); }
@Test public void testClearBucketDefaultKmsKeyName() throws ExecutionException, InterruptedException { String bucketName = RemoteStorageHelper.generateBucketName(); Bucket remoteBucket = storage.create( BucketInfo.newBuilder(bucketName) .setDefaultKmsKeyName(kmsKeyOneResourcePath) .setLocation(KMS_KEY_RING_LOCATION) .build()); try { assertEquals(kmsKeyOneResourcePath, remoteBucket.getDefaultKmsKeyName()); Bucket updatedBucket = remoteBucket.toBuilder().setDefaultKmsKeyName(null).build().update(); assertNull(updatedBucket.getDefaultKmsKeyName()); } finally { RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS); } }
/** Example of disabling Requester pays on a bucket. */ public Bucket disableRequesterPays(String bucketName) { // [START disable_requester_pays] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of the bucket to disable requester-paying for, e.g. "my-bucket" // String bucketName = "my-bucket" BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setRequesterPays(false).build(); // Update the bucket, throws StorageException on failure Bucket bucket = storage.update(bucketInfo); System.out.println("Requester pays status for " + bucketName + ": " + bucket.requesterPays()); // [END disable_requester_pays] return bucket; }
/** Example of enabling Requester pays on a bucket. */ public Bucket enableRequesterPays(String bucketName) throws StorageException { // [START enable_requester_pays] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of the existing bucket to enable requester-paying for, e.g. "my-bucket" // String bucketName = "my-bucket" BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setRequesterPays(true).build(); // Update the bucket, throws StorageException on failure Bucket bucket = storage.update(bucketInfo); System.out.println("Requester pay status for " + bucketName + ": " + bucket.requesterPays()); // [END enable_requester_pays] return bucket; }
@Test public void testReloadWithOptions() throws Exception { initializeExpectedBucket(5); BucketInfo updatedInfo = BUCKET_INFO.toBuilder().setNotFoundPage("p").build(); Bucket expectedUpdatedBucket = new Bucket(serviceMockReturnsOptions, new BucketInfo.BuilderImpl(updatedInfo)); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(updatedInfo.getName(), Storage.BucketGetOption.metagenerationMatch(42L))) .andReturn(expectedUpdatedBucket); replay(storage); initializeBucket(); Bucket updatedBucket = bucket.reload(Bucket.BucketSourceOption.metagenerationMatch()); assertEquals(expectedUpdatedBucket, updatedBucket); }
/** Creates a {@code BucketInfo} object for the provided bucket name. */ public static BucketInfo of(String name) { return newBuilder(name).build(); }
@Test public void testUpdateBucket() { BucketInfo updatedBucketInfo = BUCKET_INFO1.toBuilder().setIndexPage("some-page").build(); EasyMock.expect(storageRpcMock.patch(updatedBucketInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(updatedBucketInfo.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.update(updatedBucketInfo); assertEquals(new Bucket(storage, new BucketInfo.BuilderImpl(updatedBucketInfo)), bucket); }
@Test public void testToBuilder() { compareBuckets(BUCKET_INFO, BUCKET_INFO.toBuilder().build()); BucketInfo bucketInfo = BUCKET_INFO.toBuilder().setName("B").setGeneratedId("id").build(); assertEquals("B", bucketInfo.getName()); assertEquals("id", bucketInfo.getGeneratedId()); bucketInfo = bucketInfo.toBuilder().setName("b").setGeneratedId(GENERATED_ID).build(); compareBuckets(BUCKET_INFO, bucketInfo); }
@Test public void testUpdateBucketWithOptions() { BucketInfo updatedBucketInfo = BUCKET_INFO1.toBuilder().setIndexPage("some-page").build(); EasyMock.expect(storageRpcMock.patch(updatedBucketInfo.toPb(), BUCKET_TARGET_OPTIONS)) .andReturn(updatedBucketInfo.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.update( updatedBucketInfo, BUCKET_TARGET_METAGENERATION, BUCKET_TARGET_PREDEFINED_ACL); assertEquals(new Bucket(storage, new BucketInfo.BuilderImpl(updatedBucketInfo)), bucket); }
/** Example of updating bucket information. */ // [TARGET update(BucketInfo, BucketTargetOption...)] // [VARIABLE "my_unique_bucket"] public Bucket updateBucket(String bucketName) { // [START updateBucket] BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setVersioningEnabled(true).build(); Bucket bucket = storage.update(bucketInfo); // [END updateBucket] return bucket; }