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.
Implements
IDisposableDerived Types
Namespace
Google.Cloud.Storage.V1Assembly
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 |
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. |
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. |
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. |
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. |
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.
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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 |
Google.Apis.Storage.v1.ObjectsResourceInsertMediaUpload | The ObjectsResource.InsertMediaUpload which can be used to upload the object. |
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 |
Google.Apis.Storage.v1.ObjectsResourceInsertMediaUpload | The ObjectsResource.InsertMediaUpload which can be used to upload the object. |
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. |
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. |
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 | CancellationToken The token to monitor for cancellation requests. |
Returns | |
---|---|
Type | Description |
Task | A task representing the asynchronous operation. |
See DeleteBucket for a synchronous example.
DeleteBucketAsync(string, DeleteBucketOptions, CancellationToken)
public virtual Task DeleteBucketAsync(string bucket, DeleteBucketOptions options = null, CancellationToken cancellationToken = default)
Deletes a bucket asynchronously.
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. |
cancellationToken | CancellationToken The token to monitor for cancellation requests. |
Returns | |
---|---|
Type | Description |
Task | A task representing the asynchronous operation. |
See DeleteBucket for a synchronous example.
DeleteHmacKey(string, string, DeleteHmacKeyOptions)
public virtual void DeleteHmacKey(string projectId, string accessId, DeleteHmacKeyOptions options = null)
Synchronously deletes an HMAC key, which must already have been deactivated.
Parameters | |
---|---|
Name | Description |
projectId | string The ID of the project containing the HMAC key to delete. |
accessId | string The ID of the HMAC key to delete. Must not be null. |
options | DeleteHmacKeyOptions Additional options for the operation. May be null, in which case appropriate defaults will be used. |
DeleteHmacKeyAsync(string, string, DeleteHmacKeyOptions, CancellationToken)
public virtual Task DeleteHmacKeyAsync(string projectId, string accessId, DeleteHmacKeyOptions options = null, CancellationToken cancellationToken = default)
Asynchronously deletes an HMAC key, which must already have been deactivated.
Parameters | |
---|---|
Name | Description |
projectId | string The ID of the project containing the HMAC key to delete. |
accessId | string The ID of the HMAC key to delete. Must not be null. |
options | DeleteHmacKeyOptions 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 |
Task | A task representing the asynchronous operation. |
DeleteNotification(string, string, DeleteNotificationOptions)
public virtual void DeleteNotification(string bucket, string notificationId, DeleteNotificationOptions options = null)
Deletes a notification configuration.
Parameters | |
---|---|
Name | Description |
bucket | string The bucket associated with the notification configuration. Must not be null. |
notificationId | string ID of the notification configuration to delete. Must not be null. |
options | DeleteNotificationOptions The options for the operation. May be null, in which case defaults will be supplied. |
StorageClient client = StorageClient.Create();
client.DeleteNotification(bucket, notificationId);
DeleteNotificationAsync(string, string, DeleteNotificationOptions, CancellationToken)
public virtual Task DeleteNotificationAsync(string bucket, string notificationId, DeleteNotificationOptions options = null, CancellationToken cancellationToken = default)
Asynchronously deletes a notification configuration.
Parameters | |
---|---|
Name | Description |
bucket | string The bucket associated with the notification configuration. Must not be null. |
notificationId | string ID of the notification configuration to delete. Must not be null. |
options | DeleteNotificationOptions 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 |
Task | A task representing the asynchronous operation. |
See DeleteNotification for a synchronous example.
DeleteObject(Object, DeleteObjectOptions)
public virtual void DeleteObject(Object obj, DeleteObjectOptions options = null)
Deletes a version of the specified object synchronously.
Parameters | |
---|---|
Name | Description |
obj | Object Object to delete. Must not be null, and must have the name and bucket populated. |
options | DeleteObjectOptions Additional options for the operation. May be null, in which case appropriate defaults will be used. |
By default this will delete the latest version of the object, but this can be
controlled using Generation. Note that any generation information
contained within obj
is ignored; the generation to delete is only controlled
via options
. For buckets that support
multiple versions, implicitly deleting the latest version only archives
it so the object is still available and can be listed by specifying Versions.
If the version is explicitly specified, the object is permanently deleted.
If the bucket containing the object supports versioning, after this operation completes successfully there may still be another version of the same object. In buckets which do not support versioning, this operation will permanently delete the object.
If the request attempts to delete an object that does not exist or a specific version that does not exist, this counts as a failure with an HTTP status code of 404.
See DeleteObject for an example using an alternative overload.
DeleteObject(string, string, DeleteObjectOptions)
public virtual void DeleteObject(string bucket, string objectName, DeleteObjectOptions options = null)
Deletes a version of the specified object synchronously.
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. |
options | DeleteObjectOptions Additional options for the operation. May be null, in which case appropriate defaults will be used. |
By default this will delete the latest version of the object, but this can be controlled using Generation. For buckets that support multiple versions, implicitly deleting the latest version only archives it so the object is still available and can be listed by specifying Versions. If the version is explicitly specified, the object is permanently deleted.
If the bucket containing the object supports versioning, after this operation completes successfully there may still be another version of the same object. In buckets which do not support versioning, this operation will permanently delete the object.
If the request attempts to delete an object that does not exist or a specific version that does not exist, this counts as a failure with an HTTP status code of 404.
var client = StorageClient.Create();
var objectName = "places/deleteme.txt";
client.DeleteObject(bucketName, objectName);
DeleteObjectAsync(Object, DeleteObjectOptions, CancellationToken)
public virtual Task DeleteObjectAsync(Object obj, DeleteObjectOptions options = null, CancellationToken cancellationToken = default)
Deletes a version of the specified object asynchronously.
Parameters | |
---|---|
Name | Description |
obj | Object Object to delete. Must not be null, and must have the name and bucket populated. |
options | DeleteObjectOptions 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 |
Task | A task representing the asynchronous operation. |
By default this will delete the latest version of the object, but this can be
controlled using Generation. Note that any generation information
contained within obj
is ignored; the generation to delete is only controlled
via options
. For buckets that support
multiple versions, implicitly deleting the latest version only archives
it so the object is still available and can be listed by specifying Versions.
If the version is explicitly specified, the object is permanently deleted.
If the bucket containing the object supports versioning, after this operation completes successfully there may still be another version of the same object. In buckets which do not support versioning, this operation will permanently delete the object.
If the request attempts to delete an object that does not exist or a specific version that does not exist, this counts as a failure with an HTTP status code of 404.
See DeleteObject for a synchronous example.
DeleteObjectAsync(string, string, DeleteObjectOptions, CancellationToken)
public virtual Task DeleteObjectAsync(string bucket, string objectName, DeleteObjectOptions options = null, CancellationToken cancellationToken = default)
Deletes a version of the specified object asynchronously.
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. |
options | DeleteObjectOptions 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 |
Task | A task representing the asynchronous operation. |
By default this will delete the latest version of the object, but this can be controlled using Generation. For buckets that support multiple versions, implicitly deleting the latest version only archives it so the object is still available and can be listed by specifying Versions. If the version is explicitly specified, the object is permanently deleted.
If the bucket containing the object supports versioning, after this operation completes successfully there may still be another version of the same object. In buckets which do not support versioning, this operation will permanently delete the object.
If the request attempts to delete an object that does not exist or a specific version that does not exist, this counts as a failure with an HTTP status code of 404.
See DeleteObject for a synchronous example.
Dispose()
public virtual void Dispose()
Dispose of this instance. See the StorageClient remarks on when this should be called.
DownloadObject(Object, Stream, DownloadObjectOptions, IProgress<IDownloadProgress>)
public virtual Object DownloadObject(Object source, Stream destination, DownloadObjectOptions options = null, IProgress<IDownloadProgress> progress = null)
Downloads the data for an object from storage synchronously, into a specified stream.
Parameters | |
---|---|
Name | Description |
source | Object Source object to download the data from. Must not be null. |
destination | Stream The stream to write the data into. Must not be null. |
options | DownloadObjectOptions Additional options for the download. May be null, in which case appropriate defaults will be used. |
progress | IProgressIDownloadProgress Progress reporter for the download. May be null. |
Returns | |
---|---|
Type | Description |
Object | An Object representation of the metadata for the object that has been downloaded into the stream. This metadata is not the complete metadata for the object; it's just the information provided in headers while downloading. Additionally, the CRC32C hash is only the hash of the data downloaded; if the options specify a range which does not encompass the whole object, this will not be the same as the CRC32C hash of the complete object. |
The generation number within source
is ignored by this method.
To download a specific generation, use Generation.
See DownloadObject for an example using an alternative overload.
DownloadObject(string, string, Stream, DownloadObjectOptions, IProgress<IDownloadProgress>)
public virtual Object DownloadObject(string bucket, string objectName, Stream destination, DownloadObjectOptions options = null, IProgress<IDownloadProgress> progress = null)
Downloads the data for an object from storage synchronously, into a specified stream.
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. |
destination | Stream The stream to write the data into. Must not be null. |
options | DownloadObjectOptions Additional options for the download. May be null, in which case appropriate defaults will be used. |
progress | IProgressIDownloadProgress Progress reporter for the download. May be null. |
Returns | |
---|---|
Type | Description |
Object | An Object representation of the metadata for the object that has been downloaded into the stream. This metadata is not the complete metadata for the object; it's just the information provided in headers while downloading. Additionally, the CRC32C hash is only the hash of the data downloaded; if the options specify a range which does not encompass the whole object, this will not be the same as the CRC32C hash of the complete object. |
var client = StorageClient.Create();
var source = "greetings/hello.txt";
var destination = "hello.txt";
using (var stream = File.Create(destination))
{
// IDownloadProgress defined in Google.Apis.Download namespace
var progress = new Progress<IDownloadProgress>(
p => Console.WriteLine($"bytes: {p.BytesDownloaded}, status: {p.Status}")
);
// Download source object from bucket to local file system
client.DownloadObject(bucketName, source, stream, null, progress);
}
DownloadObjectAsync(Object, Stream, DownloadObjectOptions, CancellationToken, IProgress<IDownloadProgress>)
public virtual Task<Object> DownloadObjectAsync(Object source, Stream destination, DownloadObjectOptions options = null, CancellationToken cancellationToken = default, IProgress<IDownloadProgress> progress = null)
Downloads the data for an object from storage asynchronously, into a specified stream.
Parameters | |
---|---|
Name | Description |
source | Object Source object to download the data from. Must not be null. |
destination | Stream The stream to write the data into. Must not be null. |
options | DownloadObjectOptions Additional options for the download. May be null, in which case appropriate defaults will be used. |
cancellationToken | CancellationToken The token to monitor for cancellation requests. |
progress | IProgressIDownloadProgress Progress reporter for the download. May be null. |
Returns | |
---|---|
Type | Description |
TaskObject | A task representing the asynchronous operation. The result of the task is an Object representation of the metadata for the object that has been downloaded into the stream. This metadata is not the complete metadata for the object; it's just the information provided in headers while downloading. Additionally, the CRC32C hash is only the hash of the data downloaded; if the options specify a range which does not encompass the whole object, this will not be the same as the CRC32C hash of the complete object. |
The generation number within source
is ignored by this method.
To download a specific generation, use Generation.
See DownloadObject for a synchronous example.
DownloadObjectAsync(string, string, Stream, DownloadObjectOptions, CancellationToken, IProgress<IDownloadProgress>)
public virtual Task<Object> DownloadObjectAsync(string bucket, string objectName, Stream destination, DownloadObjectOptions options = null, CancellationToken cancellationToken = default, IProgress<IDownloadProgress> progress = null)
Downloads the data for an object from storage asynchronously, into a specified stream.
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. |
destination | Stream The stream to write the data into. Must not be null. |
options | DownloadObjectOptions Additional options for the download. May be null, in which case appropriate defaults will be used. |
cancellationToken | CancellationToken The token to monitor for cancellation requests. |
progress | IProgressIDownloadProgress Progress reporter for the download. May be null. |
Returns | |
---|---|
Type | Description |
TaskObject | A task representing the asynchronous operation. The result of the task is an Object representation of the metadata for the object that has been downloaded into the stream. This metadata is not the complete metadata for the object; it's just the information provided in headers while downloading. Additionally, the CRC32C hash is only the hash of the data downloaded; if the options specify a range which does not encompass the whole object, this will not be the same as the CRC32C hash of the complete object. |
See DownloadObject for a synchronous example.
GetBucket(string, GetBucketOptions)
public virtual Bucket GetBucket(string bucket, GetBucketOptions options = null)
Fetches the information about a bucket synchronously.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
options | GetBucketOptions Additional options for the fetch operation. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
Bucket | The Bucket representation of the storage bucket. |
var client = StorageClient.Create();
var bucket = client.GetBucket(bucketName);
Console.WriteLine($"Name: {bucket.Name}");
Console.WriteLine($"TimeCreated: {bucket.TimeCreatedDateTimeOffset}");
GetBucketAsync(string, GetBucketOptions, CancellationToken)
public virtual Task<Bucket> GetBucketAsync(string bucket, GetBucketOptions options = null, CancellationToken cancellationToken = default)
Fetches the information about a bucket asynchronously.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
options | GetBucketOptions Additional options for the fetch 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 Bucket representation of the storage bucket. |
See GetBucket for a synchronous example.
GetBucketIamPolicy(string, GetBucketIamPolicyOptions)
public virtual Policy GetBucketIamPolicy(string bucket, GetBucketIamPolicyOptions options = null)
Fetches the IAM policy information about a bucket synchronously.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
options | GetBucketIamPolicyOptions Additional options for the fetch operation. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
Policy | The Policy representation of the storage bucket IAM policy. |
StorageClient client = StorageClient.Create();
Policy policy = client.GetBucketIamPolicy(bucketName);
foreach (Policy.BindingsData binding in policy.Bindings)
{
Console.WriteLine($"Role: {binding.Role}");
foreach (var permission in binding.Members)
{
Console.WriteLine($" {permission}");
}
}
GetBucketIamPolicyAsync(string, GetBucketIamPolicyOptions, CancellationToken)
public virtual Task<Policy> GetBucketIamPolicyAsync(string bucket, GetBucketIamPolicyOptions options = null, CancellationToken cancellationToken = default)
Fetches the IAM policy information about a bucket asynchronously.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
options | GetBucketIamPolicyOptions Additional options for the fetch operation. May be null, in which case appropriate defaults will be used. |
cancellationToken | CancellationToken The token to monitor for cancellation requests. |
Returns | |
---|---|
Type | Description |
TaskPolicy | A task representing the asynchronous operation, with a result returning the Policy representation of the storage bucket IAM policy. |
See GetBucketIamPolicy for a synchronous example.
GetHmacKey(string, string, GetHmacKeyOptions)
public virtual HmacKeyMetadata GetHmacKey(string projectId, string accessId, GetHmacKeyOptions options = null)
Synchronously fetches metadata for the specified HMAC key.
Parameters | |
---|---|
Name | Description |
projectId | string The project containing the HMAC key. Must not be null. |
accessId | string The ID of the HMAC key. Must not be null. |
options | GetHmacKeyOptions Additional options for the operation. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
HmacKeyMetadata | The HMAC key metadata |
GetHmacKeyAsync(string, string, GetHmacKeyOptions, CancellationToken)
public virtual Task<HmacKeyMetadata> GetHmacKeyAsync(string projectId, string accessId, GetHmacKeyOptions options = null, CancellationToken cancellationToken = default)
Asynchronously fetches metadata for the specified HMAC key.
Parameters | |
---|---|
Name | Description |
projectId | string The project containing the HMAC key. Must not be null. |
accessId | string The ID of the HMAC key. Must not be null. |
options | GetHmacKeyOptions 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 |
TaskHmacKeyMetadata | A task representing the asynchronous operation, with a result returning the HMAC key metadata. |
GetNotification(string, string, GetNotificationOptions)
public virtual Notification GetNotification(string bucket, string notificationId, GetNotificationOptions options = null)
Fetches a notification configuration.
Parameters | |
---|---|
Name | Description |
bucket | string The bucket associated with the notification configuration. Must not be null. |
notificationId | string ID of the notification configuration to fetch. Must not be null. |
options | GetNotificationOptions The options for the operation. May be null, in which case defaults will be supplied. |
Returns | |
---|---|
Type | Description |
Notification | The Notification representation of the notification configuration. |
StorageClient client = StorageClient.Create();
Notification notification = client.GetNotification(bucket, notificationId);
Console.WriteLine($"ID: {notification.Id}");
Console.WriteLine($"Payload format: {notification.PayloadFormat}");
Console.WriteLine($"Topic: {notification.Topic}");
Console.WriteLine($"Prefix: {notification.ObjectNamePrefix}");
Console.WriteLine($"Event types: {string.Join(",", notification.EventTypes ?? new string[0])}");
GetNotificationAsync(string, string, GetNotificationOptions, CancellationToken)
public virtual Task<Notification> GetNotificationAsync(string bucket, string notificationId, GetNotificationOptions options = null, CancellationToken cancellationToken = default)
Asynchronously fetches a notification configuration.
Parameters | |
---|---|
Name | Description |
bucket | string The bucket associated with the notification configuration. Must not be null. |
notificationId | string ID of the notification configuration to fetch. Must not be null. |
options | GetNotificationOptions 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 notification configuration. |
See GetNotification for a synchronous example.
GetObject(string, string, GetObjectOptions)
public virtual Object GetObject(string bucket, string objectName, GetObjectOptions options = null)
Fetches the information about an object synchronously.
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. |
options | GetObjectOptions Additional options for the fetch operation. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
Object | The Object representation of the storage object. |
This does not retrieve the content of the object itself. Use DownloadObject(string, string, Stream, DownloadObjectOptions, IProgress<IDownloadProgress>) to download the content.
var client = StorageClient.Create();
var name = "greetings/hello.txt";
var obj = client.GetObject(bucketName, name);
Console.WriteLine($"Name: {obj.Name}");
Console.WriteLine($"Size: {obj.Size}");
Console.WriteLine($"ContentType: {obj.ContentType}");
Console.WriteLine($"TimeCreated: {obj.TimeCreatedDateTimeOffset}");
GetObjectAsync(string, string, GetObjectOptions, CancellationToken)
public virtual Task<Object> GetObjectAsync(string bucket, string objectName, GetObjectOptions options = null, CancellationToken cancellationToken = default)
Fetches the information about an object asynchronously.
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. |
options | GetObjectOptions Additional options for the fetch 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 storage object. |
This does not retrieve the content of the object itself. Use DownloadObjectAsync(string, string, Stream, DownloadObjectOptions, CancellationToken, IProgress<IDownloadProgress>) to download the content.
See GetObject for a synchronous example.
GetStorageServiceAccountEmail(string, GetStorageServiceAccountEmailOptions)
public virtual string GetStorageServiceAccountEmail(string projectId, GetStorageServiceAccountEmailOptions options = null)
Retrieves the service account email address that Google Cloud Storage uses for operations on other services such as Google Cloud Pub/Sub.
Parameters | |
---|---|
Name | Description |
projectId | string ID of the project to retrieve the service account for. Must not be null. |
options | GetStorageServiceAccountEmailOptions The options for the operation. May be null, in which case defaults will be supplied. |
Returns | |
---|---|
Type | Description |
string | The email address of the service account. |
StorageClient client = StorageClient.Create();
string serviceAccountEmail = client.GetStorageServiceAccountEmail(projectId);
Console.WriteLine(serviceAccountEmail);
GetStorageServiceAccountEmailAsync(string, GetStorageServiceAccountEmailOptions, CancellationToken)
public virtual Task<string> GetStorageServiceAccountEmailAsync(string projectId, GetStorageServiceAccountEmailOptions options = null, CancellationToken cancellationToken = default)
Asynchronously retrieves the service account email address that Google Cloud Storage uses for operations on other services such as Google Cloud Pub/Sub.
Parameters | |
---|---|
Name | Description |
projectId | string ID of the project to retrieve the service account for. Must not be null. |
options | GetStorageServiceAccountEmailOptions 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 |
Taskstring | A task representing the asynchronous operation, with a result returning the email address of the service account. |
See GetStorageServiceAccountEmail for a synchronous example.
InitiateUploadSessionAsync(Object, long?, UploadObjectOptions, CancellationToken)
public virtual Task<Uri> InitiateUploadSessionAsync(Object destination, long? contentLength, UploadObjectOptions options = null, CancellationToken cancellationToken = default)
Initiates an upload session, optionally specifying the length of the content to be uploaded. The resulting URI can be used with CreateFromUploadUri(Uri, Stream, ResumableUploadOptions).
Parameters | |
---|---|
Name | Description |
destination | Object Object to create or update. Must not be null, and must have the name and bucket populated. |
contentLength | long The length of the content to upload later. This may be null, in which case any length of content may be uploaded. If the value is non-null, it must be strictly positive (not zero), and the content uploaded later must be exactly this length. |
options | UploadObjectOptions Additional options for the upload. May be null, in which case appropriate defaults will be used. |
cancellationToken | CancellationToken The token to monitor for cancellation requests. |
Returns | |
---|---|
Type | Description |
TaskUri | A task representing the asynchronous operation, with a result returning the Uri to use in order to upload the content. |
InitiateUploadSessionAsync(string, string, string, long?, UploadObjectOptions, CancellationToken)
public virtual Task<Uri> InitiateUploadSessionAsync(string bucket, string objectName, string contentType, long? contentLength, UploadObjectOptions options = null, CancellationToken cancellationToken = default)
Initiates an upload session, optionally specifying the length of the content to be uploaded. The resulting URI can be used with CreateFromUploadUri(Uri, Stream, ResumableUploadOptions).
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. |
contentLength | long The length of the content to upload later. This may be null, in which case any length of content may be uploaded. If the value is non-null, it must be strictly positive (not zero), and the content uploaded later must be exactly this length. |
options | UploadObjectOptions Additional options for the upload. May be null, in which case appropriate defaults will be used. |
cancellationToken | CancellationToken The token to monitor for cancellation requests. |
Returns | |
---|---|
Type | Description |
TaskUri | A task representing the asynchronous operation, with a result returning the Uri to use in order to upload the content. |
ListBuckets(string, ListBucketsOptions)
public virtual PagedEnumerable<Buckets, Bucket> ListBuckets(string projectId, ListBucketsOptions options = null)
Lists the buckets for a given project, synchronously but lazily.
Parameters | |
---|---|
Name | Description |
projectId | string The ID of the project to list the buckets from. Must not be null. |
options | ListBucketsOptions The options for the operation. May be null, in which case defaults will be supplied. |
Returns | |
---|---|
Type | Description |
PagedEnumerableGoogle.Apis.Storage.v1.Data.BucketsBucket | A sequence of buckets within the project. |
This method fetches the buckets lazily, making requests to the underlying service for a page of results at a time, as required. No network requests are made until the returned sequence is enumerated. This means that any exception due to an invalid request will be deferred until that time. Callers should be prepared for exceptions to be thrown while enumerating the results. In addition to failures due to invalid requests, network or service failures can cause exceptions even after the first results have been returned.
var client = StorageClient.Create();
// List all buckets associated with a project
var buckets = client.ListBuckets(projectId);
ListBucketsAsync(string, ListBucketsOptions)
public virtual PagedAsyncEnumerable<Buckets, Bucket> ListBucketsAsync(string projectId, ListBucketsOptions options = null)
Asynchronously lists the buckets in a given project.
Parameters | |
---|---|
Name | Description |
projectId | string The ID of the project to list the buckets from. Must not be null. |
options | ListBucketsOptions The options for the operation. May be null, in which case defaults will be supplied. |
Returns | |
---|---|
Type | Description |
PagedAsyncEnumerableGoogle.Apis.Storage.v1.Data.BucketsBucket | An asynchronous sequence of buckets. |
This method fetches the buckets lazily, making requests to the underlying service for a page of results at a time, as required. No network requests are made until the returned sequence is enumerated. This means that any exception due to an invalid request will be deferred until that time. Callers should be prepared for exceptions to be thrown while enumerating the results. In addition to failures due to invalid requests, network or service failures can cause exceptions even after the first results have been returned.
See ListBuckets for a synchronous example.
ListHmacKeys(string, string, ListHmacKeysOptions)
public virtual PagedEnumerable<HmacKeysMetadata, HmacKeyMetadata> ListHmacKeys(string projectId, string serviceAccountEmail = null, ListHmacKeysOptions options = null)
Lists the HMAC keys in a given project, synchronously but lazily, optionally filtering by service account email address.
Parameters | |
---|---|
Name | Description |
projectId | string The project containing the HMAC keys. Must not be null. |
serviceAccountEmail | string The service account email address to list keys for. May be null, in which case all HMAC keys associated with the project are returned. |
options | ListHmacKeysOptions Additional options for the operation. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
PagedEnumerableGoogle.Apis.Storage.v1.Data.HmacKeysMetadataHmacKeyMetadata | A sequence of HMAC keys within the project. |
This method fetches the buckets lazily, making requests to the underlying service for a page of results at a time, as required. No network requests are made until the returned sequence is enumerated. This means that any exception due to an invalid request will be deferred until that time. Callers should be prepared for exceptions to be thrown while enumerating the results. In addition to failures due to invalid requests, network or service failures can cause exceptions even after the first results have been returned.
ListHmacKeysAsync(string, string, ListHmacKeysOptions)
public virtual PagedAsyncEnumerable<HmacKeysMetadata, HmacKeyMetadata> ListHmacKeysAsync(string projectId, string serviceAccountEmail = null, ListHmacKeysOptions options = null)
Lists the HMAC keys in a given project, asynchronously and lazily, optionally filtering by service account email address.
Parameters | |
---|---|
Name | Description |
projectId | string The project containing the HMAC keys. Must not be null. |
serviceAccountEmail | string The service account email address to list keys for. May be null, in which case all HMAC keys associated with the project are returned. |
options | ListHmacKeysOptions Additional options for the operation. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
PagedAsyncEnumerableGoogle.Apis.Storage.v1.Data.HmacKeysMetadataHmacKeyMetadata | An asynchronous sequence of HMAC keys within the project. |
This method fetches the buckets lazily, making requests to the underlying service for a page of results at a time, as required. No network requests are made until the returned sequence is enumerated. This means that any exception due to an invalid request will be deferred until that time. Callers should be prepared for exceptions to be thrown while enumerating the results. In addition to failures due to invalid requests, network or service failures can cause exceptions even after the first results have been returned.
ListNotifications(string, ListNotificationsOptions)
public virtual IReadOnlyList<Notification> ListNotifications(string bucket, ListNotificationsOptions options = null)
Lists notification configurations associated with a bucket.
Parameters | |
---|---|
Name | Description |
bucket | string The bucket for which to list associated notification configurations. Must not be null. |
options | ListNotificationsOptions The options for the operation. May be null, in which case defaults will be supplied. |
Returns | |
---|---|
Type | Description |
IReadOnlyListNotification | A list of notification configurations associated with the specified bucket. This method never returns null. |
StorageClient client = StorageClient.Create();
IReadOnlyList<Notification> notifications = client.ListNotifications(bucket);
foreach (Notification notification in notifications)
{
Console.WriteLine($"{notification.Id}: Topic={notification.Topic}; Prefix={notification.ObjectNamePrefix}");
}
ListNotificationsAsync(string, ListNotificationsOptions, CancellationToken)
public virtual Task<IReadOnlyList<Notification>> ListNotificationsAsync(string bucket, ListNotificationsOptions options = null, CancellationToken cancellationToken = default)
Asynchronously lists notification configurations associated with a bucket.
Parameters | |
---|---|
Name | Description |
bucket | string The bucket for which to list associated notification configurations. Must not be null. |
options | ListNotificationsOptions 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 |
TaskIReadOnlyListNotification | A task representing the asynchronous operation, with a result returning the list of notification configurations associated with the specified bucket. The result is never null. |
See ListNotifications for a synchronous example.
ListObjects(string, string, ListObjectsOptions)
public virtual PagedEnumerable<Objects, Object> ListObjects(string bucket, string prefix = null, ListObjectsOptions options = null)
Lists the objects in a given bucket, synchronously but lazily.
Parameters | |
---|---|
Name | Description |
bucket | string The bucket to list the objects from. Must not be null. |
prefix | string The prefix to match. Only objects with names that start with this string will be returned. This parameter may be null or empty, in which case no filtering is performed. |
options | ListObjectsOptions The options for the operation. May be null, in which case defaults will be supplied. |
Returns | |
---|---|
Type | Description |
PagedEnumerableGoogle.Apis.Storage.v1.Data.ObjectsObject | A sequence of objects in the specified bucket. |
This method fetches the objects lazily, making requests to the underlying service for a page of results at a time, as required. No network requests are made until the returned sequence is enumerated. This means that any exception due to an invalid request will be deferred until that time. Callers should be prepared for exceptions to be thrown while enumerating the results. In addition to failures due to invalid requests, network or service failures can cause exceptions even after the first results have been returned.
var client = StorageClient.Create();
// List only objects with a name starting with "greet"
var objects = client.ListObjects(bucketName, "greet");
ListObjectsAsync(string, string, ListObjectsOptions)
public virtual PagedAsyncEnumerable<Objects, Object> ListObjectsAsync(string bucket, string prefix = null, ListObjectsOptions options = null)
Asynchronously lists the objects in a given bucket.
Parameters | |
---|---|
Name | Description |
bucket | string The bucket to list the objects from. Must not be null. |
prefix | string The prefix to match. Only objects with names that start with this string will be returned. This parameter may be null or empty, in which case no filtering is performed. |
options | ListObjectsOptions The options for the operation. May be null, in which case defaults will be supplied. |
Returns | |
---|---|
Type | Description |
PagedAsyncEnumerableGoogle.Apis.Storage.v1.Data.ObjectsObject | An asynchronous sequence of objects in the specified bucket. |
This method fetches the objects lazily, making requests to the underlying service for a page of results at a time, as required. No network requests are made until the returned sequence is enumerated. This means that any exception due to an invalid request will be deferred until that time. Callers should be prepared for exceptions to be thrown while enumerating the results. In addition to failures due to invalid requests, network or service failures can cause exceptions even after the first results have been returned.
See ListObjects for a synchronous example.
LockBucketRetentionPolicy(string, long, LockBucketRetentionPolicyOptions)
public virtual void LockBucketRetentionPolicy(string bucket, long metageneration, LockBucketRetentionPolicyOptions options = null)
Locks the retention policy of a bucket. This is a one-way process: once a retention policy is locked, it cannot be shortened, removed or unlocked, although it can be increased in duration. The lock persists until the bucket is deleted.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket whose retention policy should be locked. Must not be null. |
metageneration | long The current metageneration of the bucket, used to validate that the bucket version to be locked is the intended one. |
options | LockBucketRetentionPolicyOptions The options for the operation. May be null, in which case defaults will be supplied. |
var client = StorageClient.Create();
var bucketName = Guid.NewGuid().ToString();
// Create a bucket and set its retention policy
Bucket bucket = client.CreateBucket(projectId, bucketName);
// Retention period is specified in seconds.
bucket.RetentionPolicy = new Bucket.RetentionPolicyData { RetentionPeriod = 60 };
bucket = client.UpdateBucket(bucket);
// Once the retention policy is locked, it can't be shortened, removed or unlocked.
// It will be locked until the bucket is deleted.
client.LockBucketRetentionPolicy(bucketName, bucket.Metageneration.Value);
LockBucketRetentionPolicyAsync(string, long, LockBucketRetentionPolicyOptions, CancellationToken)
public virtual Task LockBucketRetentionPolicyAsync(string bucket, long metageneration, LockBucketRetentionPolicyOptions options = null, CancellationToken cancellationToken = default)
Asynchronously locks the retention policy of a bucket. This is a one-way process: once a retention policy is locked, it cannot be shortened, removed or unlocked, although it can be increased in duration. The lock persists until the bucket is deleted.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket whose retention policy should be locked. Must not be null. |
metageneration | long The current metageneration of the bucket, used to validate that the bucket version to be locked is the intended one. |
options | LockBucketRetentionPolicyOptions 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 |
Task | A task representing the asynchronous operation. |
See LockBucketRetentionPolicy for a synchronous example.
ModifyBucketLabels(string, IDictionary<string, string>, ModifyBucketLabelsOptions)
public virtual IDictionary<string, string> ModifyBucketLabels(string bucket, IDictionary<string, string> labels, ModifyBucketLabelsOptions options = null)
Sets or removes one or more labels on a bucket.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
labels | IDictionarystringstring The labels to set. Must contain at least one entry; keys must not be null, but values may be (indicating label removal). |
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 same keys as |
Each entry in labels
is treated as a label to set or remove. If the value is null,
it will be removed from the bucket; otherwise, it will be set/added. Labels which do not have an entry
in the dictionary will be ignored.
StorageClient client = StorageClient.Create();
string now = DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss", CultureInfo.InvariantCulture);
IDictionary<string, string> labelChanges = new Dictionary<string, string>
{
{ "label1", "new_value_1_" + now },
{ "label2", "new_value_2_" + now },
};
IDictionary<string, string> oldValues = client.ModifyBucketLabels(bucketName, labelChanges);
Console.WriteLine("Old values for changed labels:");
foreach (KeyValuePair<string, string> entry in oldValues)
{
Console.WriteLine($" {entry.Key}: {entry.Value}");
}
Console.WriteLine("All labels:");
IDictionary<string, string> allLabels = client.GetBucket(bucketName).Labels ?? new Dictionary<string, string>();
foreach (KeyValuePair<string, string> entry in allLabels)
{
Console.WriteLine($" {entry.Key}: {entry.Value}");
}
ModifyBucketLabelsAsync(string, IDictionary<string, string>, ModifyBucketLabelsOptions, CancellationToken)
public virtual Task<IDictionary<string, string>> ModifyBucketLabelsAsync(string bucket, IDictionary<string, string> labels, ModifyBucketLabelsOptions options = null, CancellationToken cancellationToken = default)
Sets or removes one or more labels on a bucket asynchronously.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
labels | IDictionarystringstring The labels to set. Must contain at least one entry; keys must not be null, but values may be (indicating label removal). |
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 same keys as |
Each entry in labels
is treated as a label to set or remove. If the value is null,
it will be removed from the bucket; otherwise, it will be set/added. Labels which do not have an entry
in the dictionary will be ignored.
See ModifyBucketLabels for a synchronous example.
PatchBucket(Bucket, PatchBucketOptions)
public virtual Bucket PatchBucket(Bucket bucket, PatchBucketOptions options = null)
Patches the metadata for a storage bucket synchronously.
Parameters | |
---|---|
Name | Description |
bucket | Bucket Bucket to update. Must not be null, and must have a populated |
options | PatchBucketOptions Additional options for the update. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
Bucket | The Bucket representation of the updated storage bucket. |
var client = StorageClient.Create();
// Note: no fetching of the bucket beforehand. We only specify the values we want
// to change.
var bucket = new Bucket
{
Name = bucketName,
Website = new Bucket.WebsiteData
{
MainPageSuffix = "index.html",
NotFoundPage = "404.html"
}
};
client.PatchBucket(bucket);
PatchBucketAsync(Bucket, PatchBucketOptions, CancellationToken)
public virtual Task<Bucket> PatchBucketAsync(Bucket bucket, PatchBucketOptions options = null, CancellationToken cancellationToken = default)
Patches the metadata for a storage bucket asynchronously.
Parameters | |
---|---|
Name | Description |
bucket | Bucket Bucket to update. Must not be null, and must have a populated |
options | PatchBucketOptions Additional options for the update. 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 Bucket representation of the bucket storage bucket. |
See PatchBucket for a synchronous example.
PatchObject(Object, PatchObjectOptions)
public virtual Object PatchObject(Object obj, PatchObjectOptions options = null)
Patches the metadata for an object in storage synchronously.
Parameters | |
---|---|
Name | Description |
obj | Object Object to update. Must not be null, and must have populated |
options | PatchObjectOptions Additional options for the update. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
Object | The Object representation of the updated storage object. |
var client = StorageClient.Create();
var name = "patch-example.txt";
var content = Encoding.UTF8.GetBytes("hello, world");
var obj = new Apis.Storage.v1.Data.Object
{
Bucket = bucketName,
Name = name,
ContentType = "text/json", // Deliberately incorrect
Metadata = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
}
};
client.UploadObject(obj, new MemoryStream(content));
var patchObject = new Apis.Storage.v1.Data.Object
{
// The bucket and name are required; everything else is optional.
// Only specified properties will be updated.
Bucket = bucketName,
Name = name,
ContentType = "text/plain",
Metadata = new Dictionary<string, string>
{
{ "key2", "updated-value2" },
{ "key3", "value3" }
}
};
client.PatchObject(patchObject);
PatchObjectAsync(Object, PatchObjectOptions, CancellationToken)
public virtual Task<Object> PatchObjectAsync(Object obj, PatchObjectOptions options = null, CancellationToken cancellationToken = default)
Patches the metadata for an object in storage asynchronously.
Parameters | |
---|---|
Name | Description |
obj | Object Object to update. Must not be null, and must have populated |
options | PatchObjectOptions Additional options for the update. 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 updated storage object. |
See PatchObject for a synchronous example.
RemoveBucketLabel(string, string, ModifyBucketLabelsOptions)
public virtual string RemoveBucketLabel(string bucket, string labelName, ModifyBucketLabelsOptions options = null)
Removes a label from a bucket, if it previously existed. It is not an error to attempt to remove a label that doesn't already exist.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
labelName | string The name of the label to remove. Must not be null. |
options | ModifyBucketLabelsOptions The options for the operation. May be null, in which case defaults will be supplied. |
Returns | |
---|---|
Type | Description |
string | The previous value of the label, or null if the label did not previously exist. |
This method is implemented by creating a single-element dictionary which is passed to ModifyBucketLabels(string, IDictionary<string, string>, ModifyBucketLabelsOptions).
StorageClient client = StorageClient.Create();
string oldValue = client.RemoveBucketLabel(bucketName, "label");
Console.WriteLine($"Old value: {oldValue}");
// Verify the label is now gone...
Bucket bucket = client.GetBucket(bucketName);
string fetchedValue = null;
bucket.Labels?.TryGetValue("label", out fetchedValue);
Console.WriteLine($"Fetched value: {fetchedValue}");
RemoveBucketLabelAsync(string, string, ModifyBucketLabelsOptions, CancellationToken)
public virtual Task<string> RemoveBucketLabelAsync(string bucket, string labelName, ModifyBucketLabelsOptions options = null, CancellationToken cancellationToken = default)
Removes a label from a bucket, if it previously existed, asynchronously. It is not an error to attempt to remove a label that doesn't already exist.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
labelName | string The name of the label to remove. 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 |
Taskstring | The previous value of the label, or null if the label did not previously exist. |
This method is implemented by creating a single-element dictionary which is passed to ModifyBucketLabelsAsync(string, IDictionary<string, string>, ModifyBucketLabelsOptions, CancellationToken).
See RemoveBucketLabel for a synchronous example.
RestoreObject(string, string, long, RestoreObjectOptions)
public virtual Object RestoreObject(string bucket, string objectName, long generation, RestoreObjectOptions options = null)
Restores a soft-deleted 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 to restore. Must not be null. |
generation | long The specific revision of the object to restore. |
options | RestoreObjectOptions Additional options for the restore operation. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
Object | The Object representation of the restored Storage object. |
RestoreObjectAsync(string, string, long, RestoreObjectOptions, CancellationToken)
public virtual Task<Object> RestoreObjectAsync(string bucket, string objectName, long generation, RestoreObjectOptions options = null, CancellationToken cancellationToken = default)
Restores a soft-deleted 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. |
generation | long The specific revision of the object to restore. |
options | RestoreObjectOptions Additional options for the restore 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 restored Storage object. |
SetBucketIamPolicy(string, Policy, SetBucketIamPolicyOptions)
public virtual Policy SetBucketIamPolicy(string bucket, Policy policy, SetBucketIamPolicyOptions options = null)
Updates the IAM policy information about a bucket synchronously.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
policy | Policy The new IAM policy for the bucket. Must not be null. |
options | SetBucketIamPolicyOptions Additional options for the operation. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
Policy | The Policy representation of the resulting storage bucket IAM policy. |
// Create a new bucket and an empty file within it
StorageClient client = StorageClient.Create();
Bucket bucket = client.CreateBucket(projectId, bucketName);
var obj = client.UploadObject(bucketName, "empty.txt", "text/plain", new MemoryStream());
// Demonstrate that without authentication, we can't download the object
HttpClient httpClient = new HttpClient();
HttpResponseMessage response1 = await httpClient.GetAsync(obj.MediaLink);
Console.WriteLine($"Response code before setting policy: {response1.StatusCode}");
// Fetch the current IAM policy, and modify it in memory to allow all users
// to view objects.
Policy policy = client.GetBucketIamPolicy(bucketName);
string role = "roles/storage.objectViewer";
Policy.BindingsData binding = policy.Bindings
.Where(b => b.Role == role)
.FirstOrDefault();
if (binding == null)
{
binding = new Policy.BindingsData { Role = role, Members = new List<string>() };
policy.Bindings.Add(binding);
}
binding.Members.Add("allUsers");
// Update the IAM policy on the bucket.
client.SetBucketIamPolicy(bucketName, policy);
// Wait 10 seconds to allow the policy to be applied.
// (Normally the policy change is visible pretty much immediately, but
// 10 seconds makes this very reliable.)
await Task.Delay(TimeSpan.FromSeconds(10));
// Download the object again: this time the response should be OK
HttpResponseMessage response2 = await httpClient.GetAsync(obj.MediaLink);
Console.WriteLine($"Response code after setting policy: {response2.StatusCode}");
SetBucketIamPolicyAsync(string, Policy, SetBucketIamPolicyOptions, CancellationToken)
public virtual Task<Policy> SetBucketIamPolicyAsync(string bucket, Policy policy, SetBucketIamPolicyOptions options = null, CancellationToken cancellationToken = default)
Updates the IAM policy information about a bucket asynchronously.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
policy | Policy The new IAM policy for the bucket. Must not be null. |
options | SetBucketIamPolicyOptions Additional options for the fetch operation. May be null, in which case appropriate defaults will be used. |
cancellationToken | CancellationToken The token to monitor for cancellation requests. |
Returns | |
---|---|
Type | Description |
TaskPolicy | A task representing the asynchronous operation, with a result returning the Policy representation of the resulting storage bucket IAM policy. |
See SetBucketIamPolicy for a synchronous example.
SetBucketLabel(string, string, string, ModifyBucketLabelsOptions)
public virtual string SetBucketLabel(string bucket, string labelName, string labelValue, ModifyBucketLabelsOptions options = null)
Sets the value for a single label on a bucket. The label will be added if it does not exist, or updated if it already exists.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
labelName | string The name of the label to set. Must not be null. |
labelValue | string The value of the label to set. Must not be null. |
options | ModifyBucketLabelsOptions The options for the operation. May be null, in which case defaults will be supplied. |
Returns | |
---|---|
Type | Description |
string | The previous value of the label, or null if the label did not previously exist. |
This method is implemented by creating a single-element dictionary which is passed to ModifyBucketLabels(string, IDictionary<string, string>, ModifyBucketLabelsOptions).
StorageClient client = StorageClient.Create();
string now = DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss", CultureInfo.InvariantCulture);
string newValue = "new_value_" + now;
string oldValue = client.SetBucketLabel(bucketName, "label", newValue);
Console.WriteLine($"Old value: {oldValue}");
// Verify the label is now correct...
Bucket bucket = client.GetBucket(bucketName);
string fetchedValue = bucket.Labels?["label"];
Console.WriteLine($"Fetched value: {fetchedValue}");
SetBucketLabelAsync(string, string, string, ModifyBucketLabelsOptions, CancellationToken)
public virtual Task<string> SetBucketLabelAsync(string bucket, string labelName, string labelValue, ModifyBucketLabelsOptions options = null, CancellationToken cancellationToken = default)
Sets the value for a single label on a bucket asynchronously. The label will be added if it does not exist, or updated if it already exists.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
labelName | string The name of the label to set. Must not be null. |
labelValue | string The value of the label to set. 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 |
Taskstring | The previous value of the label, or null if the label did not previously exist. |
This method is implemented by creating a single-element dictionary which is passed to ModifyBucketLabelsAsync(string, IDictionary<string, string>, ModifyBucketLabelsOptions, CancellationToken).
See SetBucketLabel for a synchronous example.
TestBucketIamPermissions(string, IEnumerable<string>, TestBucketIamPermissionsOptions)
public virtual IList<string> TestBucketIamPermissions(string bucket, IEnumerable<string> permissions, TestBucketIamPermissionsOptions options = null)
Tests the IAM permissions for a bucket synchronously.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
permissions | IEnumerablestring The permissions to test. Must not be null or empty. |
options | TestBucketIamPermissionsOptions Additional options for the operation. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
IListstring | The permissions held by the caller. |
StorageClient client = StorageClient.Create();
IList<string> permissions = client.TestBucketIamPermissions(bucketName,
new[] { "storage.buckets.get", "storage.objects.list" });
Console.WriteLine("Permissions held:");
foreach (string permission in permissions)
{
Console.WriteLine($" {permission}");
}
TestBucketIamPermissionsAsync(string, IEnumerable<string>, TestBucketIamPermissionsOptions, CancellationToken)
public virtual Task<IList<string>> TestBucketIamPermissionsAsync(string bucket, IEnumerable<string> permissions, TestBucketIamPermissionsOptions options = null, CancellationToken cancellationToken = default)
Tests the IAM policy information about a bucket asynchronously.
Parameters | |
---|---|
Name | Description |
bucket | string The name of the bucket. Must not be null. |
permissions | IEnumerablestring The permissions to test. Must not be null or empty. |
options | TestBucketIamPermissionsOptions 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 |
TaskIListstring | A task representing the asynchronous operation, with a result returning the permissions held by the caller. |
See TestBucketIamPermissions for a synchronous example.
UpdateBucket(Bucket, UpdateBucketOptions)
public virtual Bucket UpdateBucket(Bucket bucket, UpdateBucketOptions options = null)
Updates the metadata for a storage bucket synchronously.
Parameters | |
---|---|
Name | Description |
bucket | Bucket Bucket to update. Must not be null, and must have a populated |
options | UpdateBucketOptions Additional options for the update. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
Bucket | The Bucket representation of the updated storage bucket. |
If no preconditions are explicitly set in options
, the metageneration of bucket
is used as a precondition for the update, unless ForceNoPreconditions is
set to true
.
var client = StorageClient.Create();
var bucket = client.GetBucket(bucketName);
bucket.Website = new Bucket.WebsiteData
{
MainPageSuffix = "index.html",
NotFoundPage = "404.html"
};
client.UpdateBucket(bucket);
UpdateBucketAsync(Bucket, UpdateBucketOptions, CancellationToken)
public virtual Task<Bucket> UpdateBucketAsync(Bucket bucket, UpdateBucketOptions options = null, CancellationToken cancellationToken = default)
Updates the metadata for storage bucket asynchronously.
Parameters | |
---|---|
Name | Description |
bucket | Bucket Bucket to update. Must not be null, and must have a populated |
options | UpdateBucketOptions Additional options for the update. 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 Bucket representation of the storage bucket. |
If no preconditions are explicitly set in options
, the metageneration of bucket
is used as a precondition for the update, unless ForceNoPreconditions is
set to true
.
See UpdateBucket for a synchronous example.
UpdateHmacKey(HmacKeyMetadata, UpdateHmacKeyOptions)
public virtual HmacKeyMetadata UpdateHmacKey(HmacKeyMetadata key, UpdateHmacKeyOptions options = null)
Synchronously updates an HMAC key's metadata.
Parameters | |
---|---|
Name | Description |
key | HmacKeyMetadata The key to update. Must not be null, and the ProjectId and AccessId properties must be set. |
options | UpdateHmacKeyOptions Additional options for the operation. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
HmacKeyMetadata | The updated key metadata. |
UpdateHmacKeyAsync(HmacKeyMetadata, UpdateHmacKeyOptions, CancellationToken)
public virtual Task<HmacKeyMetadata> UpdateHmacKeyAsync(HmacKeyMetadata key, UpdateHmacKeyOptions options = null, CancellationToken cancellationToken = default)
Asynchronously updates an HMAC key's metadata.
Parameters | |
---|---|
Name | Description |
key | HmacKeyMetadata The key to update. Must not be null, and the ProjectId and AccessId properties must be set. |
options | UpdateHmacKeyOptions 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 |
TaskHmacKeyMetadata | A task representing the asynchronous operation, with a result returning the updated key metadata. |
UpdateObject(Object, UpdateObjectOptions)
public virtual Object UpdateObject(Object obj, UpdateObjectOptions options = null)
Updates the metadata for an object in storage synchronously.
Parameters | |
---|---|
Name | Description |
obj | Object Object to update. Must not be null, and must have populated |
options | UpdateObjectOptions Additional options for the update. May be null, in which case appropriate defaults will be used. |
Returns | |
---|---|
Type | Description |
Object | The Object representation of the updated storage object. |
If no preconditions are explicitly set in options
, the generation and
metageneration of obj
are used as a precondition for the update,
unless ForceNoPreconditions is
set to true
.
var client = StorageClient.Create();
var name = "update-example.txt";
var content = Encoding.UTF8.GetBytes("hello, world");
var obj = new Apis.Storage.v1.Data.Object
{
Bucket = bucketName,
Name = name,
ContentType = "text/json",
Metadata = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
}
};
obj = client.UploadObject(obj, new MemoryStream(content));
obj.Metadata.Remove("key1");
obj.Metadata["key2"] = "updated-value2";
obj.Metadata["key3"] = "value3";
obj.ContentType = "text/plain";
client.UpdateObject(obj);
UpdateObjectAsync(Object, UpdateObjectOptions, CancellationToken)
public virtual Task<Object> UpdateObjectAsync(Object obj, UpdateObjectOptions options = null, CancellationToken cancellationToken = default)
Updates the metadata for an object in storage asynchronously.
Parameters | |
---|---|
Name | Description |
obj | Object Object to update. Must not be null, and must have populated |
options | UpdateObjectOptions Additional options for the update. 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 storage object. |
If no preconditions are explicitly set in options
, the generation and
metageneration of obj
are used as a precondition for the update,
unless ForceNoPreconditions is
set to true
.
See UpdateObject for a synchronous example.
UploadObject(Object, Stream, UploadObjectOptions, IProgress<IUploadProgress>)
public virtual Object UploadObject(Object destination, Stream source, UploadObjectOptions options = null, IProgress<IUploadProgress> progress = null)
Uploads the data for an object in storage synchronously, from a specified stream.
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. |
progress | IProgressIUploadProgress Progress reporter for the upload. May be null. |
Returns | |
---|---|
Type | Description |
Object | The Object representation of the uploaded object. |
See UploadObject for an example using an alternative overload.
UploadObject(string, string, string, Stream, UploadObjectOptions, IProgress<IUploadProgress>)
public virtual Object UploadObject(string bucket, string objectName, string contentType, Stream source, UploadObjectOptions options = null, IProgress<IUploadProgress> progress = null)
Uploads the data for an object in storage synchronously, from a specified stream.
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. |
progress | IProgressIUploadProgress Progress reporter for the upload. May be null. |
Returns | |
---|---|
Type | Description |
Object | The Object representation of the uploaded object. |
var client = StorageClient.Create();
var source = "world.txt";
var destination = "places/world.txt";
var contentType = "text/plain";
using (var stream = File.OpenRead(source))
{
// IUploadProgress defined in Google.Apis.Upload namespace
var progress = new Progress<IUploadProgress>(
p => Console.WriteLine($"bytes: {p.BytesSent}, status: {p.Status}")
);
// var acl = PredefinedAcl.PublicRead // public
var acl = PredefinedObjectAcl.AuthenticatedRead; // private
var options = new UploadObjectOptions { PredefinedAcl = acl };
var obj = client.UploadObject(bucketName, destination, contentType, stream, options, progress);
}
UploadObjectAsync(Object, Stream, UploadObjectOptions, CancellationToken, IProgress<IUploadProgress>)
public virtual Task<Object> UploadObjectAsync(Object destination, Stream source, UploadObjectOptions options = null, CancellationToken cancellationToken = default, IProgress<IUploadProgress> progress = null)
Uploads the data for an object in storage asynchronously, from a specified stream.
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. |
cancellationToken | CancellationToken The token to monitor for cancellation requests. |
progress | IProgressIUploadProgress Progress reporter for the upload. May be null. |
Returns | |
---|---|
Type | Description |
TaskObject | A task representing the asynchronous operation, with a result returning the Object representation of the uploaded object. |
See UploadObject for a synchronous example.
UploadObjectAsync(string, string, string, Stream, UploadObjectOptions, CancellationToken, IProgress<IUploadProgress>)
public virtual Task<Object> UploadObjectAsync(string bucket, string objectName, string contentType, Stream source, UploadObjectOptions options = null, CancellationToken cancellationToken = default, IProgress<IUploadProgress> progress = null)
Uploads the data for an object in storage asynchronously, from a specified stream.
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. |
cancellationToken | CancellationToken The token to monitor for cancellation requests. |
progress | IProgressIUploadProgress Progress reporter for the upload. May be null. |
Returns | |
---|---|
Type | Description |
TaskObject | A task representing the asynchronous operation, with a result returning the Object representation of the uploaded object. |
See UploadObject for a synchronous example.