Example #1
0
        private static async Task Main(string[] args)
        {
            IConfigurationRoot root = new ConfigurationBuilder()
                                      .AddJsonFile("Config.json", false)
                                      .Build();

            ServiceCollection services = new ServiceCollection();

            services.Configure <S3Config>(root);

            IS3ClientBuilder clientBuilder = services.AddSimpleS3((s3Config, provider) => root.Bind(s3Config));

            clientBuilder.CoreBuilder.UseProfileManager()
            .BindConfigToDefaultProfile()
            .UseDataProtection();

            IConfigurationSection proxySection = root.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.GetDefaultProfile();

                //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.SetupDefaultProfile(manager);
                }

                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(root["BucketName"]).ConfigureAwait(false);
            }
        }
Example #2
0
        private static async Task Main(string[] args)
        {
            IConfigurationRoot root = new ConfigurationBuilder()
                                      .AddJsonFile("Config.json", false)
                                      .Build();

            ServiceCollection services = new ServiceCollection();

            services.Configure <S3Config>(root);

            IS3ClientBuilder clientBuilder = services.AddSimpleS3();

            clientBuilder.CoreBuilder.UseProfileManager()
            .BindConfigToDefaultProfile()
            .UseDataProtection();

            IConfigurationSection proxySection = root.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.GetDefaultProfile();

                //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.SetupDefaultProfile(manager);
                }

                IBucketClient bucketClient = provider.GetRequiredService <IBucketClient>();

                string bucketName = root["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.");
                }
            }
        }