Example #1
0
    /// <summary>Add SimpleS3 services to a service collection.</summary>
    /// <param name="collection">The service collection</param>
    public static IClientBuilder AddAmazonS3(this IServiceCollection collection)
    {
        ICoreBuilder coreBuilder = SimpleS3CoreServices.AddSimpleS3Core(collection);

        coreBuilder.UseAmazonS3();

        IHttpClientBuilder httpBuilder = coreBuilder.UseHttpClientFactory();

        httpBuilder.UseDefaultHttpPolicy();

        coreBuilder.Services.AddSingleton(x =>
        {
            //We have to call a specific constructor for dependency injection
            IObjectClient objectClient           = x.GetRequiredService <IObjectClient>();
            IBucketClient bucketClient           = x.GetRequiredService <IBucketClient>();
            IMultipartClient multipartClient     = x.GetRequiredService <IMultipartClient>();
            IMultipartTransfer multipartTransfer = x.GetRequiredService <IMultipartTransfer>();
            ITransfer transfer = x.GetRequiredService <ITransfer>();
            ISignedObjectClient?signedObjectClient = x.GetRequiredService <ISignedObjectClient>();
            return(new AmazonS3Client(objectClient, bucketClient, multipartClient, multipartTransfer, transfer, signedObjectClient));
        });

        //Add the client as the interface too
        coreBuilder.Services.AddSingleton <ISimpleClient>(x => x.GetRequiredService <AmazonS3Client>());

        return(new ClientBuilder(collection, httpBuilder, coreBuilder));
    }
Example #2
0
        private void Initialize(IOptions <S3Config> options, INetworkDriver networkDriver, ILoggerFactory loggerFactory)
        {
            Assembly assembly = typeof(S3Config).Assembly;

            SimpleServiceProvider provider = new SimpleServiceProvider(new Tuple <Type, object>(typeof(IOptions <S3Config>), options));

            IEnumerable <IValidator>       validators       = CreateInstances <IValidator>(assembly, provider);
            IEnumerable <IRequestMarshal>  requestMarshals  = CreateInstances <IRequestMarshal>(assembly, provider);
            IEnumerable <IResponseMarshal> responseMarshals = CreateInstances <IResponseMarshal>(assembly, provider);

            ValidatorFactory      validatorFactory     = new ValidatorFactory(validators);
            IMarshalFactory       marshalFactory       = new MarshalFactory(requestMarshals, responseMarshals);
            IScopeBuilder         scopeBuilder         = new ScopeBuilder(options);
            ISigningKeyBuilder    signingKeyBuilder    = new SigningKeyBuilder(options, loggerFactory.CreateLogger <SigningKeyBuilder>());
            ISignatureBuilder     signatureBuilder     = new SignatureBuilder(signingKeyBuilder, scopeBuilder, loggerFactory.CreateLogger <SignatureBuilder>(), options);
            IAuthorizationBuilder authorizationBuilder = new AuthorizationHeaderBuilder(options, scopeBuilder, signatureBuilder, loggerFactory.CreateLogger <AuthorizationHeaderBuilder>());
            DefaultRequestHandler requestHandler       = new DefaultRequestHandler(options, validatorFactory, marshalFactory, networkDriver, authorizationBuilder, Enumerable.Empty <IRequestStreamWrapper>(), loggerFactory.CreateLogger <DefaultRequestHandler>());

            ObjectOperations objectOperations = new ObjectOperations(requestHandler, Enumerable.Empty <IRequestWrapper>(), Enumerable.Empty <IResponseWrapper>());

            _objectClient = new S3ObjectClient(objectOperations);

            BucketOperations bucketOperations = new BucketOperations(requestHandler);

            _bucketClient = new S3BucketClient(bucketOperations);

            MultipartOperations multipartOperations = new MultipartOperations(requestHandler, Enumerable.Empty <IRequestWrapper>(), Enumerable.Empty <IResponseWrapper>());

            _multipartClient = new S3MultipartClient(multipartOperations, objectOperations);

            Transfer = new Transfer(objectOperations, multipartOperations);
        }
Example #3
0
 public MultipartTransfer(IObjectClient objectClient, IMultipartClient multipartClient, IMultipartOperations multipartOperations, IEnumerable <IRequestWrapper> requestWrappers)
 {
     _objectClient        = objectClient;
     _multipartClient     = multipartClient;
     _multipartOperations = multipartOperations;
     _requestWrappers     = requestWrappers;
 }
Example #4
0
 public ControllerClient(IClient client, IObjectClient objectClient, IPropertyMapper propertyFactory, string controllerUri)
 {
     Client          = client;
     ObjectClient    = objectClient;
     PropertyFactory = propertyFactory;
     ControllerUri   = controllerUri;
 }
