コード例 #1
0
ファイル: S3Client.cs プロジェクト: LordMike/SimpleS3
        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>();
        }
コード例 #2
0
ファイル: LiveTestBase.cs プロジェクト: LordMike/SimpleS3
        protected LiveTestBase(ITestOutputHelper outputHelper)
        {
            ConfigurationBuilder configBuilder = new ConfigurationBuilder();

            configBuilder.AddJsonFile("TestConfig.json", false);

            ServiceCollection collection = new ServiceCollection();

            //Set the configuration from the config file
            configBuilder.AddUserSecrets(GetType().Assembly);
            _configRoot = configBuilder.Build();

            IS3ClientBuilder   builder           = collection.AddSimpleS3Core(ConfigureS3);
            IHttpClientBuilder httpClientBuilder = builder.UseHttpClientFactory();

            IConfigurationSection proxySection = _configRoot.GetSection("Proxy");

            if (proxySection != null && proxySection["UseProxy"].Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                httpClientBuilder.ConfigurePrimaryHttpMessageHandler(x => new HttpClientHandler {
                    Proxy = new WebProxy(proxySection["ProxyAddress"])
                });
            }

            collection.AddLogging(x =>
            {
                x.AddConfiguration(_configRoot.GetSection("Logging"));
                x.AddXUnit(outputHelper);
            });

            //A small hack to remove all validators, as we test them separately
            collection.RemoveAll(typeof(IValidator <>));
            collection.RemoveAll <IValidator>();

            Services = collection.BuildServiceProvider();

            //var _bucketClient = Services.GetRequiredService<IS3BucketClient>();

            //var serviceClient = Services.GetRequiredService<IS3ServiceClient>();

            //var enumerator = serviceClient.GetAllAsync().ToListAsync().Result;

            //foreach (S3Bucket bucket in enumerator)
            //{
            //    if (bucket.Name.Contains("test", StringComparison.OrdinalIgnoreCase))
            //        _bucketClient.DeleteBucketRecursiveAsync(bucket.Name).Wait();
            //}

            BucketName = _configRoot["BucketName"] ?? "main-test-bucket-2019";

            Config        = Services.GetRequiredService <IOptions <S3Config> >().Value;
            ObjectClient  = Services.GetRequiredService <IS3ObjectClient>();
            BucketClient  = Services.GetRequiredService <IS3BucketClient>();
            ServiceClient = Services.GetRequiredService <IS3ServiceClient>();
            Transfer      = Services.GetRequiredService <Transfer>();
        }
コード例 #3
0
        public static S3Client Create()
        {
            //In this example we are using Dependency Injection (DI) using Microsoft's DI framework
            ServiceCollection services = new ServiceCollection();

            //We use Microsoft.Extensions.Configuration framework here to load our config file
            ConfigurationBuilder configBuilder = new ConfigurationBuilder();

            configBuilder.AddJsonFile("Config.json", false);
            IConfigurationRoot root = configBuilder.Build();

            //We use Microsoft.Extensions.Logging here to add support for logging
            services.AddLogging(x =>
            {
                x.AddConsole();
                x.AddConfiguration(root.GetSection("Logging"));
            });

            //Here we setup our S3Client
            IS3ClientBuilder clientBuilder = services.AddSimpleS3Core(s3Config =>
            {
                root.Bind(s3Config);

                s3Config.Credentials = new StringAccessKey("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
                s3Config.Region      = AwsRegion.USEast1;
                s3Config.Endpoint    = new Uri("https://play.min.io:9000/");
            });

            //We enable HTTP Factory support here.
            IHttpClientBuilder httpBuilder = clientBuilder.UseHttpClientFactory();

            //Every 5 minutes we create a new connection, thereby reacting to DNS changes
            httpBuilder.SetHandlerLifetime(TimeSpan.FromMinutes(5));

            //Uncomment this line if you want to use a proxy
            //httpBuilder.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { Proxy = new WebProxy("http://<YourProxy>:<ProxyPort>") });

            //Here we enable retrying. We retry 3 times with a delay of 600 ms between attempts. For more examples, see https://github.com/App-vNext/Polly
            httpBuilder.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(600)));

            //Finally we build the service provider and return the S3Client
            IServiceProvider serviceProvider = services.BuildServiceProvider();

            return(serviceProvider.GetRequiredService <S3Client>());
        }
コード例 #4
0
        public static S3Client Create(string keyId, string accessKey, string proxyUri)
        {
            ServiceCollection services      = new ServiceCollection();
            IS3ClientBuilder  clientBuilder = services.AddSimpleS3Core(s3Config =>
            {
                s3Config.Credentials = new StringAccessKey(keyId, accessKey);
                s3Config.Region      = AwsRegion.EUWest1;
            });

            IHttpClientBuilder httpBuilder = clientBuilder.UseHttpClientFactory();

            httpBuilder.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler {
                Proxy = new WebProxy(proxyUri)
            });

            IServiceProvider serviceProvider = services.BuildServiceProvider();

            return(serviceProvider.GetRequiredService <S3Client>());
        }