Search in sources :

Example 71 with Blob

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

the class StorageSnippets method batch.

/**
   * Example of using a batch request to delete, update and get a blob.
   */
// [TARGET batch()]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name1"]
// [VARIABLE "my_blob_name2"]
public Blob batch(String bucketName, String blobName1, String blobName2) {
    // [START batch]
    StorageBatch batch = storage.batch();
    BlobId firstBlob = BlobId.of(bucketName, blobName1);
    BlobId secondBlob = BlobId.of(bucketName, blobName2);
    batch.delete(firstBlob).notify(new BatchResult.Callback<Boolean, StorageException>() {

        public void success(Boolean result) {
        // deleted successfully
        }

        public void error(StorageException exception) {
        // delete failed
        }
    });
    batch.update(BlobInfo.newBuilder(secondBlob).setContentType("text/plain").build());
    StorageBatchResult<Blob> result = batch.get(secondBlob);
    batch.submit();
    // returns get result or throws StorageException
    Blob blob = result.get();
    // [END batch]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) StorageBatch(com.google.cloud.storage.StorageBatch) StorageBatchResult(com.google.cloud.storage.StorageBatchResult) BatchResult(com.google.cloud.BatchResult) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException)

Example 72 with Blob

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

the class StorageSnippets method batchUpdate.

/**
   * Example of updating information on several blobs using a single batch request.
   */
// [TARGET update(BlobInfo...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name1"]
// [VARIABLE "my_blob_name2"]
public List<Blob> batchUpdate(String bucketName, String blobName1, String blobName2) {
    // [START batchUpdate]
    Blob firstBlob = storage.get(bucketName, blobName1);
    Blob secondBlob = storage.get(bucketName, blobName2);
    List<Blob> updatedBlobs = storage.update(firstBlob.toBuilder().setContentType("text/plain").build(), secondBlob.toBuilder().setContentType("text/plain").build());
    // [END batchUpdate]
    return updatedBlobs;
}
Also used : Blob(com.google.cloud.storage.Blob)

Example 73 with Blob

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

the class StorageSnippets method getBlobFromIdWithMetageneration.

/**
   * Example of getting information on a blob, only if its metageneration matches a value,
   * otherwise a {@link StorageException} is thrown.
   */
// [TARGET get(BlobId, BlobGetOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE 42]
public Blob getBlobFromIdWithMetageneration(String bucketName, String blobName, long blobMetageneration) {
    // [START getBlobFromIdWithMetageneration]
    BlobId blobId = BlobId.of(bucketName, blobName);
    Blob blob = storage.get(blobId, BlobGetOption.metagenerationMatch(blobMetageneration));
    // [END getBlobFromIdWithMetageneration]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) BlobId(com.google.cloud.storage.BlobId)

Example 74 with Blob

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

the class BlobSnippets method moveTo.

/**
   * Example of moving a blob to a different bucket with a different name.
   */
// [TARGET copyTo(String, String, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "move_blob_name"]
public Blob moveTo(String destBucket, String destBlob) {
    // [START storageMoveFile]
    CopyWriter copyWriter = blob.copyTo(destBucket, destBlob);
    Blob copiedBlob = copyWriter.getResult();
    boolean deleted = blob.delete();
    // [END storageMoveFile]
    if (deleted) {
        return copiedBlob;
    } else {
        return null;
    }
}
Also used : Blob(com.google.cloud.storage.Blob) CopyWriter(com.google.cloud.storage.CopyWriter)

Example 75 with Blob

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

the class BlobSnippets method update.

/**
   * Example of replacing blob's metadata.
   */
// [TARGET update(BlobTargetOption...)]
public Blob update() {
    // [START update]
    Map<String, String> newMetadata = new HashMap<>();
    newMetadata.put("key", "value");
    blob.toBuilder().setMetadata(null).build().update();
    Blob updatedBlob = blob.toBuilder().setMetadata(newMetadata).build().update();
    // [END update]
    return updatedBlob;
}
Also used : Blob(com.google.cloud.storage.Blob) HashMap(java.util.HashMap)

Aggregations

Blob (com.google.cloud.storage.Blob)80 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)16 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