@Test public void testComposeBlob() { String sourceBlobName1 = "test-compose-blob-source-1"; String sourceBlobName2 = "test-compose-blob-source-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); Blob remoteSourceBlob1 = storage.create(sourceBlob1, BLOB_BYTE_CONTENT); Blob remoteSourceBlob2 = storage.create(sourceBlob2, BLOB_BYTE_CONTENT); assertNotNull(remoteSourceBlob1); assertNotNull(remoteSourceBlob2); String targetBlobName = "test-compose-blob-target"; BlobInfo targetBlob = BlobInfo.newBuilder(BUCKET, targetBlobName).build(); Storage.ComposeRequest req = Storage.ComposeRequest.of(ImmutableList.of(sourceBlobName1, sourceBlobName2), targetBlob); Blob remoteTargetBlob = storage.compose(req); assertNotNull(remoteTargetBlob); assertEquals(targetBlob.getName(), remoteTargetBlob.getName()); assertEquals(targetBlob.getBucket(), remoteTargetBlob.getBucket()); assertNull(remoteTargetBlob.getContentType()); byte[] readBytes = storage.readAllBytes(BUCKET, targetBlobName); byte[] composedBytes = Arrays.copyOf(BLOB_BYTE_CONTENT, BLOB_BYTE_CONTENT.length * 2); System.arraycopy( BLOB_BYTE_CONTENT, 0, composedBytes, BLOB_BYTE_CONTENT.length, BLOB_BYTE_CONTENT.length); assertArrayEquals(composedBytes, readBytes); }
@Test public void testComposeBlobWithContentType() { String sourceBlobName1 = "test-compose-blob-with-content-type-source-1"; String sourceBlobName2 = "test-compose-blob-with-content-type-source-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); Blob remoteSourceBlob1 = storage.create(sourceBlob1, BLOB_BYTE_CONTENT); Blob remoteSourceBlob2 = storage.create(sourceBlob2, BLOB_BYTE_CONTENT); assertNotNull(remoteSourceBlob1); assertNotNull(remoteSourceBlob2); String targetBlobName = "test-compose-blob-with-content-type-target"; BlobInfo targetBlob = BlobInfo.newBuilder(BUCKET, targetBlobName).setContentType(CONTENT_TYPE).build(); Storage.ComposeRequest req = Storage.ComposeRequest.of(ImmutableList.of(sourceBlobName1, sourceBlobName2), targetBlob); Blob remoteTargetBlob = storage.compose(req); assertNotNull(remoteTargetBlob); assertEquals(targetBlob.getName(), remoteTargetBlob.getName()); assertEquals(targetBlob.getBucket(), remoteTargetBlob.getBucket()); assertEquals(CONTENT_TYPE, remoteTargetBlob.getContentType()); byte[] readBytes = storage.readAllBytes(BUCKET, targetBlobName); byte[] composedBytes = Arrays.copyOf(BLOB_BYTE_CONTENT, BLOB_BYTE_CONTENT.length * 2); System.arraycopy( BLOB_BYTE_CONTENT, 0, composedBytes, BLOB_BYTE_CONTENT.length, BLOB_BYTE_CONTENT.length); assertArrayEquals(composedBytes, readBytes); }
/** * Creates a {@code ComposeRequest} object. * * @param bucket name of the bucket where the compose operation takes place * @param sources source blobs names * @param target target blob name */ public static ComposeRequest of(String bucket, Iterable<String> sources, String target) { return of(sources, BlobInfo.newBuilder(BlobId.of(bucket, target)).build()); }
/** * Creates a {@code ComposeRequest} object. * * @param bucket name of the bucket where the compose operation takes place * @param sources source blobs names * @param target target blob name */ public static ComposeRequest of(String bucket, Iterable<String> sources, String target) { return of(sources, BlobInfo.newBuilder(BlobId.of(bucket, target)).build()); }