use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITBucketSnippets method testBucket.
@Test
public void testBucket() throws InterruptedException {
assertTrue(bucketSnippets.exists());
Bucket bucket = bucketSnippets.reload();
assertNotNull(bucket);
Bucket updatedBucket = bucketSnippets.update();
assertTrue(updatedBucket.versioningEnabled());
Blob blob1 = bucketSnippets.createBlobFromByteArray(BLOB1);
assertNotNull(blob1);
Blob blob2 = bucketSnippets.createBlobFromByteArrayWithContentType(BLOB2);
assertNotNull(blob2);
Blob blob3 = bucketSnippets.createBlobFromInputStream(BLOB3);
assertNotNull(blob3);
Blob blob4 = bucketSnippets.createBlobFromInputStreamWithContentType(BLOB4);
assertNotNull(blob4);
Set<Blob> blobSet = Sets.newHashSet(bucketSnippets.listBlobs().iterateAll());
while (blobSet.size() < 4) {
Thread.sleep(500);
blobSet = Sets.newHashSet(bucketSnippets.listBlobs().iterateAll());
}
assertTrue(blobSet.contains(blob1));
assertTrue(blobSet.contains(blob2));
assertTrue(blobSet.contains(blob3));
assertTrue(blobSet.contains(blob4));
blob1 = bucketSnippets.getBlob(BLOB1, blob1.getGeneration());
assertEquals(BLOB1, blob1.getName());
List<Blob> blobs = bucketSnippets.getBlobFromStrings(BLOB2, BLOB3);
assertEquals(BLOB2, blobs.get(0).getName());
assertEquals(BLOB3, blobs.get(1).getName());
blobs = bucketSnippets.getBlobFromStringIterable(BLOB3, BLOB4);
assertEquals(BLOB3, blobs.get(0).getName());
assertEquals(BLOB4, blobs.get(1).getName());
// test ACLs
assertNull(bucketSnippets.getAcl());
assertNotNull(bucketSnippets.createAcl());
Acl updatedAcl = bucketSnippets.updateAcl();
assertEquals(Role.OWNER, updatedAcl.getRole());
Set<Acl> acls = Sets.newHashSet(bucketSnippets.listAcls());
assertTrue(acls.contains(updatedAcl));
assertTrue(bucketSnippets.deleteAcl());
assertNull(bucketSnippets.getAcl());
// test default ACLs
assertNull(bucketSnippets.getDefaultAcl());
assertNotNull(bucketSnippets.createDefaultAcl());
updatedAcl = bucketSnippets.updateDefaultAcl();
assertEquals(Role.OWNER, updatedAcl.getRole());
acls = Sets.newHashSet(bucketSnippets.listDefaultAcls());
assertTrue(acls.contains(updatedAcl));
assertTrue(bucketSnippets.deleteDefaultAcl());
assertNull(bucketSnippets.getDefaultAcl());
thrown.expect(StorageException.class);
assertTrue(bucketSnippets.delete());
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageSnippets method testReadWriteAndSignUrl.
@Test
public void testReadWriteAndSignUrl() throws IOException {
String blobName = "text-read-write-sign-url";
byte[] content = "Hello, World!".getBytes(UTF_8);
Blob blob = storage.create(BlobInfo.newBuilder(BUCKET, blobName).build(), content);
assertArrayEquals(content, storageSnippets.readBlobFromId(BUCKET, blobName, blob.getGeneration()));
assertArrayEquals(content, storageSnippets.readBlobFromStringsWithGeneration(BUCKET, blobName, blob.getGeneration()));
storageSnippets.readerFromId(BUCKET, blobName);
storageSnippets.readerFromStrings(BUCKET, blobName);
storageSnippets.writer(BUCKET, blobName);
URL signedUrl = storageSnippets.signUrl(BUCKET, blobName);
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 = storageSnippets.signUrlWithSigner(BUCKET, blobName, System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
connection = signedUrl.openConnection();
try (InputStream responseStream = connection.getInputStream()) {
assertEquals(content.length, responseStream.read(readBytes));
assertArrayEquals(content, readBytes);
}
blob.delete();
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageSnippets method testBlob.
@Test
public void testBlob() throws InterruptedException {
String blobName = "directory/test-blob";
Blob blob = storageSnippets.createBlob(BUCKET, blobName);
assertNotNull(blob);
blob = storageSnippets.getBlobFromId(BUCKET, blobName);
assertNotNull(blob);
blob = storageSnippets.updateBlob(BUCKET, blobName);
assertNotNull(blob);
blob = storageSnippets.updateBlobWithMetageneration(BUCKET, blobName);
assertNotNull(blob);
Blob copiedBlob = storageSnippets.copyBlob(BUCKET, blobName, "directory/copy-blob");
assertNotNull(copiedBlob);
Page<Blob> blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/");
while (Iterators.size(blobs.iterateAll().iterator()) < 2) {
Thread.sleep(500);
blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/");
}
Set<String> blobNames = new HashSet<>();
Iterator<Blob> blobIterator = blobs.iterateAll().iterator();
while (blobIterator.hasNext()) {
blobNames.add(blobIterator.next().getName());
}
assertTrue(blobNames.contains(blobName));
assertTrue(blobNames.contains("directory/copy-blob"));
try {
storageSnippets.getBlobFromStringsWithMetageneration(BUCKET, blobName, -1);
fail("Expected StorageException to be thrown");
} catch (StorageException ex) {
// expected
}
assertTrue(storageSnippets.deleteBlob(BUCKET, blobName));
copiedBlob.delete();
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageSnippets method testCreateBlobFromInputStream.
@Test
public void testCreateBlobFromInputStream() {
Blob blob = storageSnippets.createBlobFromInputStream(BUCKET, "test-create-blob-from-input-stream");
assertNotNull(blob);
assertTrue(storageSnippets.deleteBlobFromIdWithGeneration(BUCKET, "test-create-blob-from-input-stream", blob.getGeneration()));
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class UpdateBlob method main.
public static void main(String... args) throws IOException {
Storage storage = StorageOptions.getDefaultInstance().getService();
BlobId blobId = BlobId.of("bucket", "blob_name");
Blob blob = storage.get(blobId);
if (blob != null) {
byte[] prevContent = blob.getContent();
System.out.println(new String(prevContent, UTF_8));
WritableByteChannel channel = blob.writer();
channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));
channel.close();
}
}
Aggregations