@Test public void testCreateFromStreamNoContentType() throws Exception { initializeExpectedBucket(5); BlobInfo info = BlobInfo.newBuilder("b", "n").build(); Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info)); byte[] content = {0xD, 0xE, 0xA, 0xD}; InputStream streamContent = new ByteArrayInputStream(content); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.create(info, streamContent)).andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.create("n", streamContent); assertEquals(expectedBlob, blob); }
@Test public void testUpdateBlob() { BlobInfo updatedBlobInfo = BLOB_INFO1.toBuilder().setContentType("some-content-type").build(); EasyMock.expect(storageRpcMock.patch(updatedBlobInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(updatedBlobInfo.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.update(updatedBlobInfo); assertEquals(new Blob(storage, new BlobInfo.BuilderImpl(updatedBlobInfo)), blob); }
blobInfoBuilder.setContentDisposition(contentDispositionType + "; filename=" + ffFilename); .evaluateAttributeExpressions(ff).getValue(); if (contentType != null) { blobInfoBuilder.setContentType(contentType); .evaluateAttributeExpressions(ff).getValue(); if (md5 != null) { blobInfoBuilder.setMd5(md5); blobWriteOptions.add(Storage.BlobWriteOption.md5Match()); .evaluateAttributeExpressions(ff).getValue(); if (crc32c != null) { blobInfoBuilder.setCrc32c(crc32c); blobWriteOptions.add(Storage.BlobWriteOption.crc32cMatch()); blobInfoBuilder.setMetadata(userMetadata); final Blob blob = storage.create(blobInfoBuilder.build(), in, blobWriteOptions.toArray(new Storage.BlobWriteOption[blobWriteOptions.size()]) attributes.put(BUCKET_ATTR, blob.getBucket()); attributes.put(KEY_ATTR, blob.getName()); if (blob.getSize() != null) { attributes.put(SIZE_ATTR, String.valueOf(blob.getSize()));
@Test public void testCopyToBlobId() throws Exception { initializeExpectedBlob(2); BlobInfo target = BlobInfo.newBuilder(BlobId.of("bt", "nt")).build(); BlobId targetId = BlobId.of("bt", "nt"); CopyWriter copyWriter = createMock(CopyWriter.class); Capture<CopyRequest> capturedCopyRequest = Capture.newInstance(); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter); replay(storage); initializeBlob(); CopyWriter returnedCopyWriter = blob.copyTo(targetId); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().getSource(), blob.getBlobId()); assertEquals(capturedCopyRequest.getValue().getTarget(), target); assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty()); }
@Test public void testCreateEmptyBlob() throws IOException { Capture<ByteArrayInputStream> capturedStream = Capture.newInstance(); EasyMock.expect( storageRpcMock.create( EasyMock.eq( BLOB_INFO1 .toBuilder() .setMd5("1B2M2Y8AsgTpgAmY7PhCfg==") .setCrc32c("AAAAAA==") .build() .toPb()), EasyMock.capture(capturedStream), EasyMock.eq(EMPTY_RPC_OPTIONS))) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.create(BLOB_INFO1); assertEquals(expectedBlob1, blob); ByteArrayInputStream byteStream = capturedStream.getValue(); byte[] streamBytes = new byte[BLOB_CONTENT.length]; assertEquals(-1, byteStream.read(streamBytes)); }
@Test public void testWriterWithOptions() { BlobInfo info = BLOB_INFO1.toBuilder().setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build(); EasyMock.expect(storageRpcMock.open(info.toPb(), BLOB_TARGET_OPTIONS_CREATE)) .andReturn("upload-id"); EasyMock.replay(storageRpcMock); initializeService(); WriteChannel channel = storage.writer( info, BLOB_WRITE_METAGENERATION, BLOB_WRITE_NOT_EXIST, BLOB_WRITE_PREDEFINED_ACL, BLOB_WRITE_CRC2C, BLOB_WRITE_MD5_HASH); assertNotNull(channel); assertTrue(channel.isOpen()); }
@Test public void testGet() throws Exception { initializeExpectedBucket(5); Blob expectedBlob = new Blob( serviceMockReturnsOptions, new BlobInfo.BuilderImpl(BlobInfo.newBuilder("b", "n").build())); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BlobId.of(expectedBucket.getName(), "n"), new Storage.BlobGetOption[0])) .andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.get("n"); assertEquals(expectedBlob, blob); }
private void initializeExpectedBucket(int optionsCalls) { expect(serviceMockReturnsOptions.getOptions()).andReturn(mockOptions).times(optionsCalls); replay(serviceMockReturnsOptions); expectedBucket = new Bucket(serviceMockReturnsOptions, new BucketInfo.BuilderImpl(BUCKET_INFO)); blobResults = ImmutableList.of( new Blob( serviceMockReturnsOptions, new BlobInfo.BuilderImpl(BlobInfo.newBuilder("b", "n1").build())), new Blob( serviceMockReturnsOptions, new BlobInfo.BuilderImpl(BlobInfo.newBuilder("b", "n2").build())), new Blob( serviceMockReturnsOptions, new BlobInfo.BuilderImpl(BlobInfo.newBuilder("b", "n3").build()))); }
@Test public void testCreateBlobFromStreamWithEncryptionKey() throws IOException { ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT); BlobInfo.Builder infoBuilder = BLOB_INFO1.toBuilder(); BlobInfo infoWithHashes = infoBuilder.setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build(); BlobInfo infoWithoutHashes = infoBuilder.setMd5(null).setCrc32c(null).build(); EasyMock.expect( storageRpcMock.create(infoWithoutHashes.toPb(), fileStream, ENCRYPTION_KEY_OPTIONS)) .andReturn(BLOB_INFO1.toPb()) .times(2); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.create(infoWithHashes, fileStream, BlobWriteOption.encryptionKey(BASE64_KEY)); assertEquals(expectedBlob1, blob); blob = storage.create(infoWithHashes, fileStream, BlobWriteOption.encryptionKey(BASE64_KEY)); assertEquals(expectedBlob1, blob); }
@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)); }
/** * Creates a new blob in this bucket. Direct upload is used to upload {@code content}. For large * content, {@link Blob#writer(com.google.cloud.storage.Storage.BlobWriteOption...)} is * recommended as it uses resumable upload. * * <p>Example of creating a blob in the bucket from an input stream. * * <pre>{@code * String blobName = "my_blob_name"; * InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8)); * Blob blob = bucket.create(blobName, content); * }</pre> * * @param blob a blob name * @param content the blob content as a stream * @param options options for blob creation * @return a complete blob information * @throws StorageException upon failure */ public Blob create(String blob, InputStream content, BlobWriteOption... options) { BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(getName(), blob)).build(); Tuple<BlobInfo, Storage.BlobWriteOption[]> write = BlobWriteOption.toWriteOptions(blobInfo, options); return storage.create(write.x(), content, write.y()); }
@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 testGetBlobEmptySelectedFields() { String blobName = "test-get-empty-selected-fields-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build(); assertNotNull(storage.create(blob)); Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields()); assertEquals(blob.getBlobId(), remoteBlob.getBlobId()); assertNull(remoteBlob.getContentType()); }
@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); }
/** Example of creating a blob from an input stream. */ // [TARGET create(BlobInfo, InputStream, BlobWriteOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public Blob createBlobFromInputStream(String bucketName, String blobName) { // [START createBlobFromInputStream] InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8)); BlobId blobId = BlobId.of(bucketName, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, content); // [END createBlobFromInputStream] return blob; }
@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 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()); }
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)); }
@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 testGetBlobFail() { String blobName = "test-get-blob-fail"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); BlobId wrongGenerationBlob = BlobId.of(BUCKET, blobName); try { storage.get(wrongGenerationBlob, Storage.BlobGetOption.generationMatch(-1)); fail("StorageException was expected"); } catch (StorageException ex) { // expected } }