Refine search
Storage.CopyRequest.newBuilder().setSource(fromPath.getBlobId()); if (wantReplaceExisting) { copyReqBuilder = copyReqBuilder.setTarget(tgtInfo); } else { copyReqBuilder = copyReqBuilder.setTarget(tgtInfo, Storage.BlobTargetOption.doesNotExist()); copyReqBuilder.setSourceOptions( BlobSourceOption.userProject(fromPath.getFileSystem().config().userProject())); } else if (!isNullOrEmpty(toPath.getFileSystem().config().userProject())) { copyReqBuilder = copyReqBuilder.setSourceOptions( BlobSourceOption.userProject(toPath.getFileSystem().config().userProject())); CopyWriter copyWriter = storage.copy(copyReqBuilder.build()); copyWriter.getResult(); break;
Storage.CopyRequest.newBuilder().setSource(fromPath.getBlobId()); if (wantReplaceExisting) { copyReqBuilder = copyReqBuilder.setTarget(tgtInfo); } else { copyReqBuilder = copyReqBuilder.setTarget(tgtInfo, Storage.BlobTargetOption.doesNotExist()); CopyWriter copyWriter = storage.copy(copyReqBuilder.build()); copyWriter.getResult(); break;
/** * Sends a copy request for the current blob to the target blob. Possibly also some of the * metadata are copied (e.g. content-type). * * <p>Example of copying the blob to a different bucket with a different name. * * <pre>{@code * String bucketName = "my_unique_bucket"; * String blobName = "copy_blob_name"; * CopyWriter copyWriter = blob.copyTo(BlobId.of(bucketName, blobName)); * Blob copiedBlob = copyWriter.getResult(); * }</pre> * * @param targetBlob target blob's id * @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(BlobId targetBlob, BlobSourceOption... options) { CopyRequest copyRequest = CopyRequest.newBuilder() .setSource(getBucket(), getName()) .setSourceOptions(toSourceOptions(this, options)) .setTarget(targetBlob) .build(); return storage.copy(copyRequest); }
/** * Creates a copy request. Target blob information is copied from source. * * @param sourceBucket name of the bucket containing both the source and the target blob * @param sourceBlob name of the source blob * @param targetBlob name of the target blob * @return a copy request */ public static CopyRequest of(String sourceBucket, String sourceBlob, String targetBlob) { return CopyRequest.newBuilder() .setSource(sourceBucket, sourceBlob) .setTarget(BlobId.of(sourceBucket, targetBlob)) .build(); }
/** Creates a builder for {@code CopyRequest} objects. */ public static Builder newBuilder() { return new Builder(); } }
/** * Creates a copy request. Target blob information is copied from source. * * @param sourceBlobId a {@code BlobId} object for the source blob * @param targetBlob name of the target blob, in the same bucket of the source blob * @return a copy request */ public static CopyRequest of(BlobId sourceBlobId, String targetBlob) { return CopyRequest.newBuilder() .setSource(sourceBlobId) .setTarget(BlobId.of(sourceBlobId.getBucket(), targetBlob)) .build(); }
/** * Creates a copy request. Target blob information is copied from source. * * @param sourceBucket name of the bucket containing the source blob * @param sourceBlob name of the source blob * @param target a {@code BlobId} object for the target blob * @return a copy request */ public static CopyRequest of(String sourceBucket, String sourceBlob, BlobId target) { return newBuilder().setSource(sourceBucket, sourceBlob).setTarget(target).build(); }
/** * Creates a copy request. Target blob information is copied from source. * * @param sourceBlobId a {@code BlobId} object for the source blob * @param targetBlobId a {@code BlobId} object for the target blob * @return a copy request */ public static CopyRequest of(BlobId sourceBlobId, BlobId targetBlobId) { return CopyRequest.newBuilder().setSource(sourceBlobId).setTarget(targetBlobId).build(); }
/** * Creates a copy request. {@code target} parameter is used to override source blob information * (e.g. {@code contentType}, {@code contentLanguage}). * * @param sourceBucket name of the bucket containing the source blob * @param sourceBlob name of the source blob * @param target a {@code BlobInfo} object for the target blob * @return a copy request */ public static CopyRequest of(String sourceBucket, String sourceBlob, BlobInfo target) { return newBuilder().setSource(sourceBucket, sourceBlob).setTarget(target).build(); }
/** * Creates a copy request. {@code target} parameter is used to replace source blob information * (e.g. {@code contentType}, {@code contentLanguage}). Target blob information is set exactly * to {@code target}, no information is inherited from the source blob. * * @param sourceBlobId a {@code BlobId} object for the source blob * @param target a {@code BlobInfo} object for the target blob * @return a copy request */ public static CopyRequest of(BlobId sourceBlobId, BlobInfo target) { return newBuilder().setSource(sourceBlobId).setTarget(target).build(); }
.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()); 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());
@Test public void testCopyWithEncryptionKey() { CopyRequest request = Storage.CopyRequest.newBuilder() .setSource(BLOB_INFO2.getBlobId()) .setSourceOptions(BlobSourceOption.decryptionKey(KEY)) .setTarget(BLOB_INFO1, BlobTargetOption.encryptionKey(BASE64_KEY)) .build(); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest( request.getSource().toPb(), ENCRYPTION_KEY_OPTIONS, true, request.getTarget().toPb(), ENCRYPTION_KEY_OPTIONS, null); request = Storage.CopyRequest.newBuilder() .setSource(BLOB_INFO2.getBlobId()) .setSourceOptions(BlobSourceOption.decryptionKey(BASE64_KEY)) .setTarget(BLOB_INFO1, BlobTargetOption.encryptionKey(KEY)) .build(); writer = storage.copy(request); assertEquals(42L, writer.getBlobSize());
@Test public void testCopyFromEncryptionKeyToKmsKeyName() { CopyRequest request = Storage.CopyRequest.newBuilder() .setSource(BLOB_INFO2.getBlobId()) .setSourceOptions(BlobSourceOption.decryptionKey(KEY)) .setTarget(BLOB_INFO1, BlobTargetOption.kmsKeyName(KMS_KEY_NAME)) .build(); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest( request.getSource().toPb(), ENCRYPTION_KEY_OPTIONS, true, request.getTarget().toPb(), KMS_KEY_NAME_OPTIONS, null); request = Storage.CopyRequest.newBuilder() .setSource(BLOB_INFO2.getBlobId()) .setSourceOptions(BlobSourceOption.decryptionKey(BASE64_KEY)) .setTarget(BLOB_INFO1, BlobTargetOption.kmsKeyName(KMS_KEY_NAME)) .build(); writer = storage.copy(request); assertEquals(42L, writer.getBlobSize());
.build(); Storage.CopyRequest req = Storage.CopyRequest.newBuilder() .setSource(source) .setSourceOptions(Storage.BlobSourceOption.decryptionKey(BASE64_KEY)) .setTarget(target, Storage.BlobTargetOption.kmsKeyName(kmsKeyOneResourcePath)) .build(); CopyWriter copyWriter = storage.copy(req); assertEquals(BUCKET, copyWriter.getResult().getBucket());
BlobInfo.newBuilder(BUCKET, targetBlobName).setContentType(CONTENT_TYPE).build(); Storage.CopyRequest req = Storage.CopyRequest.newBuilder() .setSource(BUCKET, sourceBlobName) .setSourceOptions(Storage.BlobSourceOption.generationMatch(-1L)) .setTarget(target) .build(); try { storage.copy(req); Storage.CopyRequest.newBuilder() .setSource(source) .setSourceOptions(Storage.BlobSourceOption.generationMatch()) .setTarget(target) .build(); try { storage.copy(req2);
try { Storage.CopyRequest req = Storage.CopyRequest.newBuilder() .setSource(source) .setSourceOptions(Storage.BlobSourceOption.decryptionKey(BASE64_KEY)) .setTarget( target, Storage.BlobTargetOption.encryptionKey(KEY), Storage.BlobTargetOption.kmsKeyName(kmsKeyOneResourcePath)) .build(); storage.copy(req); fail("StorageException was expected");
@Test public void testCopyRequest() { Storage.CopyRequest copyRequest1 = Storage.CopyRequest.newBuilder() .setSource(SOURCE_BLOB_ID) .setSourceOptions(BlobSourceOption.generationMatch(1)) .setTarget(TARGET_BLOB_INFO, BlobTargetOption.predefinedAcl(PUBLIC_READ)) .build(); assertEquals(SOURCE_BLOB_ID, copyRequest1.getSource()); assertEquals(1, copyRequest1.getSourceOptions().size()); assertEquals(BlobSourceOption.generationMatch(1), copyRequest1.getSourceOptions().get(0)); assertEquals(TARGET_BLOB_INFO, copyRequest1.getTarget()); Storage.CopyRequest.newBuilder() .setSource(SOURCE_BUCKET_NAME, SOURCE_BLOB_NAME) .setTarget(TARGET_BLOB_ID) .build(); assertEquals(SOURCE_BLOB_ID, copyRequest2.getSource()); assertEquals(BlobInfo.newBuilder(TARGET_BLOB_ID).build(), copyRequest2.getTarget()); Storage.CopyRequest.newBuilder() .setSource(SOURCE_BLOB_ID) .setTarget( TARGET_BLOB_INFO, ImmutableList.of(BlobTargetOption.predefinedAcl(PUBLIC_READ))) .build(); assertEquals(SOURCE_BLOB_ID, copyRequest3.getSource()); assertEquals(TARGET_BLOB_INFO, copyRequest3.getTarget());
@Test public void testCopyBlobWithPredefinedAcl() { String sourceBlobName = "test-copy-blob-source"; BlobId source = BlobId.of(BUCKET, sourceBlobName); ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v"); BlobInfo blob = BlobInfo.newBuilder(source).setContentType(CONTENT_TYPE).setMetadata(metadata).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); String targetBlobName = "test-copy-blob-target"; Storage.CopyRequest req = Storage.CopyRequest.newBuilder() .setSource(source) .setTarget( BlobId.of(BUCKET, targetBlobName), Storage.BlobTargetOption.predefinedAcl(Storage.PredefinedAcl.PUBLIC_READ)) .build(); 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()); assertNotNull(copyWriter.getResult().getAcl(User.ofAllUsers())); assertTrue(copyWriter.isDone()); assertTrue(remoteBlob.delete()); assertTrue(storage.delete(BUCKET, targetBlobName)); }
/** * Sends a copy request for the current blob to the target blob. Possibly also some of the * metadata are copied (e.g. content-type). * * <p>Example of copying the blob to a different bucket with a different name. * * <pre>{@code * String bucketName = "my_unique_bucket"; * String blobName = "copy_blob_name"; * CopyWriter copyWriter = blob.copyTo(BlobId.of(bucketName, blobName)); * Blob copiedBlob = copyWriter.getResult(); * }</pre> * * @param targetBlob target blob's id * @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(BlobId targetBlob, BlobSourceOption... options) { CopyRequest copyRequest = CopyRequest.newBuilder() .setSource(getBucket(), getName()) .setSourceOptions(toSourceOptions(this, options)) .setTarget(targetBlob) .build(); return storage.copy(copyRequest); }
@Test public void testCopyWithOptionsFromBlobId() { CopyRequest request = Storage.CopyRequest.newBuilder() .setSource(BLOB_INFO1.getBlobId()) .setSourceOptions(BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION) .setTarget(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) .build(); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest( request.getSource().toPb(), BLOB_SOURCE_OPTIONS_COPY, true, request.getTarget().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); EasyMock.replay(storageRpcMock); initializeService(); CopyWriter writer = storage.copy(request); assertEquals(42L, writer.getBlobSize()); assertEquals(21L, writer.getTotalBytesCopied()); assertTrue(!writer.isDone()); }