Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        protected async Task CreateTempBucketAsync(Func <string, Task> action)
        {
            string tempBucketName = "testbucket-" + Guid.NewGuid();

            CreateBucketResponse createResponse = await BucketClient.CreateBucketAsync(tempBucketName).ConfigureAwait(false);

            Assert.True(createResponse.IsSuccess);

            try
            {
                await action(tempBucketName).ConfigureAwait(false);
            }
            finally
            {
                DeleteAllObjectsStatus delResp = await ObjectClient.DeleteAllObjectsAsync(tempBucketName).ConfigureAwait(false);

                Assert.Equal(DeleteAllObjectsStatus.Ok, delResp);

                DeleteBucketResponse del2Resp = await BucketClient.DeleteBucketAsync(tempBucketName).ConfigureAwait(false);

                Assert.True(del2Resp.IsSuccess);
            }
        }