Exemplo n.º 1
0
        public static S3Client Create(string keyId, string accessKey, AwsRegion region, IWebProxy?proxy = null)
        {
            //In this example we are using using Microsoft's Dependency Injection (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 bind the configuration from above to S3Config, which is automatically used by SimpleS3
            services.Configure <S3Config>(root);

            //Here we create a core client without a network driver
            ICoreBuilder coreBuilder = services.AddSimpleS3Core(s3Config =>
            {
                s3Config.Credentials = new StringAccessKey(keyId, accessKey);
                s3Config.Region      = region;

                //Note that you can also override other configuration values here, even if they were bound to something else above. The values here take precedence.
            });

            //The default client is HttpClientFactory, but to show how we can change this, we use HttpClient here.
            IHttpClientBuilder httpBuilder = coreBuilder.UseHttpClient();

            if (proxy != null)
            {
                httpBuilder.WithProxy(proxy);
            }

            //This adds the S3Client service. This service combines ObjectClient, MultipartClient and BucketClient into a single client. Makes it easier for new people to use the library.
            coreBuilder.UseS3Client();

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

            return(serviceProvider.GetRequiredService <S3Client>());
        }
Exemplo n.º 2
0
        public static S3Client Create(string keyId, string accessKey, AwsRegion region, IWebProxy proxy = null)
        {
            //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 create a core client without a network driver
            ICoreBuilder coreBuilder = services.AddSimpleS3Core(s3Config =>
            {
                root.Bind(s3Config);

                s3Config.Credentials = new StringAccessKey(keyId, accessKey);
                s3Config.Region      = region;
            });

            //The default client is HttpClientFactory, but to show how we can change this, we use HttpClient here.
            coreBuilder.UseHttpClient()
            .WithProxy(proxy);

            //This adds the S3Client service
            coreBuilder.UseS3Client();

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

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