Search in sources :

Example 26 with BlobId

use of com.google.cloud.storage.BlobId 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();
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) WritableByteChannel(java.nio.channels.WritableByteChannel) BlobId(com.google.cloud.storage.BlobId)

Example 27 with BlobId

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

the class StorageSnippets method getBlobAcl.

/**
   * Example of getting the ACL entry for a specific user on a blob.
   */
// [TARGET getAcl(BlobId, Entity)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "google-cloud-java-tests@java-docs-samples-tests.iam.gserviceaccount.com"]
public Acl getBlobAcl(String bucketName, String blobName, String userEmail) {
    // [START storagePrintFileAclForUser]
    BlobId blobId = BlobId.of(bucketName, blobName);
    Acl acl = storage.getAcl(blobId, new User(userEmail));
    // [END storagePrintFileAclForUser]
    return acl;
}
Also used : User(com.google.cloud.storage.Acl.User) Acl(com.google.cloud.storage.Acl) BlobId(com.google.cloud.storage.BlobId)

Example 28 with BlobId

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

the class StorageSnippets method readerFromId.

/**
   * Example of reading a blob's content through a reader.
   */
// [TARGET reader(BlobId, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public void readerFromId(String bucketName, String blobName) throws IOException {
    // [START readerFromId]
    BlobId blobId = BlobId.of(bucketName, blobName);
    try (ReadChannel reader = storage.reader(blobId)) {
        ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
        while (reader.read(bytes) > 0) {
            bytes.flip();
            // do something with bytes
            bytes.clear();
        }
    }
// [END readerFromId]
}
Also used : BlobId(com.google.cloud.storage.BlobId) ByteBuffer(java.nio.ByteBuffer) ReadChannel(com.google.cloud.ReadChannel)

Example 29 with BlobId

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

the class StorageSnippets method deleteBlob.

/**
   * Example of deleting a blob.
   */
// [TARGET delete(BlobId)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public boolean deleteBlob(String bucketName, String blobName) {
    // [START deleteBlob]
    BlobId blobId = BlobId.of(bucketName, blobName);
    boolean deleted = storage.delete(blobId);
    if (deleted) {
    // the blob was deleted
    } else {
    // the blob was not found
    }
    // [END deleteBlob]
    return deleted;
}
Also used : BlobId(com.google.cloud.storage.BlobId)

Example 30 with BlobId

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

the class StorageSnippets method deleteBlobAcl.

/**
   * Example of deleting the ACL entry for an entity on a blob.
   */
// [TARGET deleteAcl(BlobId, Entity)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE 42]
public boolean deleteBlobAcl(String bucketName, String blobName, long blobGeneration) {
    // [START deleteBlobAcl]
    BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
    boolean deleted = storage.deleteAcl(blobId, User.ofAllAuthenticatedUsers());
    if (deleted) {
    // the acl entry was deleted
    } else {
    // the acl entry was not found
    }
    // [END deleteBlobAcl]
    return deleted;
}
Also used : 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