Example #5
0
        public static Task <DeleteObjectsResponse> DeleteObjectsAsync(this IObjectClient client, string bucketName, IEnumerable <string> objectKeys, Action <DeleteObjectsRequest> config = null, CancellationToken token = default)
        {
            Validator.RequireNotNull(client, nameof(client));
            Validator.RequireNotNull(bucketName, nameof(bucketName));
            Validator.RequireNotNull(objectKeys, nameof(objectKeys));

            return(client.DeleteObjectsAsync(bucketName, objectKeys.Select(x => new S3DeleteInfo(x)), config, token));
        }
Example #6
0
        public S3Client(IObjectClient objectClient, IBucketClient bucketClient, IMultipartClient multipartClient)
        {
            _objectClient    = objectClient;
            _bucketClient    = bucketClient;
            _multipartClient = multipartClient;

            Transfer = new Transfer(_objectClient.ObjectOperations, _multipartClient.MultipartOperations);
        }
Example #7
0
        public static async Task <PutObjectResponse> PutObjectDataAsync(this IObjectClient client, string bucketName, string objectKey, byte[] data, Action <PutObjectRequest> config = null, CancellationToken token = default)
        {
            Validator.RequireNotNull(client, nameof(client));
            Validator.RequireNotNull(bucketName, nameof(bucketName));
            Validator.RequireNotNull(objectKey, nameof(objectKey));

            using (MemoryStream ms = new MemoryStream(data))
                return(await client.PutObjectAsync(bucketName, objectKey, ms, config, token).ConfigureAwait(false));
        }
Example #8
0
 protected void Initialize(IObjectClient objectClient, IBucketClient bucketClient, IMultipartClient multipartClient, IMultipartTransfer multipartTransfer, ITransfer transfer, ISignedObjectClient signedObjectClient)
 {
     _objectClient       = objectClient;
     _bucketClient       = bucketClient;
     _multipartClient    = multipartClient;
     _multipartTransfer  = multipartTransfer;
     _transfer           = transfer;
     _signedObjectClient = signedObjectClient;
 }
        /// <summary>Delete all objects within the bucket</summary>
        public static async IAsyncEnumerable <S3DeleteError> DeleteAllObjectsAsync(this IObjectClient client, string bucketName, string?prefix = null, [EnumeratorCancellation] CancellationToken token = default)
        {
            Validator.RequireNotNull(client, nameof(client));
            Validator.RequireNotNullOrEmpty(bucketName, nameof(bucketName));

            ListObjectsResponse        response;
            Task <ListObjectsResponse> responseTask = client.ListObjectsAsync(bucketName, req => req.Prefix = prefix, token);

            ObjectPool <S3DeleteInfo> pool = ObjectPool <S3DeleteInfo> .Shared;

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

                response = await responseTask;

                if (!response.IsSuccess)
                {
                    throw new S3RequestException(response, $"Unable to list objects in bucket '{bucketName}");
                }

                if (response.Objects.Count == 0)
                {
                    yield break;
                }

                if (response.IsTruncated)
                {
                    string localToken = response.NextContinuationToken;
                    responseTask = client.ListObjectsAsync(bucketName, req =>
                    {
                        req.Prefix            = prefix;
                        req.ContinuationToken = localToken;
                    }, token);
                }

                IList <S3DeleteInfo> delete = response.Objects.Select(x => pool.Rent(info => info.Initialize(x.ObjectKey))).ToList();

                DeleteObjectsResponse multiDelResponse = await client.DeleteObjectsAsync(bucketName, delete, req => req.Quiet = false, token).ConfigureAwait(false);

                pool.Return(delete);

                if (!multiDelResponse.IsSuccess)
                {
                    throw new S3RequestException(response, $"Unable to delete objects in bucket '{bucketName}");
                }

                foreach (S3DeleteError error in multiDelResponse.Errors)
                {
                    yield return(error);
                }
            } while (response.IsTruncated);
        }
Example #10
0
        public static Task <PutObjectResponse> PutObjectStringAsync(this IObjectClient client, string bucketName, string objectKey, string content, Encoding?encoding = null, Action <PutObjectRequest>?config = null, CancellationToken token = default)
        {
            Validator.RequireNotNull(client, nameof(client));
            Validator.RequireNotNull(bucketName, nameof(bucketName));
            Validator.RequireNotNull(objectKey, nameof(objectKey));

            encoding ??= Constants.Utf8NoBom;

            return(client.PutObjectDataAsync(bucketName, objectKey, encoding.GetBytes(content), config, token));
        }
Example #11
0
        public static async Task <string> GetObjectStringAsync(this IObjectClient client, string bucketName, string objectKey, Encoding encoding = null, Action <GetObjectRequest> config = null, CancellationToken token = default)
        {
            ContentReader content = await GetObjectContentAsync(client, bucketName, objectKey, config, token).ConfigureAwait(false);

            if (content != null)
            {
                return(await content.AsStringAsync(encoding).ConfigureAwait(false));
            }

            return(null);
        }
