Interface Storage (2.20.1)

Stay organized with collections Save and categorize content based on your preferences.
public interface Storage extends Service<StorageOptions>, AutoCloseable

An interface for Google Cloud Storage. See Also: Google Cloud Storage

Implements

com.google.cloud.Service<com.google.cloud.storage.StorageOptions>, AutoCloseable

Methods

batch()

public abstract StorageBatch batch()

Creates a new empty batch for grouping multiple service calls in one underlying RPC call.

Example of using a batch request to delete, update and get a blob.


 String bucketName = "my-unique-bucket";
 String blobName1 = "my-blob-name1";
 String blobName2 = "my-blob-name2";
 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();
 Blob blob = result.get(); // returns get result or throws StorageException
 
Returns
TypeDescription
StorageBatch

close()

public default void close()
Exceptions
TypeDescription
Exception

thrown if interrupted while awaiting termination of underlying resources

compose(Storage.ComposeRequest composeRequest)

public abstract Blob compose(Storage.ComposeRequest composeRequest)

Sends a compose request.

Accepts an optional userProject BlobTargetOption option which defines the project id to assign operational costs.

Example of composing two blobs.


 String bucketName = "my-unique-bucket";
 String blobName = "my-blob-name";
 String sourceBlob1 = "source_blob_1";
 String sourceBlob2 = "source_blob_2";
 BlobId blobId = BlobId.of(bucketName, blobName);
 BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
 ComposeRequest request = ComposeRequest.newBuilder()
     .setTarget(blobInfo)
     .addSource(sourceBlob1)
     .addSource(sourceBlob2)
     .build();
 Blob blob = storage.compose(request);
 
Parameter
NameDescription
composeRequestStorage.ComposeRequest
Returns
TypeDescription
Blob

the composed blob

copy(Storage.CopyRequest copyRequest)

public abstract CopyWriter copy(Storage.CopyRequest copyRequest)

Sends a copy request. This method copies both blob's data and information. To override source blob's information supply a BlobInfo to the CopyRequest using either Storage.CopyRequest.Builder#setTarget(BlobInfo, Storage.BlobTargetOption...) or Storage.CopyRequest.Builder#setTarget(BlobInfo, Iterable).

This method returns a CopyWriter object for the provided CopyRequest. If source and destination objects share the same location and storage class the source blob is copied with one request and CopyWriter#getResult() immediately returns, regardless of the CopyRequest#megabytesCopiedPerChunk parameter. If source and destination have different location or storage class CopyWriter#getResult() might issue multiple RPC calls depending on blob's size.

Example of copying a blob.


 String bucketName = "my-unique-bucket";
 String blobName = "my-blob-name";
 String copyBlobName = "copy_blob_name";
 CopyRequest request = CopyRequest.newBuilder()
     .setSource(BlobId.of(bucketName, blobName))
     .setTarget(BlobId.of(bucketName, copyBlobName))
     .build();
 Blob blob = storage.copy(request).getResult();
 

Example of copying a blob in chunks.


 String bucketName = "my-unique-bucket";
 String blobName = "my-blob-name";
 String copyBlobName = "copy_blob_name";
 CopyRequest request = CopyRequest.newBuilder()
     .setSource(BlobId.of(bucketName, blobName))
     .setTarget(BlobId.of(bucketName, copyBlobName))
     .build();
 CopyWriter copyWriter = storage.copy(request);
 while (!copyWriter.isDone()) {
   copyWriter.copyChunk();
 }
 Blob blob = copyWriter.getResult();
 

Example of rotating the encryption key of a blob.


 String bucketName = "my-unique-bucket";
 String blobName = "my-blob-name";
 String oldEncryptionKey = "old_encryption_key";
 String newEncryptionKey = "new_encryption_key";
 BlobId blobId = BlobId.of(bucketName, blobName);
 CopyRequest request = CopyRequest.newBuilder()
     .setSource(blobId)
     .setSourceOptions(BlobSourceOption.decryptionKey(oldEncryptionKey))
     .setTarget(blobId, BlobTargetOption.encryptionKey(newEncryptionKey))
     .build();
 Blob blob = storage.copy(request).getResult();
 

See Also: Rewrite

Parameter
NameDescription
copyRequestStorage.CopyRequest
Returns
TypeDescription
CopyWriter

a CopyWriter object that can be used to get information on the newly created blob or to complete the copy if more than one RPC request is needed

create(BlobInfo blobInfo, byte[] content, Storage.BlobTargetOption[] options)

public abstract Blob create(BlobInfo blobInfo, byte[] content, Storage.BlobTargetOption[] options)

Creates a new blob. Direct upload is used to upload content. For large content, #writer is recommended as it uses resumable upload. MD5 and CRC32C hashes of content are computed and used for validating transferred data. Accepts an optional userProject BlobGetOption option which defines the project id to assign operational costs. The content type is detected from the blob name if not explicitly set.

Example of creating a blob from a byte array:


 String bucketName = "my-unique-bucket";
 String blobName = "my-blob-name";
 BlobId blobId = BlobId.of(bucketName, blobName);
 BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
 Blob blob