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;
}
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;
}
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;
}
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;
}
}
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;
}
Aggregations