Search in sources :

Example 1 with BlobId

use of com.google.cloud.storage.BlobId in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method listBlobAcls.

/**
   * Example of listing the ACL entries for a blob.
   */
// [TARGET listAcls(BlobId)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE 42]
public List<Acl> listBlobAcls(String bucketName, String blobName, long blobGeneration) {
    // [START listBlobAcls]
    BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
    List<Acl> acls = storage.listAcls(blobId);
    for (Acl acl : acls) {
    // do something with ACL entry
    }
    // [END listBlobAcls]
    return acls;
}
Also used : Acl(com.google.cloud.storage.Acl) BlobId(com.google.cloud.storage.BlobId)

Example 2 with BlobId

use of com.google.cloud.storage.BlobId 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 3 with BlobId

use of com.google.cloud.storage.BlobId in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method deleteBlobFromIdWithGeneration.

/**
   * Example of deleting a blob, only if its generation matches a value, otherwise a
   * {@link StorageException} is thrown.
   */
// [TARGET delete(BlobId, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE 42]
public boolean deleteBlobFromIdWithGeneration(String bucketName, String blobName, long blobGeneration) {
    // [START deleteBlobFromIdWithGeneration]
    BlobId blobId = BlobId.of(bucketName, blobName);
    boolean deleted = storage.delete(blobId, BlobSourceOption.generationMatch(blobGeneration));
    if (deleted) {
    // the blob was deleted
    } else {
    // the blob was not found
    }
    // [END deleteBlobFromIdWithGeneration]
    return deleted;
}
Also used : BlobId(com.google.cloud.storage.BlobId)

Example 4 with BlobId

use of com.google.cloud.storage.BlobId in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method createBlobFromInputStream.

/**
   * Example of creating a blob from an input stream.
   */
// [TARGET create(BlobInfo, InputStream, BlobWriteOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public Blob createBlobFromInputStream(String bucketName, String blobName) {
    // [START createBlobFromInputStream]
    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);
    // [END createBlobFromInputStream]
    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 BlobId

use of com.google.cloud.storage.BlobId in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method createBlobFromByteArray.

/**
   * Example of creating a blob from a byte array.
   */
// [TARGET create(BlobInfo, byte[], BlobTargetOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public Blob createBlobFromByteArray(String bucketName, String blobName) {
    // [START createBlobFromByteArray]
    BlobId blobId = BlobId.of(bucketName, blobName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo, "Hello, World!".getBytes(UTF_8));
    // [END createBlobFromByteArray]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Aggregations

BlobId (com.google.cloud.storage.BlobId)39 Blob (com.google.cloud.storage.Blob)23 BlobInfo (com.google.cloud.storage.BlobInfo)20 Test (org.junit.Test)12 Acl (com.google.cloud.storage.Acl)9 Storage (com.google.cloud.storage.Storage)9 StorageException (com.google.cloud.storage.StorageException)9 CopyWriter (com.google.cloud.storage.CopyWriter)5 ReadChannel (com.google.cloud.ReadChannel)2 User (com.google.cloud.storage.Acl.User)2 StorageBatch (com.google.cloud.storage.StorageBatch)2 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 BatchResult (com.google.cloud.BatchResult)1 WriteChannel (com.google.cloud.WriteChannel)1 Builder (com.google.cloud.storage.BlobInfo.Builder)1 ComposeRequest (com.google.cloud.storage.Storage.ComposeRequest)1