Пример #1
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>());
    }
Пример #2
0
    protected internal ClientBase(IInputValidator inputValidator, SimpleS3Config config, IWebProxy?proxy = null)
    {
        ServiceCollection services = new ServiceCollection();

        services.AddSingleton(inputValidator);
        services.AddSingleton(Options.Create(config));
        services.AddLogging();

        ICoreBuilder builder = SimpleS3CoreServices.AddSimpleS3Core(services);

        IHttpClientBuilder httpBuilder = builder.UseHttpClientFactory();

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

        Build(services);
    }