use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method copyBlobInChunks.
/**
* Example of copying a blob in chunks.
*/
// [TARGET copy(CopyRequest)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "copy_blob_name"]
public Blob copyBlobInChunks(String bucketName, String blobName, String copyBlobName) {
// [START copyBlobInChunks]
CopyRequest request = CopyRequest.newBuilder().setSource(BlobId.of(bucketName, blobName)).setTarget(BlobId.of(bucketName, copyBlobName)).build();
CopyWriter copyWriter = storage.copy(request);
while (!copyWriter.isDone()) {
copyWriter.copyChunk();
}
Blob blob = copyWriter.getResult();
// [END copyBlobInChunks]
return blob;
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITBlobSnippets method testMoveBlob.
@Test
public void testMoveBlob() throws IOException {
BlobSnippets blobSnippets = new BlobSnippets(blob);
Blob movedBlob = blobSnippets.moveTo(BUCKET, "moveBlob");
assertNotNull(movedBlob);
// Assert that the destination blob exists
Iterator<Blob> blobs = storage.list(BUCKET).iterateAll().iterator();
Blob moveBlob = blobs.next();
assertEquals(BUCKET, moveBlob.getBucket());
assertEquals("moveBlob", moveBlob.getName());
// Assert that the old blob doesn't exist
assertFalse(blobs.hasNext());
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITBlobSnippets method testBlob.
@Test
public void testBlob() throws IOException {
BlobSnippets blobSnippets = new BlobSnippets(blob);
assertTrue(blobSnippets.exists());
assertArrayEquals(EMPTY_CONTENT, blobSnippets.getContent());
try {
assertNotNull(blobSnippets.reload());
fail("Expected StorageException to be thrown");
} catch (StorageException ex) {
// expected
}
Blob updatedBlob = blobSnippets.update();
assertEquals(ImmutableMap.of("key", "value"), updatedBlob.getMetadata());
Blob copiedBlob = blobSnippets.copyToStrings(BUCKET, "copyBlob");
assertNotNull(copiedBlob);
copiedBlob.delete();
copiedBlob = blobSnippets.copyToId(BUCKET, "copyBlob");
assertNotNull(copiedBlob);
copiedBlob.delete();
copiedBlob = blobSnippets.copyToBucket(BUCKET);
assertNotNull(copiedBlob);
copiedBlob.delete();
blobSnippets.reload();
blobSnippets.writer();
URL signedUrl = blobSnippets.signUrl();
URLConnection connection = signedUrl.openConnection();
byte[] readBytes = new byte[CONTENT.length];
try (InputStream responseStream = connection.getInputStream()) {
assertEquals(CONTENT.length, responseStream.read(readBytes));
assertArrayEquals(CONTENT, readBytes);
}
signedUrl = blobSnippets.signUrlWithSigner(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
connection = signedUrl.openConnection();
try (InputStream responseStream = connection.getInputStream()) {
assertEquals(CONTENT.length, responseStream.read(readBytes));
assertArrayEquals(CONTENT, readBytes);
}
assertFalse(blobSnippets.delete());
blobSnippets = new BlobSnippets(storage.get(blob.getBucket(), blob.getName()));
byte[] subcontent = blobSnippets.readContentRange(1, 8);
assertArrayEquals("ello, W".getBytes(UTF_8), subcontent);
assertNull(blobSnippets.getAcl());
assertNotNull(blobSnippets.createAcl());
Acl updatedAcl = blobSnippets.updateAcl();
assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
Set<Acl> acls = Sets.newHashSet(blobSnippets.listAcls());
assertTrue(acls.contains(updatedAcl));
assertTrue(blobSnippets.deleteAcl());
assertNull(blobSnippets.getAcl());
storage.delete(BlobId.of(BUCKET, BLOB));
}
use of com.google.cloud.storage.Blob 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());
updatedAcl = storageSnippets.blobToPublicRead(BUCKET, blobName, createdBlob.getGeneration());
assertEquals(Acl.Role.READER, updatedAcl.getRole());
assertEquals(User.ofAllUsers(), updatedAcl.getEntity());
acls = Sets.newHashSet(storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
assertTrue(acls.contains(updatedAcl));
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.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageSnippets method testCreateUpdateEncryptedBlob.
@Test
public void testCreateUpdateEncryptedBlob() throws InterruptedException {
// Note: DO NOT put your encryption key in your code, like it is here. Store it somewhere safe,
// and read it in when you need it. This key is just here to make the code easier to read.
String encryptionKey1 = "0mMWhFvQOdS4AmxRpo8SJxXn5MjFhbz7DkKBUdUIef8=";
String blobName = "encrypted-blob";
Blob blob = storageSnippets.createEncryptedBlob(BUCKET, blobName, encryptionKey1);
assertNotNull(blob);
assertEquals("text/plain", blob.getContentType());
byte[] encryptedContent = storageSnippets.readEncryptedBlob(BUCKET, blobName, encryptionKey1);
assertEquals("Hello, World!", new String(encryptedContent));
blob = storageSnippets.getBlobFromId(BUCKET, blobName);
assertEquals("text/plain", blob.getContentType());
String encryptionKey2 = "wnxMO0w+dmxribu7rICJ+Q2ES9TLpFRIDy3/L7HN5ZA=";
blob = storageSnippets.rotateBlobEncryptionKey(BUCKET, blobName, encryptionKey1, encryptionKey2);
assertNotNull(blob);
encryptedContent = storageSnippets.readEncryptedBlob(BUCKET, blobName, encryptionKey2);
assertEquals("Hello, World!", new String(encryptedContent));
blob = storageSnippets.getBlobFromId(BUCKET, blobName);
assertEquals("text/plain", blob.getContentType());
}
Aggregations