protected UnitTestBase(ITestOutputHelper outputHelper, string?profileName = null) { ProfileName = profileName; IConfigurationRoot configRoot = new ConfigurationBuilder() .AddJsonFile("Config.json", false) .Build(); ServiceCollection collection = new ServiceCollection(); collection.AddSingleton(configRoot); collection.Configure <SimpleS3Config>(configRoot); collection.Configure <SimpleS3Config>(ConfigureConfig); ICoreBuilder coreBuilder = SimpleS3CoreServices.AddSimpleS3Core(collection); ConfigureCoreBuilder(coreBuilder, configRoot); 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 <>)); ConfigureServices(collection); Services = collection.BuildServiceProvider(); }
/// <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)); }
protected internal ClientBase(IInputValidator inputValidator, SimpleS3Config config, INetworkDriver networkDriver) { ServiceCollection services = new ServiceCollection(); services.AddSingleton(inputValidator); services.AddLogging(); SimpleS3CoreServices.AddSimpleS3Core(services); services.AddSingleton(networkDriver); services.AddSingleton(Options.Create(config)); Build(services); }
public CanonicalRequestFileTests() { ServiceCollection services = new ServiceCollection(); services.AddLogging(); SimpleS3CoreServices.AddSimpleS3Core(services, x => { x.Credentials = new StringAccessKey("KeyIdExampleExampleE", "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"); x.RegionCode = "us-east-1"; }); ServiceProvider?provider = services.BuildServiceProvider(); _scopeBuilder = (ScopeBuilder)provider.GetRequiredService <IScopeBuilder>(); _sigBuilder = (SignatureBuilder)provider.GetRequiredService <ISignatureBuilder>(); }
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>()); }
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); }
private async Task <string> TestProvider(Action <SimpleS3Config> configure) { ServiceCollection service = new ServiceCollection(); SimpleS3CoreServices.AddSimpleS3Core(service, x => { x.RegionCode = "myregion"; x.Credentials = new StringAccessKey("key", "secret"); configure(x); }); service.AddSingleton <INetworkDriver, NullNetworkDriver>(); //A dummy network driver using ServiceProvider serviceCollection = service.BuildServiceProvider(); IObjectClient?objectClient = serviceCollection.GetService <IObjectClient>(); Assert.NotNull(objectClient); NullNetworkDriver driver = (NullNetworkDriver)serviceCollection.GetRequiredService <INetworkDriver>(); await objectClient !.GetObjectAsync("bucket", "key"); return(driver.LastUrl); }
public ChunkedSignatureTests() { ServiceCollection services = new ServiceCollection(); services.AddLogging(); SimpleS3CoreServices.AddSimpleS3Core(services).UseAmazonS3(x => { x.Credentials = new StringAccessKey("AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); x.Region = AmazonS3Region.UsEast1; //The tests here have signatures built using path style x.NamingMode = NamingMode.PathStyle; }); ServiceProvider?provider = services.BuildServiceProvider(); _scopeBuilder = (ScopeBuilder)provider.GetRequiredService <IScopeBuilder>(); _sigBuilder = (SignatureBuilder)provider.GetRequiredService <ISignatureBuilder>(); _chunkedSigBuilder = (ChunkedSignatureBuilder)provider.GetRequiredService <IChunkedSignatureBuilder>(); _authBuilder = provider.GetRequiredService <HeaderAuthorizationBuilder>(); _config = provider.GetRequiredService <IOptions <SimpleS3Config> >().Value; }
public SignedUrlTests() { //See https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html ServiceCollection services = new ServiceCollection(); services.AddLogging(); SimpleS3CoreServices.AddSimpleS3Core(services).UseAmazonS3(x => { x.Credentials = new StringAccessKey("AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); x.Region = AmazonS3Region.UsEast1; //The tests here have signatures built using path style x.NamingMode = NamingMode.PathStyle; }); ServiceProvider provider = services.BuildServiceProvider(); _scopeBuilder = (ScopeBuilder)provider.GetRequiredService <IScopeBuilder>(); _sigBuilder = (SignatureBuilder)provider.GetRequiredService <ISignatureBuilder>(); _options = provider.GetRequiredService <IOptions <SimpleS3Config> >().Value; }