use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method createKmsEncrpytedBlob.
/**
* Example of uploading a blob encrypted service side with a Cloud KMS key.
*/
public Blob createKmsEncrpytedBlob(String bucketName, String blobName, String kmsKeyName) {
// [START storage_upload_with_kms_key]
byte[] data = "Hello, World!".getBytes(UTF_8);
// The name of the existing bucket to set a default KMS key for, e.g. "my-bucket"
// String bucketName = "my-bucket"
// The name of the KMS-key to use as a default
// Key names are provided in the following format:
// 'projects/<PROJECT>/locations/<LOCATION>/keyRings/<RING_NAME>/cryptoKeys/<KEY_NAME>'
// String kmsKeyName = ""
BlobId blobId = BlobId.of(bucketName, blobName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
Blob blob = storage.create(blobInfo, data, BlobTargetOption.kmsKeyName(kmsKeyName));
// [END storage_upload_with_kms_key]
return blob;
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageSnippets method testBlobAcl.
@Test
public void testBlobAcl() {
String blobName = "test-blob-acl";
BlobId blobId = BlobId.of(BUCKET, "test-blob-acl");
BlobInfo blob = BlobInfo.newBuilder(blobId).build();
Blob createdBlob = storage.create(blob);
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
assertNotNull(storageSnippets.createBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
Acl updatedAcl = storageSnippets.updateBlobAcl(BUCKET, blobName, createdBlob.getGeneration());
assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
Set<Acl> acls = Sets.newHashSet(storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
assertTrue(acls.contains(updatedAcl));
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL));
storage.createAcl(BlobId.of(BUCKET, blobName), Acl.of(new User(USER_EMAIL), Role.READER));
Acl userAcl = storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL);
assertNotNull(userAcl);
assertEquals(USER_EMAIL, ((User) userAcl.getEntity()).getEmail());
assertNotNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
// test non-existing blob
String nonExistingBlob = "test-blob-acl";
assertNull(storageSnippets.getBlobAcl(BUCKET, nonExistingBlob, 1L));
assertFalse(storageSnippets.deleteBlobAcl(BUCKET, nonExistingBlob, 1L));
try {
storageSnippets.createBlobAcl(BUCKET, nonExistingBlob, 1L);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
try {
storageSnippets.updateBlobAcl(BUCKET, nonExistingBlob, 1L);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
try {
storageSnippets.listBlobAcls(BUCKET, nonExistingBlob, 1L);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testBatchRequest.
@Test
public void testBatchRequest() {
String sourceBlobName1 = "test-batch-request-blob-1";
String sourceBlobName2 = "test-batch-request-blob-2";
BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
assertNotNull(storage.create(sourceBlob1));
assertNotNull(storage.create(sourceBlob2));
// Batch update request
BlobInfo updatedBlob1 = sourceBlob1.toBuilder().setContentType(CONTENT_TYPE).build();
BlobInfo updatedBlob2 = sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build();
StorageBatch updateBatch = storage.batch();
StorageBatchResult<Blob> updateResult1 = updateBatch.update(updatedBlob1);
StorageBatchResult<Blob> updateResult2 = updateBatch.update(updatedBlob2);
updateBatch.submit();
Blob remoteUpdatedBlob1 = updateResult1.get();
Blob remoteUpdatedBlob2 = updateResult2.get();
assertEquals(sourceBlob1.getBucket(), remoteUpdatedBlob1.getBucket());
assertEquals(sourceBlob1.getName(), remoteUpdatedBlob1.getName());
assertEquals(sourceBlob2.getBucket(), remoteUpdatedBlob2.getBucket());
assertEquals(sourceBlob2.getName(), remoteUpdatedBlob2.getName());
assertEquals(updatedBlob1.getContentType(), remoteUpdatedBlob1.getContentType());
assertEquals(updatedBlob2.getContentType(), remoteUpdatedBlob2.getContentType());
// Batch get request
StorageBatch getBatch = storage.batch();
StorageBatchResult<Blob> getResult1 = getBatch.get(BUCKET, sourceBlobName1);
StorageBatchResult<Blob> getResult2 = getBatch.get(BUCKET, sourceBlobName2);
getBatch.submit();
Blob remoteBlob1 = getResult1.get();
Blob remoteBlob2 = getResult2.get();
assertEquals(remoteUpdatedBlob1, remoteBlob1);
assertEquals(remoteUpdatedBlob2, remoteBlob2);
// Batch delete request
StorageBatch deleteBatch = storage.batch();
StorageBatchResult<Boolean> deleteResult1 = deleteBatch.delete(BUCKET, sourceBlobName1);
StorageBatchResult<Boolean> deleteResult2 = deleteBatch.delete(BUCKET, sourceBlobName2);
deleteBatch.submit();
assertTrue(deleteResult1.get());
assertTrue(deleteResult2.get());
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadAndWriteCaptureChannels.
@Test
public void testReadAndWriteCaptureChannels() throws IOException {
String blobName = "test-read-and-write-capture-channels-blob";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
byte[] stringBytes;
WriteChannel writer = storage.writer(blob);
stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
RestorableState<WriteChannel> writerState = writer.capture();
WriteChannel secondWriter = writerState.restore();
secondWriter.write(ByteBuffer.wrap(stringBytes));
secondWriter.close();
ByteBuffer readBytes;
ByteBuffer readStringBytes;
ReadChannel reader = storage.reader(blob.getBlobId());
reader.setChunkSize(BLOB_BYTE_CONTENT.length);
readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length);
reader.read(readBytes);
RestorableState<ReadChannel> readerState = reader.capture();
ReadChannel secondReader = readerState.restore();
readStringBytes = ByteBuffer.allocate(stringBytes.length);
secondReader.read(readStringBytes);
reader.close();
secondReader.close();
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array());
assertEquals(BLOB_STRING_CONTENT, new String(readStringBytes.array(), UTF_8));
assertTrue(storage.delete(BUCKET, blobName));
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testCreateBlobFail.
@Test
public void testCreateBlobFail() {
String blobName = "test-create-blob-fail";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Blob remoteBlob = storage.create(blob);
assertNotNull(remoteBlob);
BlobInfo wrongGenerationBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build();
try {
storage.create(wrongGenerationBlob, BLOB_BYTE_CONTENT, Storage.BlobTargetOption.generationMatch());
fail("StorageException was expected");
} catch (StorageException ex) {
// expected
}
assertTrue(remoteBlob.delete());
}
Aggregations