/** Example of creating a bucket. */ // [TARGET create(BucketInfo, BucketTargetOption...)] // [VARIABLE "my_unique_bucket"] public Bucket createBucket(String bucketName) { // [START createBucket] Bucket bucket = storage.create(BucketInfo.of(bucketName)); // [END createBucket] return bucket; }
/** Example of creating a blob from a byte array. */ // [TARGET create(BlobInfo, byte[], BlobTargetOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public Blob createBlobFromByteArray(String bucketName, String blobName) { // [START createBlobFromByteArray] BlobId blobId = BlobId.of(bucketName, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, "Hello, World!".getBytes(UTF_8)); // [END createBlobFromByteArray] return blob; }
/** Example of creating a blob with no content. */ // [TARGET create(BlobInfo, BlobTargetOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public Blob createBlob(String bucketName, String blobName) { // [START createBlob] BlobId blobId = BlobId.of(bucketName, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo); // [END createBlob] return blob; }
public static void main(String... args) { // [START storage_upload_file] Storage storage = StorageOptions.getDefaultInstance().getService(); BlobId blobId = BlobId.of("bucket", "blob_name"); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8)); // [END storage_upload_file] } }
@Test public void testCreateBucketWithOptions() { EasyMock.expect(storageRpcMock.create(BUCKET_INFO1.toPb(), BUCKET_TARGET_OPTIONS)) .andReturn(BUCKET_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.create(BUCKET_INFO1, BUCKET_TARGET_METAGENERATION, BUCKET_TARGET_PREDEFINED_ACL); assertEquals(expectedBucket1, bucket); }
@Test public void testCreateBucket() { EasyMock.expect(storageRpcMock.create(BUCKET_INFO1.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(BUCKET_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.create(BUCKET_INFO1); assertEquals(expectedBucket1, bucket); }
private static void fillFile(Storage storage, String bucket, String fname, int size) throws IOException { storage.create(BlobInfo.newBuilder(bucket, fname).build(), randomContents(size)); }
@Test public void testCreateNoContentType() 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}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.create(info, content)).andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.create("n", content); assertEquals(expectedBlob, blob); }
private static void fillRequesterPaysFile(Storage storage, String fname, int size) throws IOException { storage.create( BlobInfo.newBuilder(REQUESTER_PAYS_BUCKET, fname).build(), randomContents(size), BlobTargetOption.userProject(project)); }
@Test public void testDeleteBlobs() { String sourceBlobName1 = "test-delete-blobs-1"; String sourceBlobName2 = "test-delete-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<Boolean> deleteStatus = storage.delete(sourceBlob1.getBlobId(), sourceBlob2.getBlobId()); assertTrue(deleteStatus.get(0)); assertTrue(deleteStatus.get(1)); }
@Test public void testDeleteBlobsFail() { String sourceBlobName1 = "test-delete-blobs-fail-1"; String sourceBlobName2 = "test-delete-blobs-fail-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); List<Boolean> deleteStatus = storage.delete(sourceBlob1.getBlobId(), sourceBlob2.getBlobId()); assertTrue(deleteStatus.get(0)); assertFalse(deleteStatus.get(1)); }
@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 testGetBlobKmsKeyNameField() { String blobName = "test-get-selected-kms-key-name-field-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build(); assertNotNull(storage.create(blob, Storage.BlobTargetOption.kmsKeyName(kmsKeyOneResourcePath))); Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields(BlobField.KMS_KEY_NAME)); assertEquals(blob.getBlobId(), remoteBlob.getBlobId()); assertTrue(remoteBlob.getKmsKeyName().startsWith(kmsKeyOneResourcePath)); assertNull(remoteBlob.getContentType()); }
@BeforeClass public static void beforeClass() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { remoteStorageHelper = RemoteStorageHelper.create(); topicAdminClient = TopicAdminClient.create(); storageService = remoteStorageHelper.getOptions().getService(); notificationService = new DefaultNotificationFactory().create(remoteStorageHelper.getOptions().getService()); storageService.create(BucketInfo.of(BUCKET)); projectId = ServiceOptions.getDefaultProjectId(); }
@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 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 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 testEnableDisableTemporaryHold() { String blobName = "test-create-with-temporary-hold"; BlobInfo blobInfo = BlobInfo.newBuilder(BUCKET, blobName).setTemporaryHold(true).build(); Blob remoteBlob = storage.create(blobInfo); assertTrue(remoteBlob.getTemporaryHold()); remoteBlob = storage.get(remoteBlob.getBlobId(), Storage.BlobGetOption.fields(BlobField.TEMPORARY_HOLD)); assertTrue(remoteBlob.getTemporaryHold()); remoteBlob = remoteBlob.toBuilder().setTemporaryHold(false).build().update(); assertFalse(remoteBlob.getTemporaryHold()); }
@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()); }