Google Cloud Storage v1 API - Class StorageClient (4.13.0)

public abstract class StorageClient : IDisposable

Reference documentation and code samples for the Google Cloud Storage v1 API class StorageClient.

Abstract class providing operations for Google Cloud Storage.

Inheritance

object > StorageClient

Implements

IDisposable

Derived Types

Namespace

Google.Cloud.Storage.V1

Assembly

Google.Cloud.Storage.V1.dll

Remarks

This abstract class is provided to enable testability while permitting additional operations to be added in the future. See Create(GoogleCredential, EncryptionKey) and CreateAsync(GoogleCredential, EncryptionKey) to construct instances; alternatively, you can construct a StorageClientImpl directly from a StorageService.

All instance methods declared in this class are virtual, with an implementation which simply throws NotImplementedException. All these methods are overridden in StorageClientImpl.

This class implements IDisposable; however, the Dispose() method only requires calling if many short-lived instances of StorageClient are being created. Most code would be expected to create a single StorageClient instance, and use this instance throughout the lifetime of the application. In this case, Dispose need not be called.

Properties

EncryptionKey

public virtual EncryptionKey EncryptionKey { get; }

The customer-supplied encryption key used by default for all relevant object-based operations. This can be overridden in call-specific options. This will never return null in the built-in implementation; if no encryption key is specified, None is returned.

Property Value
Type Description
EncryptionKey

Service

public virtual StorageService Service { get; }

The underlying storage service object used by this client.

Property Value
Type Description
StorageService
Remarks

The StorageClient class only provides convenience operations built on top of an existing service object. Any more complex operations which are not supported by this wrapper may wish to use the same service object as the wrapper, in order to take advantage of its configuration (for authentication, application naming etc).

Methods

ClearBucketLabels(string, ModifyBucketLabelsOptions)

public virtual IDictionary<string, string> ClearBucketLabels(string bucket, ModifyBucketLabelsOptions options = null)

Clears all labels on a bucket.

Parameters
Name Description
bucket string

The name of the bucket. Must not be null.

options ModifyBucketLabelsOptions

The options for the operation. May be null, in which case defaults will be supplied.

Returns
Type Description
IDictionarystringstring

A dictionary with the labels on the bucket before they were cleared.

Example
StorageClient client = StorageClient.Create();

IDictionary<string, string> oldLabels = client.ClearBucketLabels(bucketName);
Console.WriteLine($"Number of labels before clearing: {oldLabels.Count}");

ClearBucketLabelsAsync(string, ModifyBucketLabelsOptions, CancellationToken)

public virtual Task<IDictionary<string, string>> ClearBucketLabelsAsync(string bucket, ModifyBucketLabelsOptions options = null, CancellationToken cancellationToken = default)

Clears all labels on a bucket asynchronously.

Parameters
Name Description
bucket string

The name of the bucket. Must not be null.

options ModifyBucketLabelsOptions

The options for the operation. May be null, in which case defaults will be supplied.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns
Type Description
TaskIDictionarystringstring

A dictionary with the labels on the bucket before they were cleared.

Example

See ClearBucketLabels for a synchronous example.

CopyObject(string, string, string, string, CopyObjectOptions)

public virtual Object CopyObject(string sourceBucket, string sourceObjectName, string destinationBucket, string destinationObjectName, CopyObjectOptions options = null)

Creates a copy of an object synchronously, potentially to a different bucket. This method uses the rewriteObject underlying API operation for more flexibility and reliability.

Parameters
Name Description
sourceBucket string

The name of the bucket containing the object to copy. Must not be null.

sourceObjectName string

The name of the object to copy within the bucket. Must not be null.

destinationBucket string

The name of the bucket to copy the object to. Must not be null.

destinationObjectName string

The name of the object within the destination bucket. Must not be null.

options CopyObjectOptions

Additional options for the copy operation. May be null, in which case appropriate defaults will be used.

Returns
Type Description
Object

The Object representation of the new storage object resulting from the copy.

Example
StorageClient client = StorageClient.Create();
string sourceName = "greetings/hello.txt";
string destinationName = "copy.txt";

// This method actually uses the "rewrite" API operation, for added reliability
// when copying large objects across locations, storage classes or encryption keys.
client.CopyObject(sourceBucket, sourceName, destinationBucket, destinationName);

