예제 #1
0
        private static async Task <PutBucketResponse> CreateIfNotExistsAsync(
            AWSS3StorageCacheOptions options,
            S3CannedACL acl)
        {
            AmazonS3Client client = AmazonS3ClientFactory.CreateClient(options);

            bool foundBucket = false;
            ListBucketsResponse listBucketsResponse = await client.ListBucketsAsync();

            foreach (S3Bucket b in listBucketsResponse.Buckets)
            {
                if (b.BucketName == options.BucketName)
                {
                    foundBucket = true;
                    break;
                }
            }

            if (!foundBucket)
            {
                var putBucketRequest = new PutBucketRequest
                {
                    BucketName   = options.BucketName,
                    BucketRegion = options.Region,
                    CannedACL    = acl
                };

                return(await client.PutBucketAsync(putBucketRequest));
            }

            return(null);
        }
예제 #2
0
        public ConfigurationTestResult Test(ProcessorConfiguration configuration)
        {
            AmazonS3ClientFactory factory = new AmazonS3ClientFactory();
            var client = factory.GetS3Client(configuration);

            try
            {
                client.ListObjects(configuration.S3Bucket);

                client.PutObject(new PutObjectRequest
                {
                    BucketName  = configuration.S3Bucket,
                    ContentBody = _contentBody,
                    Key         = _key
                });

                client.DeleteObject(configuration.S3Bucket, _key);
            }
            catch (Exception e)
            {
                return(new ConfigurationTestResult {
                    TestPassed = false, Message = e.Message
                });
            }

            return(new ConfigurationTestResult {
                TestPassed = true
            });
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AWSS3StorageCache"/> class.
        /// </summary>
        /// <param name="cacheOptions">The cache options.</param>
        public AWSS3StorageCache(IOptions <AWSS3StorageCacheOptions> cacheOptions)
        {
            Guard.NotNull(cacheOptions, nameof(cacheOptions));
            AWSS3StorageCacheOptions options = cacheOptions.Value;

            this.bucket         = options.BucketName;
            this.amazonS3Client = AmazonS3ClientFactory.CreateClient(options);
        }
예제 #4
0
    protected virtual async Task <AmazonS3Client> GetAmazonS3Client(BlobProviderArgs args)
    {
        var configuration = args.Configuration.GetAwsConfiguration();

        return(await AmazonS3ClientFactory.GetAmazonS3Client(configuration));
    }
예제 #5
0
 public S3ClientFactoryFacts()
 {
     this.factory = new AmazonS3ClientFactory();
 }
예제 #6
0
        private static async Task InitializeAWSStorageAsync(IServiceProvider services, AWSS3StorageImageProviderOptions options)
        {
            // Upload an image to the AWS Test Storage;
            AWSS3BucketClientOptions bucketOptions       = options.S3Buckets.First();
            AmazonS3Client           amazonS3Client      = AmazonS3ClientFactory.CreateClient(bucketOptions);
            ListBucketsResponse      listBucketsResponse = await amazonS3Client.ListBucketsAsync();

            bool foundBucket = false;

            foreach (S3Bucket b in listBucketsResponse.Buckets)
            {
                if (b.BucketName == bucketOptions.BucketName)
                {
                    foundBucket = true;
                    break;
                }
            }

            if (!foundBucket)
            {
                try
                {
                    var putBucketRequest = new PutBucketRequest
                    {
                        BucketName   = bucketOptions.BucketName,
                        BucketRegion = bucketOptions.Region,
                        CannedACL    = S3CannedACL.PublicRead
                    };

                    await amazonS3Client.PutBucketAsync(putBucketRequest);
                }
                catch (AmazonS3Exception e)
                {
                    // CI tests are run in parallel and can sometimes return a
                    // false negative for the existance of a bucket.
                    if (string.Equals(e.ErrorCode, "BucketAlreadyExists"))
                    {
                        return;
                    }

                    throw;
                }
            }

#if NETCOREAPP2_1
            IHostingEnvironment environment = services.GetRequiredService <IHostingEnvironment>();
#else
            IWebHostEnvironment environment = services.GetRequiredService <IWebHostEnvironment>();
#endif

            try
            {
                GetObjectRequest request = new()
                {
                    BucketName = bucketOptions.BucketName,
                    Key        = TestConstants.ImagePath
                };

                await amazonS3Client.GetObjectAsync(request);
            }
            catch
            {
                IFileInfo file = environment.WebRootFileProvider.GetFileInfo(TestConstants.ImagePath);
                using Stream stream = file.CreateReadStream();

                // Set the max-age property so we get coverage for testing in our AWS provider.
                var cacheControl = new CacheControlHeaderValue
                {
                    Public         = true,
                    MaxAge         = TimeSpan.FromDays(7),
                    MustRevalidate = true
                };

                var putRequest = new PutObjectRequest()
                {
                    BucketName = bucketOptions.BucketName,
                    Key        = TestConstants.ImagePath,
                    Headers    =
                    {
                        CacheControl = cacheControl.ToString()
                    },
                    ContentType = "	image/png",
                    InputStream = stream
                };

                await amazonS3Client.PutObjectAsync(putRequest);
            }
        }