/** 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; }
@Override public boolean delete(String bucket, BucketSourceOption... options) { final com.google.api.services.storage.model.Bucket bucketPb = BucketInfo.of(bucket).toPb(); final Map<StorageRpc.Option, ?> optionsMap = optionMap(options); try { return runWithRetries( new Callable<Boolean>() { @Override public Boolean call() { return storageRpc.delete(bucketPb, optionsMap); } }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } }
@Test public void testGetBucketWithEmptyFields() { Capture<Map<StorageRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( storageRpcMock.get( EasyMock.eq(BucketInfo.of(BUCKET_NAME1).toPb()), EasyMock.capture(capturedOptions))) .andReturn(BUCKET_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.get(BUCKET_NAME1, BUCKET_GET_METAGENERATION, BUCKET_GET_EMPTY_FIELDS); assertEquals( BUCKET_GET_METAGENERATION.getValue(), capturedOptions.getValue().get(BUCKET_GET_METAGENERATION.getRpcOption())); String selector = (String) capturedOptions.getValue().get(BLOB_GET_FIELDS.getRpcOption()); assertTrue(selector.contains("name")); assertEquals(4, selector.length()); assertEquals(BUCKET_INFO1.getName(), bucket.getName()); }
@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 testExists_True() throws Exception { initializeExpectedBucket(4); Storage.BucketGetOption[] expectedOptions = {Storage.BucketGetOption.fields()}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BUCKET_INFO.getName(), expectedOptions)).andReturn(expectedBucket); replay(storage); initializeBucket(); assertTrue(bucket.exists()); }
@Test public void testCreateBucketWithOptions() { EasyMock.expect(storageRpcMock.create(BUCKET_INFO1.toPb(), BUCKET_TARGET_OPTIONS)) .andReturn(BUCKET_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.create(BUCKET_INFO1, BUCKET_TARGET_METAGENERATION, BUCKET_TARGET_PREDEFINED_ACL); assertEquals(expectedBucket1, bucket); }
@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); }
@Test public void testListDefaultAcls() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.listDefaultAcls(BUCKET_INFO.getName())).andReturn(ACLS); replay(storage); initializeBucket(); assertEquals(ACLS, bucket.listDefaultAcls()); }
@Test public void testGetAcl() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.getAcl(BUCKET_INFO.getName(), User.ofAllAuthenticatedUsers())).andReturn(ACL); replay(storage); initializeBucket(); assertEquals(ACL, bucket.getAcl(User.ofAllAuthenticatedUsers())); }
@Test public void testDeleteAcl() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.deleteAcl(BUCKET_INFO.getName(), User.ofAllAuthenticatedUsers())) .andReturn(true); replay(storage); initializeBucket(); assertTrue(bucket.deleteAcl(User.ofAllAuthenticatedUsers())); }
@Test public void testLockRetentionPolicy() { EasyMock.expect( storageRpcMock.lockRetentionPolicy( BUCKET_INFO3.toPb(), BUCKET_TARGET_OPTIONS_LOCK_RETENTION_POLICY)) .andReturn(BUCKET_INFO3.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.lockRetentionPolicy( BUCKET_INFO3, BUCKET_TARGET_METAGENERATION, BUCKET_TARGET_USER_PROJECT); assertEquals(expectedBucket3, bucket); }
@Test public void testDeleteDefaultAcl() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.deleteDefaultAcl(BUCKET_INFO.getName(), User.ofAllAuthenticatedUsers())) .andReturn(true); replay(storage); initializeBucket(); assertTrue(bucket.deleteDefaultAcl(User.ofAllAuthenticatedUsers())); }
@Test public void testGetDefaultAcl() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.getDefaultAcl(BUCKET_INFO.getName(), User.ofAllAuthenticatedUsers())) .andReturn(ACL); replay(storage); initializeBucket(); assertEquals(ACL, bucket.getDefaultAcl(User.ofAllAuthenticatedUsers())); }