CopyObjectAsync(string, string, string, string, CopyObjectOptions, CancellationToken)

public virtual Task<Object> CopyObjectAsync(string sourceBucket, string sourceObjectName, string destinationBucket = null, string destinationObjectName = null, CopyObjectOptions options = null, CancellationToken cancellationToken = default)

Creates a copy of an object asynchronously, potentially to a different bucket. This method uses the rewriteObject underlying API operation for more flexibility and reliability.

Parameters
Name Description
sourceBucket string

The name of the bucket containing the object to copy. Must not be null.

sourceObjectName string

The name of the object to copy within the bucket. Must not be null.

destinationBucket string

The name of the bucket to copy the object to. Must not be null.

destinationObjectName string

The name of the object within the destination bucket. Must not be null.

options CopyObjectOptions

Additional options for the copy operation. May be null, in which case appropriate defaults will be used.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns
Type Description
TaskObject

A task representing the asynchronous operation, with a result returning the Object representation of the new storage object resulting from the copy.

Remarks

The fact that destinationBucket and destinationObjectName are optional is a mistake. The default value of null for these parameters is invalid for this method, meaning that any call which doesn't specify the parameters explicitly will fail. Making these parameters required would be a compile-time breaking change; this will be implemented in the next major version of this library.

Example

See CopyObject for a synchronous example.

Create()

public static StorageClient Create()

Synchronously creates a StorageClient using application default credentials. For any non-default values, please use StorageClientBuilder.

Returns
Type Description
StorageClient

Create(GoogleCredential, EncryptionKey)

public static StorageClient Create(GoogleCredential credential = null, EncryptionKey encryptionKey = null)

Synchronously creates a StorageClient, using application default credentials if no credentials are specified.

Parameters
Name Description
credential GoogleCredential

Optional GoogleCredential. If this is non-null, default scopes are applied to it (for credential types that support scoping), overriding any existing scopes in the credentials. To use scoped credentials without any modifications, create a StorageClientBuilder and set the Credential property.

encryptionKey EncryptionKey

Optional EncryptionKey to use for all relevant object-based operations by default. May be null.

Returns
Type Description
StorageClient

The created StorageClient.

Remarks

If a credential is supplied, the default scopes are applied to it (for credential types that support scoping), overriding any existing scopes in the credentials. To use scoped credentials without any modifications, create a StorageClientBuilder and set the Credential property.

CreateAsync()

public static Task<StorageClient> CreateAsync()

Asynchronously creates a StorageClient using application default credentials. For any non-default values, please use StorageClientBuilder.

Returns
Type Description
TaskStorageClient

The task representing the created StorageClient.

CreateAsync(GoogleCredential, EncryptionKey)

public static Task<StorageClient> CreateAsync(GoogleCredential credential = null, EncryptionKey encryptionKey = null)

Asynchronously creates a StorageClient, using application default credentials if no credentials are specified.

Parameters
Name Description
credential GoogleCredential

Optional GoogleCredential. If this is non-null, default scopes are applied to it (for credential types that support scoping), overriding any existing scopes in the credentials. To use scoped credentials without any modifications, create a StorageClientBuilder and set the Credential property.

encryptionKey EncryptionKey

Optional EncryptionKey to use for all relevant object-based operations by default. May be null.

Returns
Type Description
TaskStorageClient

The task representing the created StorageClient.

Remarks

If a credential is supplied, the default scopes are applied to it (for credential types that support scoping), overriding any existing scopes in the credentials. To use scoped credentials without any modifications, create a StorageClientBuilder and set the Credential property.

CreateBucket(string, Bucket, CreateBucketOptions)

public virtual Bucket CreateBucket(string projectId, Bucket bucket, CreateBucketOptions options = null)

Creates a storage bucket synchronously.

Parameters
Name Description
projectId string

The ID of the project to create the buckets in. Must not be null.

bucket Bucket

The resource representation of the bucket to create. Must not be null, and must have a name.

options CreateBucketOptions

Additional options for the operation. May be null, in which case appropriate defaults will be used.

Returns
Type Description
Bucket

The resource representation of the newly-created bucket.

Example

See CreateBucket for an example using an alternative overload.

CreateBucket(string, string, CreateBucketOptions)

public virtual Bucket CreateBucket(string projectId, string bucket, CreateBucketOptions options = null)

