Search in sources :

Example 1 with Blob

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;
}
Also used : Blob(com.google.cloud.storage.Blob) LinkedList(java.util.LinkedList)

Example 2 with Blob

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;
}
Also used : Blob(com.google.cloud.storage.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 3 with 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);
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 4 with Blob

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;
}
Also used : Blob(com.google.cloud.storage.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Example 5 with 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;
}
Also used : Blob(com.google.cloud.storage.Blob) CopyRequest(com.google.cloud.storage.Storage.CopyRequest)

Aggregations

Blob (com.google.cloud.storage.Blob)77 BlobInfo (com.google.cloud.storage.BlobInfo)50 Test (org.junit.Test)50 BlobId (com.google.cloud.storage.BlobId)23 StorageException (com.google.cloud.storage.StorageException)15 Storage (com.google.cloud.storage.Storage)12 CopyWriter (com.google.cloud.storage.CopyWriter)10 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)7 StorageBatch (com.google.cloud.storage.StorageBatch)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 ReadChannel (com.google.cloud.ReadChannel)3 Acl (com.google.cloud.storage.Acl)3 Bucket (com.google.cloud.storage.Bucket)3 CopyRequest (com.google.cloud.storage.Storage.CopyRequest)3 HashMap (java.util.HashMap)3 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 FileInputStream (java.io.FileInputStream)2 ByteBuffer (java.nio.ByteBuffer)2