Exemple #1
0
    private static async Task <bool> TryCreateBucketAsync(IBucketClient bucketClient, string bucketName)
    {
        HeadBucketResponse headResp = await bucketClient.HeadBucketAsync(bucketName);

        //Bucket already exist - we return true to apply post-configuration
        if (headResp.StatusCode == 200)
        {
            return(true);
        }

        CreateBucketResponse createResp = await bucketClient.CreateBucketAsync(bucketName, r => r.EnableObjectLocking = true).ConfigureAwait(false);

        if (createResp.IsSuccess)
        {
            Console.WriteLine($"Successfully created '{bucketName}'.");
            return(true);
        }

        Console.Error.WriteLine($"Failed to create '{bucketName}'. Error: " + createResp.Error?.Message);
        return(false);
    }
Exemple #2
0
 public Task <CreateBucketResponse> CreateBucketAsync(string bucketName, Action <CreateBucketRequest>?config = null, CancellationToken token = default)
 {
     return(_bucketClient.CreateBucketAsync(bucketName, config, token));
 }
Exemple #3
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.");
                }
            }
        }