Creates a storage bucket synchronously.

Parameters
Name Description
projectId string

The ID of the project to create the buckets in. Must not be null.

bucket string

The name of the bucket to create. Must not be null.

options CreateBucketOptions

Additional options for the operation. May be null, in which case appropriate defaults will be used.

Returns
Type Description
Bucket

The resource representation of the newly-created bucket.

Example
var client = StorageClient.Create();

// GCS bucket names must be globally unique
var bucketName = Guid.NewGuid().ToString();

// Bucket defined in Google.Apis.Storage.v1.Data namespace
var bucket = client.CreateBucket(projectId, bucketName);

CreateBucketAsync(string, Bucket, CreateBucketOptions, CancellationToken)

public virtual Task<Bucket> CreateBucketAsync(string projectId, Bucket bucket, CreateBucketOptions options = null, CancellationToken cancellationToken = default)

Creates a storage bucket asynchronously.

Parameters
Name Description
projectId string

The ID of the project to create the buckets in. Must not be null.

bucket Bucket

The resource representation of the bucket to create. Must not be null, and must have a name.

options CreateBucketOptions

Additional options for the operation. May be null, in which case appropriate defaults will be used.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns
Type Description
TaskBucket

A task representing the asynchronous operation, with a result returning the resource representation of the newly-created bucket.

Example

See CreateBucket for a synchronous example.

CreateBucketAsync(string, string, CreateBucketOptions, CancellationToken)

public virtual Task<Bucket> CreateBucketAsync(string projectId, string bucket, CreateBucketOptions options = null, CancellationToken cancellationToken = default)

Creates a storage bucket asynchronously.

Parameters
Name Description
projectId string

The ID of the project to create the buckets in. Must not be null.

bucket string

The name of the bucket to create. Must not be null.

options CreateBucketOptions

Additional options for the operation. May be null, in which case appropriate defaults will be used.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns
Type Description
TaskBucket

A task representing the asynchronous operation, with a result returning the resource representation of the newly-created bucket.

Example

See CreateBucket for a synchronous example.

CreateHmacKey(string, string, CreateHmacKeyOptions)

public virtual HmacKey CreateHmacKey(string projectId, string serviceAccountEmail, CreateHmacKeyOptions options = null)

Synchronously creates a new HMAC key associated with a service account.

Parameters
Name Description
projectId string

The project ID in which to create the HMAC key. Must not be null.

serviceAccountEmail string

The service account to associate with the HMAC key. Must not be null.

options CreateHmacKeyOptions

Additional options for the operation. May be null, in which case appropriate defaults will be used.

Returns
Type Description
HmacKey

The new HMAC key, including the secret information in Secret. This secret is only ever returned when creating a key.

CreateHmacKeyAsync(string, string, CreateHmacKeyOptions, CancellationToken)

public virtual Task<HmacKey> CreateHmacKeyAsync(string projectId, string serviceAccountEmail, CreateHmacKeyOptions options = null, CancellationToken cancellationToken = default)

Asynchronously creates a new HMAC key associated with a service account.

Parameters
Name Description
projectId string

The project ID in which to create the HMAC key. Must not be null.

serviceAccountEmail string

The service account to associate with the HMAC key. Must not be null.

options CreateHmacKeyOptions

Additional options for the operation. May be null, in which case appropriate defaults will be used.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns
Type Description
TaskHmacKey

A task representing the asynchronous operation, with a result returning the the new HMAC key, including the secret information in Secret. This secret is only ever returned when creating a key.

CreateNotification(string, Notification, CreateNotificationOptions)

public virtual Notification CreateNotification(string bucket, Notification notification, CreateNotificationOptions options = null)

Creates a notification configuration.

Parameters
Name Description
bucket string

The bucket for which to create the notification configuration. Must not be null.

notification Notification

The notification configuration to create. Must not be null.

options CreateNotificationOptions

The options for the operation. May be null, in which case defaults will be supplied.

Returns
Type Description
Notification

The Notification representation of the newly-created notification configuration.

Example
TopicName topicName = new TopicName(projectId, topicId);
StorageClient client = StorageClient.Create();
Notification notification = new Notification
{
    Topic = $"//pubsub.googleapis.com/{topicName}",
    PayloadFormat = "JSON_API_V1"
};
notification = client.CreateNotification(bucket, notification);
Console.WriteLine($"Created notification ID: {notification.Id}");

