@Test public void testGetBlobs() { String sourceBlobName1 = "test-get-blobs-1"; String sourceBlobName2 = "test-get-blobs-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); assertNotNull(storage.create(sourceBlob2)); List<Blob> remoteBlobs = storage.get(sourceBlob1.getBlobId(), sourceBlob2.getBlobId()); assertEquals(sourceBlob1.getBucket(), remoteBlobs.get(0).getBucket()); assertEquals(sourceBlob1.getName(), remoteBlobs.get(0).getName()); assertEquals(sourceBlob2.getBucket(), remoteBlobs.get(1).getBucket()); assertEquals(sourceBlob2.getName(), remoteBlobs.get(1).getName()); }
@Test public void testGetBlobsFail() { String sourceBlobName1 = "test-get-blobs-fail-1"; String sourceBlobName2 = "test-get-blobs-fail-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); List<Blob> remoteBlobs = storage.get(sourceBlob1.getBlobId(), sourceBlob2.getBlobId()); assertEquals(sourceBlob1.getBucket(), remoteBlobs.get(0).getBucket()); assertEquals(sourceBlob1.getName(), remoteBlobs.get(0).getName()); assertNull(remoteBlobs.get(1)); }
@Test public void testCreateBlobStream() { String blobName = "test-create-blob-stream"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build(); ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8)); Blob remoteBlob = storage.create(blob, stream); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); assertEquals(blob.getContentType(), remoteBlob.getContentType()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertEquals(BLOB_STRING_CONTENT, new String(readBytes, UTF_8)); }
@Test public void testUpdateBlobUnsetMetadata() { String blobName = "test-update-blob-unset-metadata"; ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a", "k2", "b"); Map<String, String> newMetadata = new HashMap<>(); newMetadata.put("k1", "a"); newMetadata.put("k2", null); ImmutableMap<String, String> expectedMetadata = ImmutableMap.of("k1", "a"); BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName) .setContentType(CONTENT_TYPE) .setMetadata(metadata) .build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); Blob updatedBlob = remoteBlob.toBuilder().setMetadata(newMetadata).build().update(); assertNotNull(updatedBlob); assertEquals(blob.getName(), updatedBlob.getName()); assertEquals(blob.getBucket(), updatedBlob.getBucket()); assertEquals(expectedMetadata, updatedBlob.getMetadata()); }
@Test public void testUpdateBlobs() { String sourceBlobName1 = "test-update-blobs-1"; String sourceBlobName2 = "test-update-blobs-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); Blob remoteBlob1 = storage.create(sourceBlob1); Blob remoteBlob2 = storage.create(sourceBlob2); assertNotNull(remoteBlob1); assertNotNull(remoteBlob2); List<Blob> updatedBlobs = storage.update( remoteBlob1.toBuilder().setContentType(CONTENT_TYPE).build(), remoteBlob2.toBuilder().setContentType(CONTENT_TYPE).build()); assertEquals(sourceBlob1.getBucket(), updatedBlobs.get(0).getBucket()); assertEquals(sourceBlob1.getName(), updatedBlobs.get(0).getName()); assertEquals(CONTENT_TYPE, updatedBlobs.get(0).getContentType()); assertEquals(sourceBlob2.getBucket(), updatedBlobs.get(1).getBucket()); assertEquals(sourceBlob2.getName(), updatedBlobs.get(1).getName()); assertEquals(CONTENT_TYPE, updatedBlobs.get(1).getContentType()); }
@Test public void testUpdateBlobsFail() { String sourceBlobName1 = "test-update-blobs-fail-1"; String sourceBlobName2 = "test-update-blobs-fail-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); BlobInfo remoteBlob1 = storage.create(sourceBlob1); assertNotNull(remoteBlob1); List<Blob> updatedBlobs = storage.update( remoteBlob1.toBuilder().setContentType(CONTENT_TYPE).build(), sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build()); assertEquals(sourceBlob1.getBucket(), updatedBlobs.get(0).getBucket()); assertEquals(sourceBlob1.getName(), updatedBlobs.get(0).getName()); assertEquals(CONTENT_TYPE, updatedBlobs.get(0).getContentType()); assertNull(updatedBlobs.get(1)); }
@Test public void testPostSignedUrl() throws IOException { if (storage.getOptions().getCredentials() != null) { assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner); } String blobName = "test-post-signed-url-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); assertNotNull(storage.create(blob)); URL url = storage.signUrl(blob, 1, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.POST)); URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.connect(); Blob remoteBlob = storage.get(BUCKET, blobName); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); }
@Test public void testUpdateBlobMergeMetadata() { String blobName = "test-update-blob-merge-metadata"; ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a"); ImmutableMap<String, String> newMetadata = ImmutableMap.of("k2", "b"); ImmutableMap<String, String> expectedMetadata = ImmutableMap.of("k1", "a", "k2", "b"); BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName) .setContentType(CONTENT_TYPE) .setMetadata(metadata) .build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); Blob updatedBlob = remoteBlob.toBuilder().setMetadata(newMetadata).build().update(); assertNotNull(updatedBlob); assertEquals(blob.getName(), updatedBlob.getName()); assertEquals(blob.getBucket(), updatedBlob.getBucket()); assertEquals(expectedMetadata, updatedBlob.getMetadata()); }
@Test public void testCreateEmptyBlob() { String blobName = "test-create-empty-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertArrayEquals(new byte[0], readBytes); }
@Test public void testCreateBlobWithKmsKeyName() { String blobName = "test-create-with-kms-key-name-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create( blob, BLOB_BYTE_CONTENT, Storage.BlobTargetOption.kmsKeyName(kmsKeyOneResourcePath)); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); assertNotNull(remoteBlob.getKmsKeyName()); assertTrue(remoteBlob.getKmsKeyName().startsWith(kmsKeyOneResourcePath)); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); }
@Test public void testGetBlobAllSelectedFields() { String blobName = "test-get-all-selected-fields-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName) .setContentType(CONTENT_TYPE) .setMetadata(ImmutableMap.of("k", "v")) .build(); assertNotNull(storage.create(blob)); Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields(BlobField.values())); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); assertEquals(ImmutableMap.of("k", "v"), remoteBlob.getMetadata()); assertNotNull(remoteBlob.getGeneratedId()); assertNotNull(remoteBlob.getSelfLink()); }
@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 testCreateBlob() { String blobName = "test-create-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); assertTrue(remoteBlob.delete()); }
@Test public void testCreateBlobWithEncryptionKey() { String blobName = "test-create-with-customer-key-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT, Storage.BlobTargetOption.encryptionKey(KEY)); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName, Storage.BlobSourceOption.decryptionKey(BASE64_KEY)); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); }
@Test public void testUpdateBlob() { String blobName = "test-update-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); Blob updatedBlob = remoteBlob.toBuilder().setContentType(CONTENT_TYPE).build().update(); assertNotNull(updatedBlob); assertEquals(blob.getName(), updatedBlob.getName()); assertEquals(blob.getBucket(), updatedBlob.getBucket()); assertEquals(CONTENT_TYPE, updatedBlob.getContentType()); }
@Test public void testUpdateBlobReplaceMetadata() { String blobName = "test-update-blob-replace-metadata"; ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a"); ImmutableMap<String, String> newMetadata = ImmutableMap.of("k2", "b"); BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName) .setContentType(CONTENT_TYPE) .setMetadata(metadata) .build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); Blob updatedBlob = remoteBlob.toBuilder().setMetadata(null).build().update(); assertNotNull(updatedBlob); assertNull(updatedBlob.getMetadata()); updatedBlob = remoteBlob.toBuilder().setMetadata(newMetadata).build().update(); assertEquals(blob.getName(), updatedBlob.getName()); assertEquals(blob.getBucket(), updatedBlob.getBucket()); assertEquals(newMetadata, updatedBlob.getMetadata()); }
@Test public void testCopyBlob() { 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.of(source, BlobId.of(BUCKET, targetBlobName)); 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()); assertTrue(copyWriter.isDone()); assertTrue(remoteBlob.delete()); assertTrue(storage.delete(BUCKET, targetBlobName)); }
@Test public void testCopyBlobNoContentType() { String sourceBlobName = "test-copy-blob-no-content-type-source"; BlobId source = BlobId.of(BUCKET, sourceBlobName); Blob remoteSourceBlob = storage.create(BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT); assertNotNull(remoteSourceBlob); String targetBlobName = "test-copy-blob-no-content-type-target"; ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v"); BlobInfo target = BlobInfo.newBuilder(BUCKET, targetBlobName).setMetadata(metadata).build(); Storage.CopyRequest req = Storage.CopyRequest.of(source, target); CopyWriter copyWriter = storage.copy(req); assertEquals(BUCKET, copyWriter.getResult().getBucket()); assertEquals(targetBlobName, copyWriter.getResult().getName()); assertNull(copyWriter.getResult().getContentType()); assertEquals(metadata, copyWriter.getResult().getMetadata()); assertTrue(copyWriter.isDone()); assertTrue(remoteSourceBlob.delete()); assertTrue(storage.delete(BUCKET, targetBlobName)); }
@Test public void testCopyBlobUpdateMetadata() { String sourceBlobName = "test-copy-blob-update-metadata-source"; BlobId source = BlobId.of(BUCKET, sourceBlobName); Blob remoteSourceBlob = storage.create(BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT); assertNotNull(remoteSourceBlob); String targetBlobName = "test-copy-blob-update-metadata-target"; ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v"); BlobInfo target = BlobInfo.newBuilder(BUCKET, targetBlobName) .setContentType(CONTENT_TYPE) .setMetadata(metadata) .build(); Storage.CopyRequest req = Storage.CopyRequest.of(source, target); 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()); assertTrue(copyWriter.isDone()); assertTrue(remoteSourceBlob.delete()); assertTrue(storage.delete(BUCKET, targetBlobName)); }
public void testCopyBlobUpdateStorageClass() { String sourceBlobName = "test-copy-blob-update-storage-class-source"; BlobId source = BlobId.of(BUCKET, sourceBlobName); BlobInfo sourceInfo = BlobInfo.newBuilder(source).setStorageClass(StorageClass.STANDARD).build(); Blob remoteSourceBlob = storage.create(sourceInfo, BLOB_BYTE_CONTENT); assertNotNull(remoteSourceBlob); assertEquals(StorageClass.STANDARD, remoteSourceBlob.getStorageClass()); String targetBlobName = "test-copy-blob-update-storage-class-target"; BlobInfo targetInfo = BlobInfo.newBuilder(BUCKET, targetBlobName).setStorageClass(StorageClass.COLDLINE).build(); Storage.CopyRequest req = Storage.CopyRequest.of(source, targetInfo); CopyWriter copyWriter = storage.copy(req); assertEquals(BUCKET, copyWriter.getResult().getBucket()); assertEquals(targetBlobName, copyWriter.getResult().getName()); assertEquals(StorageClass.COLDLINE, copyWriter.getResult().getStorageClass()); assertTrue(copyWriter.isDone()); assertTrue(remoteSourceBlob.delete()); assertTrue(storage.delete(BUCKET, targetBlobName)); }