com.google.cloud.storage.CopyWriter Java Examples

The following examples show how to use com.google.cloud.storage.CopyWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** 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;
}
 
Example #2
Source File: GcsMessageHandlerTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewFiles() throws IOException {
	File testFile = this.temporaryFolder.newFile("benfica");

	BlobInfo expectedCreateBlobInfo =
			BlobInfo.newBuilder(BlobId.of("testGcsBucket", "benfica.writing")).build();
	WriteChannel writeChannel = mock(WriteChannel.class);
	willAnswer((invocationOnMock) -> writeChannel).given(GCS)
			.writer(eq(expectedCreateBlobInfo));
	willAnswer((invocationOnMock) -> 10).given(writeChannel).write(isA(ByteBuffer.class));

	CopyWriter copyWriter = mock(CopyWriter.class);
	ArgumentCaptor<Storage.CopyRequest> copyRequestCaptor = ArgumentCaptor.forClass(Storage.CopyRequest.class);
	willAnswer((invocationOnMock) -> copyWriter).given(GCS).copy(isA(Storage.CopyRequest.class));

	willAnswer((invocationOnMock) -> true).given(GCS)
			.delete(eq(BlobId.of("testGcsBucket", "benfica.writing")));

	this.channel.send(new GenericMessage<Object>(testFile));

	verify(GCS, times(1)).writer(eq(expectedCreateBlobInfo));
	verify(GCS, times(1)).copy(copyRequestCaptor.capture());
	verify(GCS, times(1)).delete(eq(BlobId.of("testGcsBucket", "benfica.writing")));

	Storage.CopyRequest expectedCopyRequest = copyRequestCaptor.getValue();
	assertThat(expectedCopyRequest.getSource()).isEqualTo(BlobId.of("testGcsBucket", "benfica.writing"));
	assertThat(expectedCopyRequest.getTarget().getBlobId()).isEqualTo(BlobId.of("testGcsBucket", "benfica"));
}
 
Example #3
Source File: MoveObject.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void moveObject(
    String projectId, String sourceBucketName, String objectName, String targetBucketName) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

  // The ID of your GCS bucket
  // String bucketName = "your-unique-bucket-name";

  // The ID of your GCS object
  // String objectName = "your-object-name";

  // The ID of the bucket to move the object objectName to
  // String targetBucketName = "target-object-bucket"

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  Blob blob = storage.get(sourceBucketName, objectName);
  // Write a copy of the object to the target bucket
  CopyWriter copyWriter = blob.copyTo(targetBucketName, objectName);
  Blob copiedBlob = copyWriter.getResult();
  // Delete the original blob now that we've copied to where we want it, finishing the "move"
  // operation
  blob.delete();

  System.out.println(
      "Moved object "
          + objectName
          + " from bucket "
          + sourceBucketName
          + " to "
          + copiedBlob.getBucket());
}
 
Example #4
Source File: GcsPinotFS.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
private boolean copyFile(URI srcUri, URI dstUri) throws IOException {
  Blob blob = getBlob(srcUri);
  Blob newBlob = getBucket(dstUri).create(sanitizePath(getBase(dstUri).relativize(dstUri).getPath()), new byte[0]);
  CopyWriter copyWriter = blob.copyTo(newBlob.getBlobId());
  copyWriter.getResult();
  return copyWriter.isDone();
}
 
Example #5
Source File: StorageExample.java    From google-cloud-java with Apache License 2.0 4 votes vote down vote up
@Override
public void run(Storage storage, CopyRequest request) {
  CopyWriter copyWriter = storage.copy(request);
  System.out.printf("Copied %s%n", copyWriter.getResult());
}