/** Example of copying the blob to a different bucket, keeping the original name. */ // [TARGET copyTo(String, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] public Blob copyToBucket(String bucketName) { // [START copyToBucket] CopyWriter copyWriter = blob.copyTo(bucketName); Blob copiedBlob = copyWriter.getResult(); // [END copyToBucket] return copiedBlob; }
/** Example of copying the blob to a different bucket with a different name. */ // [TARGET copyTo(String, String, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "copy_blob_name"] public Blob copyToStrings(String bucketName, String blobName) { // [START copyToStrings] CopyWriter copyWriter = blob.copyTo(bucketName, blobName); Blob copiedBlob = copyWriter.getResult(); // [END copyToStrings] return copiedBlob; }
/** * Sends a copy request for the current blob to the target bucket, preserving its name. Possibly * copying also some of the metadata (e.g. content-type). * * <p>Example of copying the blob to a different bucket, keeping the original name. * * <pre>{@code * String bucketName = "my_unique_bucket"; * CopyWriter copyWriter = blob.copyTo(bucketName); * Blob copiedBlob = copyWriter.getResult(); * }</pre> * * @param targetBucket target bucket's name * @param options source blob options * @return a {@link CopyWriter} object that can be used to get information on the newly created * blob or to complete the copy if more than one RPC request is needed * @throws StorageException upon failure */ public CopyWriter copyTo(String targetBucket, BlobSourceOption... options) { return copyTo(targetBucket, getName(), options); }
return copyTo(BlobId.of(targetBucket, targetBlob), options);
/** Example of copying the blob to a different bucket with a different name. */ // [TARGET copyTo(BlobId, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "copy_blob_name"] public Blob copyToId(String bucketName, String blobName) { // [START copyToId] CopyWriter copyWriter = blob.copyTo(BlobId.of(bucketName, blobName)); Blob copiedBlob = copyWriter.getResult(); // [END copyToId] return copiedBlob; }
/** Example of moving a blob to a different bucket with a different name. */ // [TARGET copyTo(String, String, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "move_blob_name"] public Blob moveTo(String destBucket, String destBlob) { // [START storageMoveFile] CopyWriter copyWriter = blob.copyTo(destBucket, destBlob); Blob copiedBlob = copyWriter.getResult(); boolean deleted = blob.delete(); // [END storageMoveFile] if (deleted) { return copiedBlob; } else { return null; } }
@Test public void testCopyToBucket() throws Exception { initializeExpectedBlob(2); BlobInfo target = BlobInfo.newBuilder(BlobId.of("bt", "n")).build(); CopyWriter copyWriter = createMock(CopyWriter.class); Capture<CopyRequest> capturedCopyRequest = Capture.newInstance(); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter); replay(storage); initializeBlob(); CopyWriter returnedCopyWriter = blob.copyTo("bt"); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().getSource(), blob.getBlobId()); assertEquals(capturedCopyRequest.getValue().getTarget(), target); assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty()); }
@Test public void testCopyTo() throws Exception { initializeExpectedBlob(2); BlobInfo target = BlobInfo.newBuilder(BlobId.of("bt", "nt")).build(); CopyWriter copyWriter = createMock(CopyWriter.class); Capture<CopyRequest> capturedCopyRequest = Capture.newInstance(); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter); replay(storage); initializeBlob(); CopyWriter returnedCopyWriter = blob.copyTo("bt", "nt"); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().getSource(), blob.getBlobId()); assertEquals(capturedCopyRequest.getValue().getTarget(), target); assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty()); }
@Test public void testCopyToBlobId() throws Exception { initializeExpectedBlob(2); BlobInfo target = BlobInfo.newBuilder(BlobId.of("bt", "nt")).build(); BlobId targetId = BlobId.of("bt", "nt"); CopyWriter copyWriter = createMock(CopyWriter.class); Capture<CopyRequest> capturedCopyRequest = Capture.newInstance(); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter); replay(storage); initializeBlob(); CopyWriter returnedCopyWriter = blob.copyTo(targetId); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().getSource(), blob.getBlobId()); assertEquals(capturedCopyRequest.getValue().getTarget(), target); assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty()); }
/** * Sends a copy request for the current blob to the target bucket, preserving its name. Possibly * copying also some of the metadata (e.g. content-type). * * <p>Example of copying the blob to a different bucket, keeping the original name. * * <pre>{@code * String bucketName = "my_unique_bucket"; * CopyWriter copyWriter = blob.copyTo(bucketName); * Blob copiedBlob = copyWriter.getResult(); * }</pre> * * @param targetBucket target bucket's name * @param options source blob options * @return a {@link CopyWriter} object that can be used to get information on the newly created * blob or to complete the copy if more than one RPC request is needed * @throws StorageException upon failure */ public CopyWriter copyTo(String targetBucket, BlobSourceOption... options) { return copyTo(targetBucket, getName(), options); }
return copyTo(BlobId.of(targetBucket, targetBlob), options);
@Override public boolean rename(String fromPath, String toPath) { final BlobId fromBlob = GcsBlob.of(fromPath).getBlobId(); final BlobId toBlob = GcsBlob.of(toPath).getBlobId(); storageProvider.getStorage().get(fromBlob).copyTo(toBlob); keyCache.invalidate(fromBlob.getBucket()); keyCache.invalidate(toBlob.getBucket()); return storageProvider.getStorage().delete(fromBlob); }