55using System . IO ;
66using System . Linq ;
77using System . Net . Http . Headers ;
8- using Amazon ;
8+ using System . Threading . Tasks ;
99using Amazon . S3 ;
1010using Amazon . S3 . Model ;
1111using Microsoft . AspNetCore . Hosting ;
1212using Microsoft . Extensions . DependencyInjection ;
1313using Microsoft . Extensions . FileProviders ;
1414using Microsoft . Extensions . Options ;
15- using SixLabors . ImageSharp . Web . Providers ;
1615using SixLabors . ImageSharp . Web . Providers . AWS ;
1716
1817namespace SixLabors . ImageSharp . Web . Tests . TestUtilities
@@ -23,44 +22,19 @@ public static AWSS3StorageImageProvider Create(IServiceProvider services)
2322 {
2423 IOptions < AWSS3StorageImageProviderOptions > options = services . GetRequiredService < IOptions < AWSS3StorageImageProviderOptions > > ( ) ;
2524 FormatUtilities utilities = services . GetRequiredService < FormatUtilities > ( ) ;
26- InitializeAWSStorage ( services , options . Value ) ;
25+ AsyncHelper . RunSync ( ( ) => InitializeAWSStorageAsync ( services , options . Value ) ) ;
2726
2827 return new AWSS3StorageImageProvider ( options , utilities ) ;
2928 }
3029
31- private static void InitializeAWSStorage ( IServiceProvider services , AWSS3StorageImageProviderOptions options )
30+ private static async Task InitializeAWSStorageAsync ( IServiceProvider services , AWSS3StorageImageProviderOptions options )
3231 {
3332 // Upload an image to the AWS Test Storage;
3433 AWSS3BucketClientOptions bucketOptions = options . S3Buckets . First ( ) ;
35- AmazonS3Client amazonS3Client = null ;
36- bool foundBucket = false ;
37-
38- if ( ! string . IsNullOrEmpty ( bucketOptions . Endpoint ) &&
39- bucketOptions . AccessKey != null &&
40- bucketOptions . AccessSecret != null )
41- {
42- var config = new AmazonS3Config
43- {
44- ServiceURL = bucketOptions . Endpoint ,
45- ForcePathStyle = true
46- } ;
47- amazonS3Client = new AmazonS3Client ( string . Empty , string . Empty , config ) ;
48- }
49- else if ( ! string . IsNullOrEmpty ( bucketOptions . AccessKey ) &&
50- ! string . IsNullOrEmpty ( bucketOptions . AccessSecret ) &&
51- ! string . IsNullOrEmpty ( bucketOptions . Region ) )
52- {
53- var region = RegionEndpoint . GetBySystemName ( bucketOptions . Region ) ;
54- amazonS3Client = new AmazonS3Client ( bucketOptions . AccessKey , bucketOptions . AccessSecret , region ) ;
55- }
56- else
57- {
58- var region = RegionEndpoint . GetBySystemName ( bucketOptions . Region ) ;
59- amazonS3Client = new AmazonS3Client ( region ) ;
60- }
61-
62- ListBucketsResponse listBucketsResponse = amazonS3Client . ListBucketsAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
34+ AmazonS3Client amazonS3Client = AmazonS3ClientFactory . CreateClient ( bucketOptions ) ;
35+ ListBucketsResponse listBucketsResponse = await amazonS3Client . ListBucketsAsync ( ) ;
6336
37+ bool foundBucket = false ;
6438 foreach ( S3Bucket b in listBucketsResponse . Buckets )
6539 {
6640 if ( b . BucketName == bucketOptions . BucketName )
@@ -79,7 +53,7 @@ private static void InitializeAWSStorage(IServiceProvider services, AWSS3Storage
7953 CannedACL = S3CannedACL . PublicRead
8054 } ;
8155
82- amazonS3Client . PutBucketAsync ( putBucketRequest ) . GetAwaiter ( ) . GetResult ( ) ;
56+ await amazonS3Client . PutBucketAsync ( putBucketRequest ) ;
8357 }
8458
8559#if NETCOREAPP2_1
@@ -88,22 +62,22 @@ private static void InitializeAWSStorage(IServiceProvider services, AWSS3Storage
8862 IWebHostEnvironment environment = services . GetRequiredService < IWebHostEnvironment > ( ) ;
8963#endif
9064
91- var request = new GetObjectRequest ( )
92- {
93- BucketName = bucketOptions . BucketName ,
94- Key = TestConstants . ImagePath
95- } ;
96-
9765 try
9866 {
99- amazonS3Client . GetObjectAsync ( request ) . GetAwaiter ( ) . GetResult ( ) ;
67+ GetObjectRequest request = new ( )
68+ {
69+ BucketName = bucketOptions . BucketName ,
70+ Key = TestConstants . ImagePath
71+ } ;
72+
73+ await amazonS3Client . GetObjectAsync ( request ) ;
10074 }
10175 catch
10276 {
10377 IFileInfo file = environment . WebRootFileProvider . GetFileInfo ( TestConstants . ImagePath ) ;
10478 using Stream stream = file . CreateReadStream ( ) ;
10579
106- // Set the max-age property so we get coverage for testing is in our Azure provider.
80+ // Set the max-age property so we get coverage for testing is in our AWS provider.
10781 var cacheControl = new CacheControlHeaderValue
10882 {
10983 Public = true ,
@@ -123,7 +97,7 @@ private static void InitializeAWSStorage(IServiceProvider services, AWSS3Storage
12397 InputStream = stream
12498 } ;
12599
126- amazonS3Client . PutObjectAsync ( putRequest ) . GetAwaiter ( ) . GetResult ( ) ;
100+ await amazonS3Client . PutObjectAsync ( putRequest ) ;
127101 }
128102 }
129103 }
0 commit comments