use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class BucketSnippets method getBlobFromStringIterable.
/**
* Example of getting some blobs in the bucket, using a batch request.
*/
// [TARGET get(Iterable)]
// [VARIABLE "my_blob_name1"]
// [VARIABLE "my_blob_name2"]
public List<Blob> getBlobFromStringIterable(String blobName1, String blobName2) {
// [START getBlobFromStringIterable]
List<String> blobNames = new LinkedList<>();
blobNames.add(blobName1);
blobNames.add(blobName2);
List<Blob> blobs = bucket.get(blobNames);
for (Blob blob : blobs) {
if (blob == null) {
// the blob was not found
}
}
// [END getBlobFromStringIterable]
return blobs;
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class BucketSnippets method createBlobFromInputStreamWithContentType.
/**
* Example of creating a blob in the bucket from an input stream with a content type.
*/
// [TARGET create(String, InputStream, String, BlobWriteOption...)]
// [VARIABLE "my_blob_name"]
public Blob createBlobFromInputStreamWithContentType(String blobName) {
// [START createBlobFromInputStreamWithContentType]
InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
Blob blob = bucket.create(blobName, content, "text/plain");
// [END createBlobFromInputStreamWithContentType]
return blob;
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class CreateAndListBucketsAndBlobs method main.
public static void main(String... args) {
// Create a service object
// Credentials are inferred from the environment.
Storage storage = StorageOptions.getDefaultInstance().getService();
// Create a bucket
// Change this to something unique
String bucketName = "my_unique_bucket";
Bucket bucket = storage.create(BucketInfo.of(bucketName));
// Upload a blob to the newly created bucket
Blob blob = bucket.create("my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain");
// Read the blob content from the server
String blobContent = new String(blob.getContent(), UTF_8);
// List all your buckets
System.out.println("My buckets:");
for (Bucket currentBucket : storage.list().iterateAll()) {
System.out.println(currentBucket);
}
// List the blobs in a particular bucket
System.out.println("My blobs:");
for (Blob currentBlob : bucket.list().iterateAll()) {
System.out.println(currentBlob);
}
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method createEncryptedBlob.
/**
* Example of uploading an encrypted blob.
*/
// [TARGET create(BlobInfo, InputStream, BlobWriteOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "my_encryption_key"]
public Blob createEncryptedBlob(String bucketName, String blobName, String encryptionKey) {
// [START storageUploadEncryptedFile]
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, BlobWriteOption.encryptionKey(encryptionKey));
// [END storageUploadEncryptedFile]
return blob;
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method copyBlob.
/**
* Example of copying a blob.
*/
// [TARGET copy(CopyRequest)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "copy_blob_name"]
public Blob copyBlob(String bucketName, String blobName, String copyBlobName) {
// [START copyBlob]
CopyRequest request = CopyRequest.newBuilder().setSource(BlobId.of(bucketName, blobName)).setTarget(BlobId.of(bucketName, copyBlobName)).build();
Blob blob = storage.copy(request).getResult();
// [END copyBlob]
return blob;
}
Aggregations