public static S3Client Create(string profileName) { ServiceCollection services = new ServiceCollection(); //Here we setup our S3Client IS3ClientBuilder builder = services.AddSimpleS3(); //Here we enable in-memory encryption using Microsoft Data Protection builder.CoreBuilder .UseProfileManager() .BindConfigToProfile(profileName) .UseDataProtection(); //Finally we build the service provider and return the S3Client IServiceProvider serviceProvider = services.BuildServiceProvider(); IProfileManager manager = serviceProvider.GetRequiredService <IProfileManager>(); IProfile? profile = manager.GetProfile(profileName); //If profile is null, then we do not yet have a profile stored on disk. We use ConsoleSetup as an easy and secure way of asking for credentials if (profile == null) { ConsoleSetup.SetupProfile(manager, profileName); } return(serviceProvider.GetRequiredService <S3Client>()); }
private CliManager(string profileName, AwsRegion region) { ServiceCollection services = new ServiceCollection(); IS3ClientBuilder builder = services.AddSimpleS3(); builder.CoreBuilder.UseProfileManager() .UseDataProtection(); //Override the region if it is set via commandline builder.Services.PostConfigure <S3Config>(config => { if (region != AwsRegion.Unknown) { config.Region = region; } }); services.AddSingleton <BucketManager>(); services.AddSingleton <ObjectManager>(); ServiceProvider provider = services.BuildServiceProvider(); ProfileManager = provider.GetRequiredService <IProfileManager>(); IProfile profile = ProfileManager.GetProfile(profileName) ?? ConsoleSetup.SetupProfile(ProfileManager, profileName); S3Client = provider.GetRequiredService <IClient>(); BucketManager = provider.GetRequiredService <BucketManager>(); ObjectManager = provider.GetRequiredService <ObjectManager>(); }
private static async Task Main(string[] args) { IConfigurationRoot configRoot = new ConfigurationBuilder() .AddJsonFile("Config.json", false) .Build(); ServiceCollection services = new ServiceCollection(); services.Configure <S3Config>(configRoot); IS3ClientBuilder clientBuilder = services.AddSimpleS3((s3Config, provider) => configRoot.Bind(s3Config)); string profileName = configRoot["ProfileName"]; clientBuilder.CoreBuilder.UseProfileManager() .BindConfigToProfile(profileName) .UseDataProtection(); IConfigurationSection proxySection = configRoot.GetSection("Proxy"); if (proxySection != null && proxySection["UseProxy"].Equals("true", StringComparison.OrdinalIgnoreCase)) { clientBuilder.HttpBuilder.WithProxy(proxySection["ProxyAddress"]); } using (ServiceProvider serviceProvider = services.BuildServiceProvider()) { IProfileManager manager = serviceProvider.GetRequiredService <IProfileManager>(); IProfile? profile = manager.GetProfile(profileName); //If profile is null, then we do not yet have a profile stored on disk. We use ConsoleSetup as an easy and secure way of asking for credentials if (profile == null) { ConsoleSetup.SetupProfile(manager, profileName); } S3Client client = serviceProvider.GetRequiredService <S3Client>(); await foreach (S3Bucket bucket in client.ListAllBucketsAsync()) { if (!bucket.Name.StartsWith("testbucket-", StringComparison.OrdinalIgnoreCase)) { continue; } DeleteAllObjectsStatus objDelResp = await client.DeleteAllObjectsAsync(bucket.Name).ConfigureAwait(false); if (objDelResp == DeleteAllObjectsStatus.Ok) { await client.DeleteBucketAsync(bucket.Name).ConfigureAwait(false); } } //Empty the main test bucket await client.DeleteAllObjectsAsync(configRoot["BucketName"]).ConfigureAwait(false); } }
private static async Task Main(string[] args) { IConfigurationRoot configRoot = new ConfigurationBuilder() .AddJsonFile("Config.json", false) .Build(); ServiceCollection services = new ServiceCollection(); services.Configure <S3Config>(configRoot); IS3ClientBuilder clientBuilder = services.AddSimpleS3(); string profileName = configRoot["ProfileName"]; clientBuilder.CoreBuilder.UseProfileManager() .BindConfigToProfile(profileName) .UseDataProtection(); IConfigurationSection proxySection = configRoot.GetSection("Proxy"); if (proxySection != null && proxySection["UseProxy"].Equals("true", StringComparison.OrdinalIgnoreCase)) { clientBuilder.HttpBuilder.WithProxy(proxySection["ProxyAddress"]); } using (ServiceProvider provider = services.BuildServiceProvider()) { IProfileManager manager = provider.GetRequiredService <IProfileManager>(); IProfile? profile = manager.GetProfile(profileName); //If profile is null, then we do not yet have a profile stored on disk. We use ConsoleSetup as an easy and secure way of asking for credentials if (profile == null) { Console.WriteLine("No profile found. Starting setup."); ConsoleSetup.SetupProfile(manager, profileName); } IBucketClient bucketClient = provider.GetRequiredService <IBucketClient>(); string bucketName = configRoot["BucketName"]; Console.WriteLine($"Setting up bucket '{bucketName}'"); HeadBucketResponse headResp = await bucketClient.HeadBucketAsync(bucketName).ConfigureAwait(false); if (headResp.IsSuccess) { Console.WriteLine($"'{bucketName}' already exist."); } else { Console.WriteLine($"'{bucketName}' does not exist - creating."); CreateBucketResponse createResp = await bucketClient.CreateBucketAsync(bucketName, x => x.EnableObjectLocking = true).ConfigureAwait(false); if (createResp.IsSuccess) { Console.WriteLine($"Successfully created '{bucketName}'."); } else { Console.Error.WriteLine($"Failed to create '{bucketName}'. Exiting."); return; } } Console.WriteLine("Adding lock configuration"); PutBucketLockConfigurationResponse putLockResp = await bucketClient.PutBucketLockConfigurationAsync(bucketName, true).ConfigureAwait(false); if (putLockResp.IsSuccess) { Console.WriteLine("Successfully applied lock configuration."); } else { Console.Error.WriteLine("Failed to apply lock configuration."); } List <S3Rule> rules = new List <S3Rule> { new S3Rule("ExpireAll", true) { AbortIncompleteMultipartUploadDays = 1, NonCurrentVersionExpirationDays = 1, Expiration = new S3Expiration(1), Filter = new S3Filter { Prefix = "" } } }; Console.WriteLine("Adding lifecycle configuration"); PutBucketLifecycleConfigurationResponse putLifecycleResp = await bucketClient.PutBucketLifecycleConfigurationAsync(bucketName, rules).ConfigureAwait(false); if (putLifecycleResp.IsSuccess) { Console.WriteLine("Successfully applied lifecycle configuration."); } else { Console.Error.WriteLine("Failed to apply lifecycle configuration."); } } }