CreateNotificationAsync(string, Notification, CreateNotificationOptions, CancellationToken)

public virtual Task<Notification> CreateNotificationAsync(string bucket, Notification notification, CreateNotificationOptions options = null, CancellationToken cancellationToken = default)

Asynchronously creates a notification configuration.

Parameters
Name Description
bucket string

The bucket for which to create the notification configuration. Must not be null.

notification Notification

The notification configuration to create. Must not be null.

options CreateNotificationOptions

The options for the operation. May be null, in which case defaults will be supplied.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns
Type Description
TaskNotification

A task representing the asynchronous operation, with a result returning the Notification representation of the newly-created notification configuration.

Example

See CreateNotification for a synchronous example.

CreateObjectUploader(Object, Stream, UploadObjectOptions)

public virtual ObjectsResource.InsertMediaUpload CreateObjectUploader(Object destination, Stream source, UploadObjectOptions options = null)

Creates an instance which is capable of starting a resumable upload for an object.

Parameters
Name Description
destination Object

Object to create or update. Must not be null, and must have the name and bucket populated.

source Stream

The stream to read the data from. Must not be null.

options UploadObjectOptions

Additional options for the upload. May be null, in which case appropriate defaults will be used.

Returns
Type Description
ObjectsResourceInsertMediaUpload

The ObjectsResource.InsertMediaUpload which can be used to upload the object.

See Also

CreateObjectUploader(string, string, string, Stream, UploadObjectOptions)

public virtual ObjectsResource.InsertMediaUpload CreateObjectUploader(string bucket, string objectName, string contentType, Stream source, UploadObjectOptions options = null)

Creates an instance which is capable of starting a resumable upload for an object.

Parameters
Name Description
bucket string

The name of the bucket containing the object. Must not be null.

objectName string

The name of the object within the bucket. Must not be null.

contentType string

The content type of the object. This should be a MIME type such as "text/html" or "application/octet-stream". May be null.

source Stream

The stream to read the data from. Must not be null.

options UploadObjectOptions

Additional options for the upload. May be null, in which case appropriate defaults will be used.

Returns
Type Description
ObjectsResourceInsertMediaUpload

The ObjectsResource.InsertMediaUpload which can be used to upload the object.

See Also

CreateUnauthenticated()

public static StorageClient CreateUnauthenticated()

Creates a StorageClient with no credentials. This can only be used in limited situations (where authentication isn't required), primarily downloading public data.

Returns
Type Description
StorageClient

The created StorageClient.

CreateUrlSigner()

public UrlSigner CreateUrlSigner()

Creates a URL signer based on this client, that uses the same credential as this client, as well as defaulting to this client's URI scheme, host and port.

Returns
Type Description
UrlSigner
Exceptions
Type Description
InvalidOperationException

If the credential used by this client is not compatible with UrlSigner. See UrlSigner's documentation for more information on compatible credentials.

DeleteBucket(Bucket, DeleteBucketOptions)

public virtual void DeleteBucket(Bucket bucket, DeleteBucketOptions options = null)

Deletes a bucket synchronously.

Parameters
Name Description
bucket Bucket

Bucket to delete. Must not be null, and must have the name populated.

options DeleteBucketOptions

Additional options for the operation. May be null, in which case appropriate defaults will be used.

Example

See DeleteBucket for an example using an alternative overload.

DeleteBucket(string, DeleteBucketOptions)

public virtual void DeleteBucket(string bucket, DeleteBucketOptions options = null)

Deletes a bucket synchronously.

Parameters
Name Description
bucket string

The name of the bucket to delete. Must not be null.

options DeleteBucketOptions

Additional options for the operation. May be null, in which case appropriate defaults will be used.

Example
var client = StorageClient.Create();

client.DeleteBucket(bucketName);

DeleteBucketAsync(Bucket, DeleteBucketOptions, CancellationToken)

public virtual Task DeleteBucketAsync(Bucket bucket, DeleteBucketOptions options = null, CancellationToken cancellationToken = default)

Deletes bucket asynchronously.

Parameters
Name Description
bucket Bucket

Bucket to delete. Must not be null, and must have the name populated.

options DeleteBucketOptions

Additional options for the operation. May be null, in which case appropriate defaults will be used.

cancellationToken