Search in sources :

Example 1 with RewriteResponse

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();
}
Also used : Storage(com.google.api.services.storage.Storage) GoogleCloudStorage(com.google.cloud.hadoop.gcsio.GoogleCloudStorage) Objects(com.google.api.services.storage.model.Objects) GcsOptions(org.apache.beam.sdk.extensions.gcp.options.GcsOptions) RewriteResponse(com.google.api.services.storage.model.RewriteResponse) Test(org.junit.Test)

Example 2 with RewriteResponse

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();
}
Also used : Storage(com.google.api.services.storage.Storage) GoogleCloudStorage(com.google.cloud.hadoop.gcsio.GoogleCloudStorage) SocketTimeoutException(java.net.SocketTimeoutException) Objects(com.google.api.services.storage.model.Objects) GcsOptions(org.apache.beam.sdk.extensions.gcp.options.GcsOptions) RewriteResponse(com.google.api.services.storage.model.RewriteResponse) Test(org.junit.Test)

Example 3 with RewriteResponse

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();
}
Also used : Storage(com.google.api.services.storage.Storage) GoogleCloudStorage(com.google.cloud.hadoop.gcsio.GoogleCloudStorage) Objects(com.google.api.services.storage.model.Objects) FileNotFoundException(java.io.FileNotFoundException) GcsOptions(org.apache.beam.sdk.extensions.gcp.options.GcsOptions) RewriteResponse(com.google.api.services.storage.model.RewriteResponse) Test(org.junit.Test)

Example 4 with RewriteResponse

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);
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) RewriteResponse(com.google.api.services.storage.model.RewriteResponse)

Aggregations

RewriteResponse (com.google.api.services.storage.model.RewriteResponse)4 Storage (com.google.api.services.storage.Storage)3 Objects (com.google.api.services.storage.model.Objects)3 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)3 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)3 Test (org.junit.Test)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 FileNotFoundException (java.io.FileNotFoundException)1 SocketTimeoutException (java.net.SocketTimeoutException)1