Beispiel #1
0
        public static IServiceCollection AddAwsS3Storage(this IServiceCollection serviceCollection, Action <AwsS3StorageOptions> storageOptionsAction)
        {
            var storageOptions = new AwsS3StorageOptions();

            storageOptionsAction?.Invoke(storageOptions);

            if (string.IsNullOrWhiteSpace(storageOptions.AccessKeyId))
            {
                throw new InvalidConfigurationException(nameof(storageOptions.AccessKeyId));
            }
            if (string.IsNullOrWhiteSpace(storageOptions.BucketName))
            {
                throw new InvalidConfigurationException(nameof(storageOptions.BucketName));
            }
            if (string.IsNullOrWhiteSpace(storageOptions.SecretAccessKey))
            {
                throw new InvalidConfigurationException(nameof(storageOptions.SecretAccessKey));
            }
            if (string.IsNullOrWhiteSpace(storageOptions.ServiceUrl))
            {
                throw new InvalidConfigurationException(nameof(storageOptions.ServiceUrl));
            }

            serviceCollection.AddSingleton(storageOptions);
            serviceCollection.AddTransient <HttpRequestHandler>();
            serviceCollection.AddSingleton <AwsS3Service>();
            serviceCollection.AddSingleton <IObjectStorage, ContentAwsS3Storage>();

            serviceCollection
            .AddHttpClient <AwsS3HttpClientFactory>(opt =>
            {
                opt.Timeout = storageOptions.Timeout;
            })
            .ConfigurePrimaryHttpMessageHandler(() =>
            {
                var handler = new HttpClientHandler();
                if (storageOptions.IgnoreCertificateErrors)
                {
                    handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return(true); }
                }
                ;
                return(handler);
            })
            .AddHttpMessageHandler <HttpRequestHandler>()
            .Services
            .AddAWSService <IAmazonS3>(new AWSOptions
            {
                Credentials         = new BasicAWSCredentials(storageOptions.AccessKeyId, storageOptions.SecretAccessKey),
                DefaultClientConfig =
                {
                    ServiceURL    = storageOptions.ServiceUrl,
                    MaxErrorRetry =                         0,
                },
            });

            return(serviceCollection);
        }
 public ContentAwsS3Storage(AwsS3Service awsS3Service, AwsS3StorageOptions storageOptions)
 {
     _awsS3Service   = awsS3Service;
     _storageOptions = storageOptions;
 }