@@ -14,6 +14,7 @@ import 'package:amplify_storage_s3_dart/src/path_resolver/s3_path_resolver.dart'
14
14
import 'package:amplify_storage_s3_dart/src/sdk/s3.dart' as s3;
15
15
import 'package:amplify_storage_s3_dart/src/sdk/src/s3/common/endpoint_resolver.dart'
16
16
as endpoint_resolver;
17
+ import 'package:amplify_storage_s3_dart/src/storage_s3_service/service/s3_client_info.dart' ;
17
18
import 'package:amplify_storage_s3_dart/src/storage_s3_service/storage_s3_service.dart' ;
18
19
import 'package:amplify_storage_s3_dart/src/storage_s3_service/transfer/transfer.dart'
19
20
as transfer;
@@ -84,10 +85,8 @@ class StorageS3Service {
84
85
..supportedProtocols = SupportedProtocols .http1,
85
86
),
86
87
_pathResolver = pathResolver,
88
+ _credentialsProvider = credentialsProvider,
87
89
_logger = logger,
88
- // dependencyManager.get() => sigv4.AWSSigV4Signer is used for unit tests
89
- _awsSigV4Signer = dependencyManager.get () ??
90
- sigv4.AWSSigV4Signer (credentialsProvider: credentialsProvider),
91
90
_dependencyManager = dependencyManager,
92
91
_serviceStartingTime = DateTime .now ();
93
92
@@ -101,14 +100,10 @@ class StorageS3Service {
101
100
final s3.S3Client _defaultS3Client;
102
101
final S3PathResolver _pathResolver;
103
102
final AWSLogger _logger;
104
- final sigv4.AWSSigV4Signer _awsSigV4Signer;
105
103
final DependencyManager _dependencyManager;
106
104
final DateTime _serviceStartingTime;
107
-
108
- sigv4.AWSCredentialScope get _signerScope => sigv4.AWSCredentialScope (
109
- region: _storageOutputs.awsRegion,
110
- service: AWSService .s3,
111
- );
105
+ final AWSIamAmplifyAuthProvider _credentialsProvider;
106
+ final Map <String , S3ClientInfo > _s3ClientsInfo = {};
112
107
113
108
transfer.TransferDatabase get _transferDatabase =>
114
109
_dependencyManager.getOrCreate ();
@@ -261,10 +256,20 @@ class StorageS3Service {
261
256
path: '/$resolvedPath ' ,
262
257
);
263
258
259
+ // dependencyManager.get<sigv4.AWSSigV4Signer>() is used for unit tests
260
+ final awsSigV4Signer = _dependencyManager.get< sigv4.AWSSigV4Signer > () ??
261
+ sigv4.AWSSigV4Signer (
262
+ credentialsProvider: _credentialsProvider,
263
+ );
264
+ final signerScope = sigv4.AWSCredentialScope (
265
+ region: _storageOutputs.awsRegion,
266
+ service: AWSService .s3,
267
+ );
268
+
264
269
return S3GetUrlResult (
265
- url: await _awsSigV4Signer .presign (
270
+ url: await awsSigV4Signer .presign (
266
271
urlRequest,
267
- credentialScope: _signerScope ,
272
+ credentialScope: signerScope ,
268
273
expiresIn: s3PluginOptions.expiresIn,
269
274
serviceConfiguration: _defaultS3SignerConfiguration,
270
275
),
@@ -323,12 +328,17 @@ class StorageS3Service {
323
328
void Function (S3TransferProgress )? onProgress,
324
329
FutureOr <void > Function ()? onDone,
325
330
FutureOr <void > Function ()? onError,
331
+ StorageBucket ? bucket,
326
332
}) {
333
+ // ignore: invalid_use_of_internal_member
334
+ final bucketName = bucket? .resolveBucketInfo (_storageOutputs).bucketName ??
335
+ _storageOutputs.bucketName;
336
+ final s3ClientInfo = _getS3ClientInfo (bucket);
327
337
final uploadDataTask = S3UploadTask .fromDataPayload (
328
338
dataPayload,
329
- s3Client: _defaultS3Client ,
330
- defaultS3ClientConfig: _defaultS3ClientConfig ,
331
- bucket: _storageOutputs. bucketName,
339
+ s3Client: s3ClientInfo.client ,
340
+ defaultS3ClientConfig: s3ClientInfo.config ,
341
+ bucket: bucketName,
332
342
path: path,
333
343
options: options,
334
344
pathResolver: _pathResolver,
@@ -611,4 +621,45 @@ class StorageS3Service {
611
621
}
612
622
}
613
623
}
624
+
625
+ S3ClientInfo _getS3ClientInfo (StorageBucket ? storageBucket) {
626
+ if (storageBucket == null ) {
627
+ return S3ClientInfo (
628
+ client: _defaultS3Client,
629
+ config: _defaultS3ClientConfig,
630
+ );
631
+ }
632
+ // ignore: invalid_use_of_internal_member
633
+ final bucketInfo = storageBucket.resolveBucketInfo (_storageOutputs);
634
+ if (_s3ClientsInfo[bucketInfo.bucketName] != null ) {
635
+ return _s3ClientsInfo[bucketInfo.bucketName]! ;
636
+ }
637
+
638
+ final usePathStyle = bucketInfo.bucketName.contains ('.' );
639
+ if (usePathStyle) {
640
+ _logger.warn (
641
+ 'Since your bucket name contains dots (`"."`), the StorageS3 plugin'
642
+ ' will use path style URLs to communicate with the S3 service. S3'
643
+ ' Transfer acceleration is not supported for path style URLs. For more'
644
+ ' information, refer to:'
645
+ ' https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html' );
646
+ }
647
+ final s3ClientConfig = smithy_aws.S3ClientConfig (
648
+ signerConfiguration: _defaultS3SignerConfiguration,
649
+ usePathStyle: usePathStyle,
650
+ );
651
+ final s3Client = s3.S3Client (
652
+ region: bucketInfo.region,
653
+ credentialsProvider: _credentialsProvider,
654
+ s3ClientConfig: s3ClientConfig,
655
+ client: AmplifyHttpClient (_dependencyManager)
656
+ ..supportedProtocols = SupportedProtocols .http1,
657
+ );
658
+ final s3ClientInfo = S3ClientInfo (
659
+ client: s3Client,
660
+ config: s3ClientConfig,
661
+ );
662
+ _s3ClientsInfo[bucketInfo.bucketName] = s3ClientInfo;
663
+ return s3ClientInfo;
664
+ }
614
665
}
0 commit comments