Example #1
0
        protected OnlineTestBase(ITestOutputHelper outputHelper)
        {
            ConfigurationBuilder configBuilder = new ConfigurationBuilder();

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

            //Set the configuration from the config file
            IConfigurationRoot configRoot = configBuilder.Build();

            ServiceCollection collection = new ServiceCollection();

            ICoreBuilder coreBuilder = collection.AddSimpleS3Core(config =>
            {
                //Set the configuration from the config file
                configRoot.Bind(config);
            });

            string profileName = configRoot["ProfileName"];

            coreBuilder.UseProfileManager()
            .BindConfigToProfile(profileName)
            .UseDataProtection();

            IHttpClientBuilder httpBuilder = coreBuilder.UseHttpClientFactory();

            httpBuilder.UseTimeoutPolicy(TimeSpan.FromMinutes(10));

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

            if (proxySection != null && proxySection["UseProxy"].Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                httpBuilder.WithProxy(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();

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

            ObjectClient       = _services.GetRequiredService <IObjectClient>();
            BucketClient       = _services.GetRequiredService <IBucketClient>();
            MultipartClient    = _services.GetRequiredService <IMultipartClient>();
            Transfer           = _services.GetRequiredService <Fluent.Transfer>();
            SignedObjectClient = _services.GetRequiredService <ISignedObjectClient>();
        }
Example #2
0
    private static ISimpleClient BuildClient(IWebProxy?proxy = null)
    {
        //Create the dependency injection container
        ServiceCollection services = new ServiceCollection();

        //We use Microsoft.Extensions.Logging to log to the console
        services.AddLogging(x => x.AddConsole());

        //Here we create a core client. It has no network driver at this point.
        ICoreBuilder coreBuilder = SimpleS3CoreServices.AddSimpleS3Core(services);

        //We want to use HttpClientFactory as the HTTP driver
        IHttpClientBuilder httpBuilder = coreBuilder.UseHttpClientFactory();

        //Add a default timeout and retry policy
        httpBuilder.UseDefaultHttpPolicy();

        //If you set a proxy, this is where it gets added
        if (proxy != null)
        {
            httpBuilder.UseProxy(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.UseAmazonS3();

        //Here we add the profile manager. It is a profile system that persist your credentials to disk in a very secure way.
        coreBuilder.UseProfileManager()
        .BindConfigToDefaultProfile() //We can either name the profile (so you can have more than one) or use the default one.
        .UseConsoleSetup();           //This adds a service that ask you to setup your profile if it does not exist.

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

        IProfileManager profileManager = serviceProvider.GetRequiredService <IProfileManager>();
        IProfile?       profile        = profileManager.GetDefaultProfile();

        if (profile == null)
        {
            IProfileSetup setup = serviceProvider.GetRequiredService <IProfileSetup>();
            setup.SetupDefaultProfile();
        }

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