Example #12
0
 public SmartObjectClientTest()
 {
     config =
         new ClientConfig.Builder()
     {
         Environment    = ClientConfig.Environments.Sandbox,
         ConsumerKey    = Config.ConsumerKey,
         ConsumerSecret = Config.ConsumerSecret
     };
     client       = ClientFactory.Create(config);
     objectClient = client.Objects;
 }
Example #13
0
    private void Build(IServiceCollection services)
    {
        _serviceProvider = services.BuildServiceProvider();

        IObjectClient       objectClient       = _serviceProvider.GetRequiredService <IObjectClient>();
        IBucketClient       bucketClient       = _serviceProvider.GetRequiredService <IBucketClient>();
        IMultipartClient    multipartClient    = _serviceProvider.GetRequiredService <IMultipartClient>();
        IMultipartTransfer  multipartTransfer  = _serviceProvider.GetRequiredService <IMultipartTransfer>();
        ITransfer           transfer           = _serviceProvider.GetRequiredService <ITransfer>();
        ISignedObjectClient signedObjectClient = _serviceProvider.GetRequiredService <ISignedObjectClient>();

        Client = new SimpleClient(objectClient, bucketClient, multipartClient, multipartTransfer, transfer, signedObjectClient);
    }
Example #14
0
        public static async Task <DeleteObjectResponse> DeleteObjectAsync(this IObjectClient client, string bucketName, string objectKey, string versionId = null, MfaAuthenticationBuilder mfa = null, CancellationToken token = default)
        {
            Validator.RequireNotNull(client, nameof(client));
            Validator.RequireNotNull(bucketName, nameof(bucketName));
            Validator.RequireNotNull(objectKey, nameof(objectKey));

            DeleteObjectResponse resp = await client.DeleteObjectAsync(bucketName, objectKey, req =>
            {
                req.VersionId = versionId;
                req.Mfa       = mfa;
            }, token).ConfigureAwait(false);

            return(resp);
        }
Example #15
0
        public static async Task <PutObjectResponse> PutObjectFileAsync(this IObjectClient client, string bucketName, string objectKey, string file, Action <PutObjectRequest> config = null, CancellationToken token = default)
        {
            Validator.RequireNotNull(client, nameof(client));
            Validator.RequireNotNull(bucketName, nameof(bucketName));
            Validator.RequireNotNull(objectKey, nameof(objectKey));

            if (!File.Exists(file))
            {
                throw new FileNotFoundException("The file does not exist.", file);
            }

            using (FileStream fs = File.OpenRead(file))
                return(await client.PutObjectAsync(bucketName, objectKey, fs, config, token).ConfigureAwait(false));
        }
        public static ICoreBuilder UseS3Client(this ICoreBuilder builder)
        {
            builder.Services.TryAddSingleton(x =>
            {
                //We have to call a specific constructor for dependency injection
                IObjectClient objectClient       = x.GetRequiredService <IObjectClient>();
                IBucketClient bucketClient       = x.GetRequiredService <IBucketClient>();
                IMultipartClient multipartClient = x.GetRequiredService <IMultipartClient>();
                return(new S3Client(objectClient, bucketClient, multipartClient));
            });

            //Add the client as the interface too
            builder.Services.TryAddSingleton <IClient>(x => x.GetRequiredService <S3Client>());
            return(builder);
        }
Example #17
0
        public static async Task <ContentReader> GetObjectContentAsync(this IObjectClient client, string bucketName, string objectKey, Action <GetObjectRequest> config = null, CancellationToken token = default)
        {
            Validator.RequireNotNull(client, nameof(client));
            Validator.RequireNotNull(bucketName, nameof(bucketName));
            Validator.RequireNotNull(objectKey, nameof(objectKey));

            GetObjectResponse resp = await client.GetObjectAsync(bucketName, objectKey, config, token).ConfigureAwait(false);

            if (resp.IsSuccess)
            {
                return(resp.Content);
            }

            return(null);
        }
Example #18
0
        /// <summary>Delete all objects within the bucket</summary>
        public static async Task <DeleteAllObjectsStatus> DeleteAllObjectsAsync(this IObjectClient client, string bucketName, CancellationToken token = default)
        {
            string continuationToken = null;
            ListObjectsResponse response;

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

                string cToken = continuationToken;
                response = await client.ListObjectsAsync(bucketName, req => req.ContinuationToken = cToken, token).ConfigureAwait(false);

                if (!response.IsSuccess)
                {
                    return(DeleteAllObjectsStatus.RequestFailed);
                }

                if (response.KeyCount == 0)
                {
                    break;
                }

                DeleteObjectsResponse multiDelResponse = await client.DeleteObjectsAsync(bucketName, response.Objects.Select(x => x.ObjectKey), req => req.Quiet = false, token).ConfigureAwait(false);

                if (!multiDelResponse.IsSuccess)
                {
                    return(DeleteAllObjectsStatus.RequestFailed);
                }

                if (multiDelResponse.Errors.Count > 0)
                {
                    return(DeleteAllObjectsStatus.ObjectDeleteFailed);
                }

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

            return(DeleteAllObjectsStatus.Ok);
        }
