/* * MinIO .NET Library for Amazon S3 Compatible Cloud Storage, * (C) 2017-2021 MinIO, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System.Diagnostics.CodeAnalysis; using System.Net; using System.Reactive.Linq; using Minio.ApiEndpoints; using Minio.DataModel; using Minio.DataModel.Args; using Minio.DataModel.Encryption; using Minio.DataModel.ILM; using Minio.DataModel.Notification; using Minio.DataModel.ObjectLock; using Minio.DataModel.Replication; using Minio.DataModel.Response; using Minio.DataModel.Result; using Minio.DataModel.Tags; using Minio.Exceptions; using Minio.Helper; namespace Minio; [SuppressMessage("Design", "MA0048:File name must match type name", Justification = "Split up in partial classes")] public partial class MinioClient : IBucketOperations { /// /// List all the buckets for the current Endpoint URL /// /// Optional cancellation token to cancel the operation /// Task with an iterator lazily populated with objects public async Task ListBucketsAsync( CancellationToken cancellationToken = default) { var requestMessageBuilder = await this.CreateRequest(HttpMethod.Get).ConfigureAwait(false); using var response = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var bucketList = new ListAllMyBucketsResult(); if (HttpStatusCode.OK.Equals(response.StatusCode)) { using var stream = new MemoryStream(response.ContentBytes.ToArray()); bucketList = Utils.DeserializeXml(stream); } return bucketList; } /// /// Check if a private bucket with the given name exists. /// /// BucketExistsArgs Arguments Object which has bucket identifier information - bucket name, region /// Optional cancellation token to cancel the operation /// Task public async Task BucketExistsAsync(BucketExistsArgs args, CancellationToken cancellationToken = default) { args?.Validate(); try { var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var response = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken).ConfigureAwait(false); if (response.Exception is not null && response.Exception.GetType() == typeof(BucketNotFoundException)) return false; } catch (InternalClientException ice) { if ((ice.ServerResponse is not null && HttpStatusCode.NotFound.Equals(ice.ServerResponse.StatusCode)) || ice.ServerResponse is null) return false; } catch (Exception ex) { if (ex.GetType() == typeof(BucketNotFoundException)) return false; throw; } return true; } /// /// Remove the bucket with the given name. /// /// RemoveBucketArgs Arguments Object which has bucket identifier information like bucket name .etc. /// Optional cancellation token to cancel the operation /// Task /// When bucketName is invalid /// When bucketName is not found /// When bucketName is null public async Task RemoveBucketAsync(RemoveBucketArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var response = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken).ConfigureAwait(false); } /// /// Create a bucket with the given name. /// /// MakeBucketArgs Arguments Object that has bucket info like name, location. etc /// Optional cancellation token to cancel the operation /// Task /// When access or secret key is invalid /// When bucketName is invalid /// When object-lock or another extension is not implemented public async Task MakeBucketAsync(MakeBucketArgs args, CancellationToken cancellationToken = default) { args?.Validate(); if (string.IsNullOrEmpty(args.Location)) args.Location = Config.Region; if (string.Equals(args.Location, "us-east-1", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(Config.Region)) args.Location = Config.Region; args.IsBucketCreationRequest = true; var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var response = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Get Versioning information on the bucket with given bucket name /// /// GetVersioningArgs takes bucket as argument. /// Optional cancellation token to cancel the operation /// GetVersioningResponse with information populated from REST response /// When bucketName is invalid /// When access or secret key is invalid /// When bucket name is invalid /// When a functionality or extension is not implemented /// When bucket is not found public async Task GetVersioningAsync(GetVersioningArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var responseResult = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var versioningResponse = new GetVersioningResponse(responseResult.StatusCode, responseResult.Content); return versioningResponse.VersioningConfig; } /// /// Set Versioning as specified on the bucket with given bucket name /// /// SetVersioningArgs Arguments Object with information like Bucket name, Versioning configuration /// Optional cancellation token to cancel the operation /// Task /// When access or secret key is invalid /// When bucket name is invalid /// When a functionality or extension is not implemented /// When bucket is not found /// When configuration XML provided is invalid public async Task SetVersioningAsync(SetVersioningArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var response = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken).ConfigureAwait(false); } /// /// List all objects along with versions non-recursively in a bucket with a given prefix, optionally emulating a /// directory /// /// /// ListObjectsArgs Arguments Object with information like Bucket name, prefix, recursive listing, /// versioning /// /// Optional cancellation token to cancel the operation /// An observable of items that client can subscribe to /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// If a functionality or extension (like versioning) is not implemented /// /// For example, if you call ListObjectsAsync on a bucket with versioning /// enabled or object lock enabled /// public IObservable ListObjectsAsync(ListObjectsArgs args, CancellationToken cancellationToken = default) { args?.Validate(); return Observable.Create( async (obs, ct) => { var isRunning = true; var delimiter = args.Recursive ? string.Empty : "/"; var marker = string.Empty; uint count = 0; var versionIdMarker = string.Empty; var nextContinuationToken = string.Empty; using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, ct); while (isRunning) { var goArgs = new GetObjectListArgs() .WithBucket(args.BucketName) .WithPrefix(args.Prefix) .WithDelimiter(delimiter) .WithVersions(args.Versions) .WithContinuationToken(nextContinuationToken) .WithMarker(marker) .WithListObjectsV1(!args.UseV2) .WithHeaders(args.Headers) .WithVersionIdMarker(versionIdMarker); if (args.Versions) { var objectList = await GetObjectVersionsListAsync(goArgs, cts.Token).ConfigureAwait(false); var listObjectsItemResponse = new ListObjectVersionResponse(args, objectList, obs); if (objectList.Item2.Count == 0 && count == 0) return; obs = listObjectsItemResponse.ItemObservable; marker = listObjectsItemResponse.NextKeyMarker; versionIdMarker = listObjectsItemResponse.NextVerMarker; isRunning = objectList.Item1.IsTruncated; } else { var objectList = await GetObjectListAsync(goArgs, cts.Token).ConfigureAwait(false); if (objectList.Item2.Count == 0 && objectList.Item1.KeyCount.Equals("0", StringComparison.OrdinalIgnoreCase) && count == 0) return; var listObjectsItemResponse = new ListObjectsItemResponse(args, objectList, obs); marker = listObjectsItemResponse.NextMarker; isRunning = objectList.Item1.IsTruncated; nextContinuationToken = objectList.Item1.IsTruncated ? objectList.Item1.NextContinuationToken : string.Empty; } cts.Token.ThrowIfCancellationRequested(); count++; } } ); } /// /// Gets notification configuration for this bucket /// /// GetBucketNotificationsArgs Arguments Object with information like Bucket name /// Optional cancellation token to cancel the operation /// /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found public async Task GetBucketNotificationsAsync(GetBucketNotificationsArgs args, CancellationToken cancellationToken = default) { if (args is null) throw new ArgumentNullException(nameof(args)); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var responseResult = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var getBucketNotificationsResponse = new GetBucketNotificationsResponse(responseResult.StatusCode, responseResult.Content); return getBucketNotificationsResponse.BucketNotificationConfiguration; } /// /// Sets the notification configuration for this bucket /// /// /// SetBucketNotificationsArgs Arguments Object with information like Bucket name, notification object /// with configuration to set /// /// Optional cancellation token to cancel the operation /// /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When configuration XML provided is invalid public async Task SetBucketNotificationsAsync(SetBucketNotificationsArgs args, CancellationToken cancellationToken = default) { if (args is null) throw new ArgumentNullException(nameof(args)); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var response = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Removes all bucket notification configurations stored on the server. /// /// RemoveAllBucketNotificationsArgs Arguments Object with information like Bucket name /// Optional cancellation token to cancel the operation /// /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When configuration XML provided is invalid public async Task RemoveAllBucketNotificationsAsync(RemoveAllBucketNotificationsArgs args, CancellationToken cancellationToken = default) { if (args is null) throw new ArgumentNullException(nameof(args)); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var response = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Subscribes to bucket change notifications (a Minio-only extension) /// /// /// ListenBucketNotificationsArgs Arguments Object with information like Bucket name, listen events, /// prefix filter keys, suffix filter keys /// /// Optional cancellation token to cancel the operation /// An observable of JSON-based notification events /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When configuration XML provided is invalid public IObservable ListenBucketNotificationsAsync(ListenBucketNotificationsArgs args, CancellationToken cancellationToken = default) { if (S3utils.IsAmazonEndPoint(Config.BaseUrl)) // Amazon AWS does not support bucket notifications throw new ConnectionException( "Listening for bucket notification is specific only to `minio` server endpoints"); return Observable.Create( async (obs, ct) => { using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, ct); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); args = args.WithNotificationObserver(obs) .WithEnableTrace(Config.TraceHttp); using var response = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); cts.Token.ThrowIfCancellationRequested(); }); } /// /// Gets Tagging values set for this bucket /// /// GetBucketTagsArgs Arguments Object with information like Bucket name /// Optional cancellation token to cancel the operation /// Tagging Object with key-value tag pairs /// When access or secret key is invalid /// When bucket name is invalid /// When a functionality or extension is not implemented /// When bucket is not found public async Task GetBucketTagsAsync(GetBucketTagsArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var responseResult = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var getBucketNotificationsResponse = new GetBucketTagsResponse(responseResult.StatusCode, responseResult.Content); return getBucketNotificationsResponse.BucketTags; } /// /// Sets the Encryption Configuration for the mentioned bucket. /// /// SetBucketEncryptionArgs Arguments Object with information like Bucket name, encryption config /// Optional cancellation token to cancel the operation /// Task /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When a functionality or extension is not implemented /// When configuration XML provided is invalid public async Task SetBucketEncryptionAsync(SetBucketEncryptionArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var restResponse = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Returns the Encryption Configuration for the mentioned bucket. /// /// GetBucketEncryptionArgs Arguments Object encapsulating information like Bucket name /// Optional cancellation token to cancel the operation /// An object of type ServerSideEncryptionConfiguration /// When access or secret key is invalid /// When a functionality or extension is not implemented /// When bucket name is invalid /// When bucket is not found public async Task GetBucketEncryptionAsync(GetBucketEncryptionArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var responseResult = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var getBucketEncryptionResponse = new GetBucketEncryptionResponse(responseResult.StatusCode, responseResult.Content); return getBucketEncryptionResponse.BucketEncryptionConfiguration; } /// /// Removes the Encryption Configuration for the mentioned bucket. /// /// RemoveBucketEncryptionArgs Arguments Object encapsulating information like Bucket name /// Optional cancellation token to cancel the operation /// Task /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When a functionality or extension is not implemented /// When configuration XML provided is invalid public async Task RemoveBucketEncryptionAsync(RemoveBucketEncryptionArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var restResponse = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Sets the Tagging values for this bucket /// /// SetBucketTagsArgs Arguments Object with information like Bucket name, tag key-value pairs /// Optional cancellation token to cancel the operation /// /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When a functionality or extension is not implemented /// When configuration XML provided is invalid public async Task SetBucketTagsAsync(SetBucketTagsArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var restResponse = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Removes Tagging values stored for the bucket. /// /// RemoveBucketTagsArgs Arguments Object with information like Bucket name /// Optional cancellation token to cancel the operation /// /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When a functionality or extension is not implemented /// When configuration XML provided is invalid public async Task RemoveBucketTagsAsync(RemoveBucketTagsArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var restResponse = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Sets the Object Lock Configuration on this bucket /// /// /// SetObjectLockConfigurationArgs Arguments Object with information like Bucket name, object lock /// configuration to set /// /// Optional cancellation token to cancel the operation /// /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When object lock configuration on bucket is not set /// When a functionality or extension is not implemented /// When configuration XML provided is invalid public async Task SetObjectLockConfigurationAsync(SetObjectLockConfigurationArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var restResponse = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Gets the Object Lock Configuration on this bucket /// /// GetObjectLockConfigurationArgs Arguments Object with information like Bucket name /// Optional cancellation token to cancel the operation /// ObjectLockConfiguration object /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When a functionality or extension is not implemented /// When object lock configuration on bucket is not set public async Task GetObjectLockConfigurationAsync(GetObjectLockConfigurationArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var responseResult = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var resp = new GetObjectLockConfigurationResponse(responseResult.StatusCode, responseResult.Content); return resp.LockConfiguration; } /// /// Removes the Object Lock Configuration on this bucket /// /// RemoveObjectLockConfigurationArgs Arguments Object with information like Bucket name /// Optional cancellation token to cancel the operation /// /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When object lock configuration on bucket is not set /// When a functionality or extension is not implemented /// When configuration XML provided is invalid public async Task RemoveObjectLockConfigurationAsync(RemoveObjectLockConfigurationArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var restResponse = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Sets the Lifecycle configuration for this bucket /// /// /// SetBucketLifecycleArgs Arguments Object with information like Bucket name, Lifecycle configuration /// object /// /// Optional cancellation token to cancel the operation /// /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When a functionality or extension is not implemented /// When configuration XML provided is invalid public async Task SetBucketLifecycleAsync(SetBucketLifecycleArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var restResponse = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Gets Lifecycle configuration set for this bucket returned in an object /// /// GetBucketLifecycleArgs Arguments Object with information like Bucket name /// Optional cancellation token to cancel the operation /// LifecycleConfiguration Object with the lifecycle configuration /// When access or secret key is invalid /// When bucket name is invalid /// When a functionality or extension is not implemented /// When bucket is not found public async Task GetBucketLifecycleAsync(GetBucketLifecycleArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var responseResult = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var response = new GetBucketLifecycleResponse(responseResult.StatusCode, responseResult.Content); return response.BucketLifecycle; } /// /// Removes Lifecycle configuration stored for the bucket. /// /// RemoveBucketLifecycleArgs Arguments Object with information like Bucket name /// Optional cancellation token to cancel the operation /// /// When access or secret key is invalid /// When bucket name is invalid /// When bucket is not found /// When a functionality or extension is not implemented /// When configuration XML provided is invalid public async Task RemoveBucketLifecycleAsync(RemoveBucketLifecycleArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var restResponse = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Get Replication configuration for the bucket /// /// GetBucketReplicationArgs Arguments Object with information like Bucket name /// Optional cancellation token to cancel the operation /// Replication configuration object /// When access or secret key provided is invalid /// When bucket name is invalid /// When bucket replication configuration is not set /// When a functionality or extension is not implemented /// When bucket is not found public async Task GetBucketReplicationAsync(GetBucketReplicationArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var responseResult = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var response = new GetBucketReplicationResponse(responseResult.StatusCode, responseResult.Content); return response.Config; } /// /// Set the Replication configuration for the bucket /// /// /// SetBucketReplicationArgs Arguments Object with information like Bucket name, Replication /// Configuration object /// /// Optional cancellation token to cancel the operation /// /// When access or secret key provided is invalid /// When bucket name is invalid /// When bucket replication configuration is not set /// When a functionality or extension is not implemented /// When bucket is not found public async Task SetBucketReplicationAsync(SetBucketReplicationArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var restResponse = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Remove Replication configuration for the bucket. /// /// RemoveBucketReplicationArgs Arguments Object with information like Bucket name /// Optional cancellation token to cancel the operation /// /// When access or secret key provided is invalid /// When bucket name is invalid /// When bucket replication configuration is not set /// When a functionality or extension is not implemented /// When bucket is not found public async Task RemoveBucketReplicationAsync(RemoveBucketReplicationArgs args, CancellationToken cancellationToken = default) { args?.Validate(); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var restResponse = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Subscribes to bucket change notifications (a Minio-only extension) /// /// Bucket to get notifications from /// Events to listen for /// Filter keys starting with this prefix /// Filter keys ending with this suffix /// Optional cancellation token to cancel the operation /// An observable of JSON-based notification events public IObservable ListenBucketNotificationsAsync( string bucketName, IList events, string prefix = "", string suffix = "", CancellationToken cancellationToken = default) { var eventList = new List(events); var args = new ListenBucketNotificationsArgs() .WithBucket(bucketName) .WithEvents(eventList) .WithPrefix(prefix) .WithSuffix(suffix); return ListenBucketNotificationsAsync(args, cancellationToken); } /// /// Returns current policy stored on the server for this bucket /// /// GetPolicyArgs object has information like Bucket name. /// Optional cancellation token to cancel the operation /// Task that returns the Bucket policy as a json string /// When bucketName is invalid /// When a functionality or extension is not implemented /// When a policy is not set public async Task GetPolicyAsync(GetPolicyArgs args, CancellationToken cancellationToken = default) { if (args is null) throw new ArgumentNullException(nameof(args)); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var responseResult = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var getPolicyResponse = new GetPolicyResponse(responseResult.StatusCode, responseResult.Content); return getPolicyResponse.PolicyJsonString; } /// /// Sets the current bucket policy /// /// SetPolicyArgs object has information like Bucket name and the policy to set in Json format /// Optional cancellation token to cancel the operation /// When bucketName is invalid /// When a functionality or extension is not implemented /// When a policy is not set /// Task to set a policy public async Task SetPolicyAsync(SetPolicyArgs args, CancellationToken cancellationToken = default) { if (args is null) throw new ArgumentNullException(nameof(args)); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var response = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Removes the current bucket policy /// /// RemovePolicyArgs object has information like Bucket name /// Optional cancellation token to cancel the operation /// Task to set a policy /// When bucketName is invalid /// When a functionality or extension is not implemented /// When a policy is not set public async Task RemovePolicyAsync(RemovePolicyArgs args, CancellationToken cancellationToken = default) { if (args is null) throw new ArgumentNullException(nameof(args)); var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var response = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); } /// /// Gets the list of objects in the bucket filtered by prefix /// /// /// GetObjectListArgs Arguments Object with information like Bucket name, prefix, delimiter, marker, /// versions(get version IDs of the objects) /// /// Task with a tuple populated with objects /// Optional cancellation token to cancel the operation private async Task>> GetObjectListAsync(GetObjectListArgs args, CancellationToken cancellationToken = default) { var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var responseResult = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var getObjectsListResponse = new GetObjectsListResponse(responseResult.StatusCode, responseResult.Content); return getObjectsListResponse.ObjectsTuple; } /// /// Gets the list of objects along with version IDs in the bucket filtered by prefix /// /// /// GetObjectListArgs Arguments Object with information like Bucket name, prefix, delimiter, marker, /// versions(get version IDs of the objects) /// /// Task with a tuple populated with objects /// Optional cancellation token to cancel the operation private async Task>> GetObjectVersionsListAsync(GetObjectListArgs args, CancellationToken cancellationToken = default) { var requestMessageBuilder = await this.CreateRequest(args).ConfigureAwait(false); using var responseResult = await this.ExecuteTaskAsync(ResponseErrorHandlers, requestMessageBuilder, cancellationToken: cancellationToken) .ConfigureAwait(false); var getObjectsVersionsListResponse = new GetObjectsVersionsListResponse(responseResult.StatusCode, responseResult.Content); return getObjectsVersionsListResponse.ObjectsTuple; } /// /// Gets the list of objects in the bucket filtered by prefix /// /// Bucket to list objects from /// Filters all objects starting with a given prefix /// Delimit the output upto this character /// marks location in the iterator sequence /// Task with a tuple populated with objects /// Optional cancellation token to cancel the operation private Task>> GetObjectListAsync(string bucketName, string prefix, string delimiter, string marker, CancellationToken cancellationToken = default) { // null values are treated as empty strings. var args = new GetObjectListArgs() .WithBucket(bucketName) .WithPrefix(prefix) .WithDelimiter(delimiter) .WithMarker(marker); return GetObjectListAsync(args, cancellationToken); } }