use of com.google.cloud.storage.CopyWriter in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method copyBlobInChunks.
/**
* Example of copying a blob in chunks.
*/
// [TARGET copy(CopyRequest)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "copy_blob_name"]
public Blob copyBlobInChunks(String bucketName, String blobName, String copyBlobName) {
// [START copyBlobInChunks]
CopyRequest request = CopyRequest.newBuilder().setSource(BlobId.of(bucketName, blobName)).setTarget(BlobId.of(bucketName, copyBlobName)).build();
CopyWriter copyWriter = storage.copy(request);
while (!copyWriter.isDone()) {
copyWriter.copyChunk();
}
Blob blob = copyWriter.getResult();
// [END copyBlobInChunks]
return blob;
}
use of com.google.cloud.storage.CopyWriter in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testCopyBlobNoContentType.
@Test
public void testCopyBlobNoContentType() {
String sourceBlobName = "test-copy-blob-no-content-type-source";
BlobId source = BlobId.of(BUCKET, sourceBlobName);
Blob remoteSourceBlob = storage.create(BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT);
assertNotNull(remoteSourceBlob);
String targetBlobName = "test-copy-blob-no-content-type-target";
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
BlobInfo target = BlobInfo.newBuilder(BUCKET, targetBlobName).setMetadata(metadata).build();
Storage.CopyRequest req = Storage.CopyRequest.of(source, target);
CopyWriter copyWriter = storage.copy(req);
assertEquals(BUCKET, copyWriter.getResult().getBucket());
assertEquals(targetBlobName, copyWriter.getResult().getName());
assertNull(copyWriter.getResult().getContentType());
assertEquals(metadata, copyWriter.getResult().getMetadata());
assertTrue(copyWriter.isDone());
assertTrue(remoteSourceBlob.delete());
assertTrue(storage.delete(BUCKET, targetBlobName));
}
use of com.google.cloud.storage.CopyWriter in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testCopyBlobUpdateStorageClass.
@Test
public void testCopyBlobUpdateStorageClass() {
String sourceBlobName = "test-copy-blob-update-storage-class-source";
BlobId source = BlobId.of(BUCKET, sourceBlobName);
BlobInfo sourceInfo = BlobInfo.newBuilder(source).setStorageClass(StorageClass.STANDARD).build();
Blob remoteSourceBlob = storage.create(sourceInfo, BLOB_BYTE_CONTENT);
assertNotNull(remoteSourceBlob);
assertEquals(StorageClass.STANDARD, remoteSourceBlob.getStorageClass());
String targetBlobName = "test-copy-blob-update-storage-class-target";
BlobInfo targetInfo = BlobInfo.newBuilder(BUCKET, targetBlobName).setStorageClass(StorageClass.COLDLINE).build();
Storage.CopyRequest req = Storage.CopyRequest.of(source, targetInfo);
CopyWriter copyWriter = storage.copy(req);
assertEquals(BUCKET, copyWriter.getResult().getBucket());
assertEquals(targetBlobName, copyWriter.getResult().getName());
assertEquals(StorageClass.COLDLINE, copyWriter.getResult().getStorageClass());
assertTrue(copyWriter.isDone());
assertTrue(remoteSourceBlob.delete());
assertTrue(storage.delete(BUCKET, targetBlobName));
}
use of com.google.cloud.storage.CopyWriter in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testCopyBlobWithEncryptionKeys.
@Test
public void testCopyBlobWithEncryptionKeys() {
String sourceBlobName = "test-copy-blob-encryption-key-source";
BlobId source = BlobId.of(BUCKET, sourceBlobName);
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
Blob remoteBlob = storage.create(BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT, Storage.BlobTargetOption.encryptionKey(KEY));
assertNotNull(remoteBlob);
String targetBlobName = "test-copy-blob-encryption-key-target";
BlobInfo target = BlobInfo.newBuilder(BUCKET, targetBlobName).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
Storage.CopyRequest req = Storage.CopyRequest.newBuilder().setSource(source).setTarget(target, Storage.BlobTargetOption.encryptionKey(OTHER_BASE64_KEY)).setSourceOptions(Storage.BlobSourceOption.decryptionKey(BASE64_KEY)).build();
CopyWriter copyWriter = storage.copy(req);
assertEquals(BUCKET, copyWriter.getResult().getBucket());
assertEquals(targetBlobName, copyWriter.getResult().getName());
assertEquals(CONTENT_TYPE, copyWriter.getResult().getContentType());
assertArrayEquals(BLOB_BYTE_CONTENT, copyWriter.getResult().getContent(Blob.BlobSourceOption.decryptionKey(OTHER_BASE64_KEY)));
assertEquals(metadata, copyWriter.getResult().getMetadata());
assertTrue(copyWriter.isDone());
req = Storage.CopyRequest.newBuilder().setSource(source).setTarget(target).setSourceOptions(Storage.BlobSourceOption.decryptionKey(BASE64_KEY)).build();
copyWriter = storage.copy(req);
assertEquals(BUCKET, copyWriter.getResult().getBucket());
assertEquals(targetBlobName, copyWriter.getResult().getName());
assertEquals(CONTENT_TYPE, copyWriter.getResult().getContentType());
assertArrayEquals(BLOB_BYTE_CONTENT, copyWriter.getResult().getContent());
assertEquals(metadata, copyWriter.getResult().getMetadata());
assertTrue(copyWriter.isDone());
assertTrue(remoteBlob.delete());
assertTrue(storage.delete(BUCKET, targetBlobName));
}
use of com.google.cloud.storage.CopyWriter in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testCopyBlobUpdateMetadata.
@Test
public void testCopyBlobUpdateMetadata() {
String sourceBlobName = "test-copy-blob-update-metadata-source";
BlobId source = BlobId.of(BUCKET, sourceBlobName);
Blob remoteSourceBlob = storage.create(BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT);
assertNotNull(remoteSourceBlob);
String targetBlobName = "test-copy-blob-update-metadata-target";
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
BlobInfo target = BlobInfo.newBuilder(BUCKET, targetBlobName).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
Storage.CopyRequest req = Storage.CopyRequest.of(source, target);
CopyWriter copyWriter = storage.copy(req);
assertEquals(BUCKET, copyWriter.getResult().getBucket());
assertEquals(targetBlobName, copyWriter.getResult().getName());
assertEquals(CONTENT_TYPE, copyWriter.getResult().getContentType());
assertEquals(metadata, copyWriter.getResult().getMetadata());
assertTrue(copyWriter.isDone());
assertTrue(remoteSourceBlob.delete());
assertTrue(storage.delete(BUCKET, targetBlobName));
}
Aggregations