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); }
/// <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); }
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); } }