/** Example of replacing blob's metadata. */ // [TARGET update(BlobInfo)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public Blob updateBlob(String bucketName, String blobName) { // [START updateBlob] Map<String, String> newMetadata = new HashMap<>(); newMetadata.put("key", "value"); storage.update(BlobInfo.newBuilder(bucketName, blobName).setMetadata(null).build()); Blob blob = storage.update(BlobInfo.newBuilder(bucketName, blobName).setMetadata(newMetadata).build()); // [END updateBlob] return blob; }
/** 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; }
/** 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 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 updating information on several blobs using a single batch request. */ // [TARGET update(Iterable)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] // [VARIABLE "my_blob_name2"] public List<Blob> batchUpdateIterable(String bucketName, String blobName1, String blobName2) { // [START batchUpdateIterable] Blob firstBlob = storage.get(bucketName, blobName1); Blob secondBlob = storage.get(bucketName, blobName2); List<BlobInfo> blobs = new LinkedList<>(); blobs.add(firstBlob.toBuilder().setContentType("text/plain").build()); blobs.add(secondBlob.toBuilder().setContentType("text/plain").build()); List<Blob> updatedBlobs = storage.update(blobs); // [END batchUpdateIterable] return updatedBlobs; }
/** 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 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 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 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 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; }
/** 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; } }
@Test public void testUpdate() throws Exception { initializeExpectedBucket(5); Bucket expectedUpdatedBucket = expectedBucket.toBuilder().setNotFoundPage("p").build(); expect(storage.getOptions()).andReturn(mockOptions).times(2); expect(storage.update(expectedUpdatedBucket)).andReturn(expectedUpdatedBucket); replay(storage); initializeBucket(); Bucket updatedBucket = new Bucket(storage, new BucketInfo.BuilderImpl(expectedUpdatedBucket)); Bucket actualUpdatedBucket = updatedBucket.update(); assertEquals(expectedUpdatedBucket, actualUpdatedBucket); }
@Test public void testUpdate() throws Exception { initializeExpectedBlob(2); Blob expectedUpdatedBlob = expectedBlob.toBuilder().setCacheControl("c").build(); expect(storage.getOptions()).andReturn(mockOptions).times(2); expect(storage.update(eq(expectedUpdatedBlob), new Storage.BlobTargetOption[0])) .andReturn(expectedUpdatedBlob); replay(storage); initializeBlob(); Blob updatedBlob = new Blob(storage, new BlobInfo.BuilderImpl(expectedUpdatedBlob)); Blob actualUpdatedBlob = updatedBlob.update(); assertEquals(expectedUpdatedBlob, actualUpdatedBlob); }
@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 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 testUpdateBlob() { BlobInfo updatedBlobInfo = BLOB_INFO1.toBuilder().setContentType("some-content-type").build(); EasyMock.expect(storageRpcMock.patch(updatedBlobInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(updatedBlobInfo.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.update(updatedBlobInfo); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(updatedBlobInfo)), blob); }
@Test public void testUpdateBlobWithOptions() { BlobInfo updatedBlobInfo = BLOB_INFO1.toBuilder().setContentType("some-content-type").build(); EasyMock.expect(storageRpcMock.patch(updatedBlobInfo.toPb(), BLOB_TARGET_OPTIONS_UPDATE)) .andReturn(updatedBlobInfo.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.update(updatedBlobInfo, BLOB_TARGET_METAGENERATION, BLOB_TARGET_PREDEFINED_ACL); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(updatedBlobInfo)), blob); }
@Test public void testUpdateBucketLabel() { Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID)); assertNull(remoteBucket.getLabels()); remoteBucket = remoteBucket.toBuilder().setLabels(BUCKET_LABELS).build(); Bucket updatedBucket = storage.update(remoteBucket); assertEquals(BUCKET_LABELS, updatedBucket.getLabels()); }
@Test public void testUpdateBlobFail() { String blobName = "test-update-blob-fail"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); BlobInfo wrongGenerationBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).setContentType(CONTENT_TYPE).build(); try { storage.update(wrongGenerationBlob, Storage.BlobTargetOption.generationMatch()); fail("StorageException was expected"); } catch (StorageException ex) { // expected } }
@Test public void testUpdateBucketRequesterPays() { Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID)); assertNull(remoteBucket.requesterPays()); remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build(); Bucket updatedBucket = storage.update(remoteBucket); assertTrue(updatedBucket.requesterPays()); String projectId = remoteStorageHelper.getOptions().getProjectId(); Bucket.BlobTargetOption option = Bucket.BlobTargetOption.userProject(projectId); String blobName = "test-create-empty-blob-requester-pays"; Blob remoteBlob = updatedBucket.create(blobName, BLOB_BYTE_CONTENT, option); assertNotNull(remoteBlob); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); }