Exemple #1
0
    /// <summary>Add SimpleS3 services to a service collection.</summary>
    /// <param name="collection">The service collection</param>
    public static IClientBuilder AddAmazonS3(this IServiceCollection collection)
    {
        ICoreBuilder coreBuilder = SimpleS3CoreServices.AddSimpleS3Core(collection);

        coreBuilder.UseAmazonS3();

        IHttpClientBuilder httpBuilder = coreBuilder.UseHttpClientFactory();

        httpBuilder.UseDefaultHttpPolicy();

        coreBuilder.Services.AddSingleton(x =>
        {
            //We have to call a specific constructor for dependency injection
            IObjectClient objectClient           = x.GetRequiredService <IObjectClient>();
            IBucketClient bucketClient           = x.GetRequiredService <IBucketClient>();
            IMultipartClient multipartClient     = x.GetRequiredService <IMultipartClient>();
            IMultipartTransfer multipartTransfer = x.GetRequiredService <IMultipartTransfer>();
            ITransfer transfer = x.GetRequiredService <ITransfer>();
            ISignedObjectClient?signedObjectClient = x.GetRequiredService <ISignedObjectClient>();
            return(new AmazonS3Client(objectClient, bucketClient, multipartClient, multipartTransfer, transfer, signedObjectClient));
        });

        //Add the client as the interface too
        coreBuilder.Services.AddSingleton <ISimpleClient>(x => x.GetRequiredService <AmazonS3Client>());

        return(new ClientBuilder(collection, httpBuilder, coreBuilder));
    }
Exemple #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>());
    }