Class StorageClient (2.4.5)

public class StorageClient implements BackgroundResource

Service Description: ## API Overview and Naming Syntax

The Cloud Storage gRPC API allows applications to read and write data through the abstractions of buckets and objects. For a description of these abstractions please see https://cloud.google.com/storage/docs.

Resources are named as follows: - Projects are referred to as they are defined by the Resource Manager API, using strings like projects/123456 or projects/my-string-id. - Buckets are named using string names of the form: projects/{project}/buckets/{bucket} For globally unique buckets, _ may be substituted for the project. - Objects are uniquely identified by their name along with the name of the bucket they belong to, as separate strings in this API. For example:

ReadObjectRequest { bucket: 'projects/_/buckets/my-bucket' object: 'my-object' } Note that object names can contain / characters, which are treated as any other character (no special directory semantics).

This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:


 try (StorageClient storageClient = StorageClient.create()) {
   BucketName name = BucketName.of("[PROJECT]", "[BUCKET]");
   storageClient.deleteBucket(name);
 }
 

Note: close() needs to be called on the StorageClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().

The surface of this class includes several types of Java methods for each of the API's methods:

  1. A "flattened" method. With this type of method, the fields of the request type have been converted into function parameters. It may be the case that not all fields are available as parameters, and not every API method will have a flattened method entry point.
  2. A "request object" method. This type of method only takes one parameter, a request object, which must be constructed before the call. Not every API method will have a request object method.
  3. A "callable" method. This type of method takes no parameters and returns an immutable API callable object, which can be used to initiate calls to the service.

See the individual methods for example code.

Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.

This class can be customized by passing in a custom instance of StorageSettings to create(). For example:

To customize credentials:


 StorageSettings storageSettings =
     StorageSettings.newBuilder()
         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
         .build();
 StorageClient storageClient = StorageClient.create(storageSettings);
 

To customize the endpoint:


 StorageSettings storageSettings = StorageSettings.newBuilder().setEndpoint(myEndpoint).build();
 StorageClient storageClient = StorageClient.create(storageSettings);
 

Please refer to the GitHub repository's samples for more quickstart code snippets.

Inheritance

java.lang.Object > StorageClient

Implements

BackgroundResource

Static Methods

create()

public static final StorageClient create()

Constructs an instance of StorageClient with default settings.

Returns
Type Description
StorageClient
Exceptions
Type Description
IOException

create(StorageSettings settings)

public static final StorageClient create(StorageSettings settings)

Constructs an instance of StorageClient, using the given settings. The channels are created based on the settings passed in, or defaults for any settings that are not set.

Parameter
Name Description
settings StorageSettings
Returns
Type Description
StorageClient
Exceptions
Type Description
IOException

create(StorageStub stub)

public static final StorageClient create(StorageStub stub)

Constructs an instance of StorageClient, using the given stub for making calls. This is for advanced usage - prefer using create(StorageSettings).

Parameter
Name Description
stub StorageStub
Returns
Type Description
StorageClient

Constructors

StorageClient(StorageSettings settings)

protected StorageClient(StorageSettings settings)

Constructs an instance of StorageClient, using the given settings. This is protected so that it is easy to make a subclass, but otherwise, the static factory methods should be preferred.

Parameter
Name Description
settings StorageSettings

StorageClient(StorageStub stub)

protected StorageClient(StorageStub stub)
Parameter
Name Description
stub StorageStub

Methods

awaitTermination(long duration, TimeUnit unit)

public boolean awaitTermination(long duration, TimeUnit unit)
Parameters
Name Description
duration long
unit TimeUnit
Returns
Type Description
boolean
Exceptions
Type Description
InterruptedException

close()

public final void close()

composeObject(ComposeObjectRequest request)

public final Object composeObject(ComposeObjectRequest request)

Concatenates a list of existing objects into a new object in the same bucket.

Sample code:


 try (StorageClient storageClient = StorageClient.create()) {
   ComposeObjectRequest request =
       ComposeObjectRequest.newBuilder()
           .setDestination(Object.newBuilder().build())
           .addAllSourceObjects(new ArrayList
Parameter
Name Description
request ComposeObjectRequest

The request object containing all of the parameters for the API call.

Returns
Type Description
Object

composeObjectCallable()

public final UnaryCallable<ComposeObjectRequest,Object> composeObjectCallable()

Concatenates a list of existing objects into a new object in the same bucket.

Sample code:


 try (StorageClient storageClient = StorageClient.create()) {
   ComposeObjectRequest request =
       ComposeObjectRequest.newBuilder()
           .setDestination(Object.newBuilder().build())
           .addAllSourceObjects(new ArrayList
Returns
Type Description
UnaryCallable<ComposeObjectRequest,Object>

createBucket(CreateBucketRequest request)

public final Bucket createBucket(CreateBucketRequest request)

Creates a new bucket.

Sample code:


 try (StorageClient storageClient = StorageClient.create()) {
   CreateBucketRequest request =
       CreateBucketRequest.newBuilder()
           .setParent(ProjectName.of("[PROJECT]").toString())
           .setBucket(Bucket.newBuilder().build())
           .setBucketId("bucketId-1603305307")
           .setPredefinedAcl(PredefinedBucketAcl.forNumber(0))
           .setPredefinedDefaultObjectAcl(PredefinedObjectAcl.forNumber(0))
           .build();
   Bucket response = storageClient.createBucket(request);
 }
 
Parameter
Name Description
request CreateBucketRequest

The request object containing all of the parameters for the API call.

Returns
Type Description