Ejemplo n.º 1
0
        public static async IAsyncEnumerable <S3Upload> ListAllMultipartUploadsAsync(this IS3BucketClient client, string bucketName, [EnumeratorCancellation] CancellationToken token = default)
        {
            Validator.RequireNotNull(client);
            Validator.RequireNotNull(bucketName);

            string uploadIdMarker = null;
            ListMultipartUploadsResponse response;

            do
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }

                string marker = uploadIdMarker;
                response = await client.ListMultipartUploadsAsync(bucketName, req => req.UploadIdMarker = marker, token).ConfigureAwait(false);

                foreach (S3Upload responseObject in response.Uploads)
                {
                    yield return(responseObject);
                }

                uploadIdMarker = response.NextUploadIdMarker;
            } while (response.IsTruncated);
        }
Ejemplo n.º 2
0
        public static Task <PutBucketResponse> PutBucketAsync(this IS3BucketClient client, string bucketName, AwsRegion region, CancellationToken token = default)
        {
            Validator.RequireNotNull(client);
            Validator.RequireNotNull(bucketName);

            return(client.PutBucketAsync(bucketName, req => req.Region = region, token));
        }
Ejemplo n.º 3
0
        public S3Client(S3Config config, HttpMessageHandler messageHandler)
        {
            ServiceCollection services = new ServiceCollection();

            services.AddSingleton(x => Options.Create(config));

            IS3ClientBuilder   builder     = services.AddSimpleS3Core();
            IHttpClientBuilder httpBuilder = builder.UseHttpClientFactory();

            if (messageHandler != null)
            {
                httpBuilder.ConfigurePrimaryHttpMessageHandler(x => messageHandler);
            }

            httpBuilder.SetHandlerLifetime(TimeSpan.FromMinutes(5));

            Random random = new Random();

            // Policy is:
            // Retries: 3
            // Timeout: 2^attempt seconds (2, 4, 8 seconds) + -100 to 100 ms jitter
            httpBuilder.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3,
                                                                             retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
                                                                             + TimeSpan.FromMilliseconds(random.Next(-100, 100))));

            _provider      = services.BuildServiceProvider();
            _objectClient  = _provider.GetRequiredService <IS3ObjectClient>();
            _bucketClient  = _provider.GetRequiredService <IS3BucketClient>();
            _serviceClient = _provider.GetRequiredService <IS3ServiceClient>();
        }
Ejemplo n.º 4
0
        public static async Task <DeleteBucketStatus> DeleteBucketRecursiveAsync(this IS3BucketClient client, string bucketName, CancellationToken token = default)
        {
            Validator.RequireNotNull(client);
            Validator.RequireNotNull(bucketName);

            DeleteBucketStatus emptyResp = await client.EmptyBucket(bucketName, token).ConfigureAwait(false);

            if (emptyResp != DeleteBucketStatus.Ok)
            {
                return(emptyResp);
            }

            DeleteBucketResponse delResponse = await client.DeleteBucketAsync(bucketName, null, token).ConfigureAwait(false);

            if (!delResponse.IsSuccess && delResponse.Error.Code == ErrorCode.BucketNotEmpty)
            {
                return(DeleteBucketStatus.BucketNotEmpty);
            }

            return(DeleteBucketStatus.Ok);
        }
Ejemplo n.º 5
0
        /// <summary>List all objects in a bucket</summary>
        /// <param name="client">The BucketClient</param>
        /// <param name="bucketName">The name of the bucket you want to list objects in.</param>
        /// <param name="getOwnerInfo">Set to true if you want to get object owner information as well.</param>
        /// <param name="token">A cancellation token</param>
        public static async IAsyncEnumerable <S3Object> GetBucketRecursiveAsync(this IS3BucketClient client, string bucketName, bool getOwnerInfo = false, [EnumeratorCancellation] CancellationToken token = default)
        {
            Validator.RequireNotNull(client);
            Validator.RequireNotNull(bucketName);

            string            continuationToken = null;
            GetBucketResponse response;

            do
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }

                string cToken = continuationToken;
                response = await client.GetBucketAsync(bucketName, req =>
                {
                    req.ContinuationToken = cToken;

                    if (getOwnerInfo)
                    {
                        req.FetchOwner = true;
                    }
                }, token).ConfigureAwait(false);

                if (!response.IsSuccess)
                {
                    throw new Exception();
                }

                foreach (S3Object responseObject in response.Objects)
                {
                    yield return(responseObject);
                }

                continuationToken = response.NextContinuationToken;
            } while (response.IsTruncated);
        }