use of com.google.api.services.storage.model.RewriteResponse in project beam by apache.
the class GcsUtilTest method testRenameSkipDestinationExistsSameBucket.
@Test
public void testRenameSkipDestinationExistsSameBucket() throws IOException {
GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
GcsUtil gcsUtil = pipelineOptions.getGcsUtil();
Storage mockStorage = Mockito.mock(Storage.class);
gcsUtil.setStorageClient(mockStorage);
gcsUtil.setBatchRequestSupplier(() -> new FakeBatcher());
Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class);
Storage.Objects.Rewrite mockStorageRewrite = Mockito.mock(Storage.Objects.Rewrite.class);
Storage.Objects.Delete mockStorageDelete = Mockito.mock(Storage.Objects.Delete.class);
when(mockStorage.objects()).thenReturn(mockStorageObjects);
when(mockStorageObjects.rewrite("bucket", "s0", "bucket", "d0", null)).thenReturn(mockStorageRewrite);
when(mockStorageRewrite.execute()).thenReturn(new RewriteResponse().setDone(true));
when(mockStorageObjects.delete("bucket", "s0")).thenReturn(mockStorageDelete);
gcsUtil.rename(makeStrings("s", 1), makeStrings("d", 1), StandardMoveOptions.SKIP_IF_DESTINATION_EXISTS);
verify(mockStorageRewrite, times(1)).execute();
verify(mockStorageDelete, times(1)).execute();
}
use of com.google.api.services.storage.model.RewriteResponse in project beam by apache.
the class GcsUtilTest method testRename.
@Test
public void testRename() throws IOException {
GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
GcsUtil gcsUtil = pipelineOptions.getGcsUtil();
Storage mockStorage = Mockito.mock(Storage.class);
gcsUtil.setStorageClient(mockStorage);
gcsUtil.setBatchRequestSupplier(() -> new FakeBatcher());
Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class);
Storage.Objects.Rewrite mockStorageRewrite = Mockito.mock(Storage.Objects.Rewrite.class);
Storage.Objects.Delete mockStorageDelete1 = Mockito.mock(Storage.Objects.Delete.class);
Storage.Objects.Delete mockStorageDelete2 = Mockito.mock(Storage.Objects.Delete.class);
when(mockStorage.objects()).thenReturn(mockStorageObjects);
when(mockStorageObjects.rewrite("bucket", "s0", "bucket", "d0", null)).thenReturn(mockStorageRewrite);
when(mockStorageRewrite.execute()).thenThrow(new SocketTimeoutException("SocketException")).thenReturn(new RewriteResponse().setDone(true));
when(mockStorageObjects.delete("bucket", "s0")).thenReturn(mockStorageDelete1).thenReturn(mockStorageDelete2);
when(mockStorageDelete1.execute()).thenThrow(new SocketTimeoutException("SocketException"));
gcsUtil.rename(makeStrings("s", 1), makeStrings("d", 1));
verify(mockStorageRewrite, times(2)).execute();
verify(mockStorageDelete1, times(1)).execute();
verify(mockStorageDelete2, times(1)).execute();
}
use of com.google.api.services.storage.model.RewriteResponse in project beam by apache.
the class GcsUtilTest method testRenameIgnoringMissing.
@Test
public void testRenameIgnoringMissing() throws IOException {
GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
GcsUtil gcsUtil = pipelineOptions.getGcsUtil();
Storage mockStorage = Mockito.mock(Storage.class);
gcsUtil.setStorageClient(mockStorage);
gcsUtil.setBatchRequestSupplier(() -> new FakeBatcher());
Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class);
Storage.Objects.Rewrite mockStorageRewrite1 = Mockito.mock(Storage.Objects.Rewrite.class);
Storage.Objects.Rewrite mockStorageRewrite2 = Mockito.mock(Storage.Objects.Rewrite.class);
Storage.Objects.Delete mockStorageDelete = Mockito.mock(Storage.Objects.Delete.class);
when(mockStorage.objects()).thenReturn(mockStorageObjects);
when(mockStorageObjects.rewrite("bucket", "s0", "bucket", "d0", null)).thenReturn(mockStorageRewrite1);
when(mockStorageRewrite1.execute()).thenThrow(new FileNotFoundException());
when(mockStorageObjects.rewrite("bucket", "s1", "bucket", "d1", null)).thenReturn(mockStorageRewrite2);
when(mockStorageRewrite2.execute()).thenReturn(new RewriteResponse().setDone(true));
when(mockStorageObjects.delete("bucket", "s1")).thenReturn(mockStorageDelete);
gcsUtil.rename(makeStrings("s", 2), makeStrings("d", 2), StandardMoveOptions.IGNORE_MISSING_FILES);
verify(mockStorageRewrite1, times(1)).execute();
verify(mockStorageRewrite2, times(1)).execute();
verify(mockStorageDelete, times(1)).execute();
}
use of com.google.api.services.storage.model.RewriteResponse in project java-docs-samples by GoogleCloudPlatform.
the class CustomerSuppliedEncryptionKeysSamples method rotateKey.
/**
* Given an existing, CSEK-protected object, changes the key used to store that object.
*
* @param storage A Storage object, ready for use
* @param bucketName The name of the destination bucket
* @param objectName The name of the destination object
* @param originalBase64Key The AES256 key currently associated with this object,
* encoded as a base64 string.
* @param originalBase64KeyHash The SHA-256 hash of the above key,
* also encoded as a base64 string.
* @param newBase64Key An AES256 key which will replace the existing key,
* encoded as a base64 string.
* @param newBase64KeyHash The SHA-256 hash of the above key, also encoded as a base64 string.
* @throws IOException if there was some error download from GCS.
*/
public static void rotateKey(Storage storage, String bucketName, String objectName, String originalBase64Key, String originalBase64KeyHash, String newBase64Key, String newBase64KeyHash) throws Exception {
// Set the CSEK headers
final HttpHeaders httpHeaders = new HttpHeaders();
// Specify the exiting object's current CSEK.
httpHeaders.set("x-goog-copy-source-encryption-algorithm", "AES256");
httpHeaders.set("x-goog-copy-source-encryption-key", originalBase64Key);
httpHeaders.set("x-goog-copy-source-encryption-key-sha256", originalBase64KeyHash);
// Specify the new CSEK that we would like to apply.
httpHeaders.set("x-goog-encryption-algorithm", "AES256");
httpHeaders.set("x-goog-encryption-key", newBase64Key);
httpHeaders.set("x-goog-encryption-key-sha256", newBase64KeyHash);
Storage.Objects.Rewrite rewriteObject = storage.objects().rewrite(bucketName, objectName, bucketName, objectName, null);
rewriteObject.setRequestHeaders(httpHeaders);
try {
RewriteResponse rewriteResponse = rewriteObject.execute();
// rewrite until the operation completes.
while (!rewriteResponse.getDone()) {
System.out.println("Rewrite did not complete. Resuming...");
rewriteObject.setRewriteToken(rewriteResponse.getRewriteToken());
rewriteResponse = rewriteObject.execute();
}
} catch (GoogleJsonResponseException e) {
System.out.println("Error rotating key: " + e.getContent());
System.exit(1);
}
}
Aggregations