Example #19
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> ListAllObjectsAsync(this IObjectClient client, string bucketName, bool getOwnerInfo = false, Action <ListObjectsRequest> config = null, [EnumeratorCancellation] CancellationToken token = default)
        {
            Validator.RequireNotNull(client, nameof(client));
            Validator.RequireNotNullOrEmpty(bucketName, nameof(bucketName));

            string continuationToken = null;
            ListObjectsResponse response;

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

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

                    if (getOwnerInfo)
                    {
                        req.FetchOwner = true;
                    }

                    config?.Invoke(req);
                }, token).ConfigureAwait(false);

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

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

                continuationToken = response.NextContinuationToken;
            } while (response.IsTruncated);
        }
Example #20
0
        public static async IAsyncEnumerable <S3DeleteError> DeleteAllObjectVersionsAsync(this IObjectClient client, string bucketName, string?prefix = null, [EnumeratorCancellation] CancellationToken token = default)
        {
            Validator.RequireNotNull(client, nameof(client));
            Validator.RequireNotNullOrEmpty(bucketName, nameof(bucketName));

            ListObjectVersionsResponse        response;
            Task <ListObjectVersionsResponse> responseTask = client.ListObjectVersionsAsync(bucketName, req => req.Prefix = prefix, token);

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

                response = await responseTask;

                if (!response.IsSuccess)
                {
                    throw new S3RequestException(response, $"Unable to list objects in bucket '{bucketName}");
                }

                if (response.Versions.Count + response.DeleteMarkers.Count == 0)
                {
                    yield break;
                }

                if (response.IsTruncated)
                {
                    string keyMarker = response.NextKeyMarker;
                    responseTask = client.ListObjectVersionsAsync(bucketName, req =>
                    {
                        req.Prefix    = prefix;
                        req.KeyMarker = keyMarker;
                    }, token);
                }

                IEnumerable <S3DeleteInfo> delete = response.Versions.Select(x => new S3DeleteInfo(x.ObjectKey, x.VersionId))
                                                    .Concat(response.DeleteMarkers.Select(x => new S3DeleteInfo(x.ObjectKey, x.VersionId)));

                DeleteObjectsResponse multiDelResponse = await client.DeleteObjectsAsync(bucketName, delete, req => req.Quiet = false, token).ConfigureAwait(false);

                if (!multiDelResponse.IsSuccess)
                {
                    throw new S3RequestException(response, $"Unable to delete objects in bucket '{bucketName}");
                }

                foreach (S3DeleteError error in multiDelResponse.Errors)
                {
                    yield return(error);
                }
            } while (response.IsTruncated);
        }
Example #21
0
 public SimpleClient(IObjectClient objectClient, IBucketClient bucketClient, IMultipartClient multipartClient, IMultipartTransfer multipartTransfer, ITransfer transfer, ISignedObjectClient signedObject)
 {
     Initialize(objectClient, bucketClient, multipartClient, multipartTransfer, transfer, signedObject);
 }
Example #22
0
 internal AmazonS3Client(IObjectClient objectClient, IBucketClient bucketClient, IMultipartClient multipartClient, IMultipartTransfer multipartTransfer, ITransfer transfer, ISignedObjectClient signedObjectClient) : base(objectClient, bucketClient, multipartClient, multipartTransfer, transfer, signedObjectClient)
 {
 }
Example #23
0
 protected internal ClientBase(IObjectClient objectClient, IBucketClient bucketClient, IMultipartClient multipartClient, IMultipartTransfer multipartTransfer, ITransfer transfer, ISignedObjectClient signedObjectClient)
 {
     Client = new SimpleClient(objectClient, bucketClient, multipartClient, multipartTransfer, transfer, signedObjectClient);
 }
 internal GoogleCloudStorageClient(IObjectClient objectClient, IBucketClient bucketClient, IMultipartClient multipartClient, IMultipartTransfer multipartTransfer, ITransfer transfer, ISignedObjectClient signedObjectClient)
     : base(objectClient, bucketClient, multipartClient, multipartTransfer, transfer, signedObjectClient)
 {
 }
Example #25
0
 public ContentObjectClient(IObjectClient client)
 {
     Client = client;
 }
 public ObjectStorageClient(string keyId, string key)
 {
     amazonS3Client = CreateClient(keyId, key);
     Bucket         = new BucketClient(amazonS3Client);
     Object         = new ObjectClient(amazonS3Client);
 }