admin管理员组文章数量:1336293
I am calling an API to get temporary accessKey, SecretAccessKey, SessionToken, for uploading the image and video to s3 bucket in Flutter. i have tried almost all the packages from pub dev and most of them are deprecated.
Future<void> uploadToS3({
required String accessKeyId,
required String secretAccessKey,
required String sessionToken,
required String filePath,
required String bucketName,
required String region,
required String objectKey,
}) async {
final s3_storage = S3Storage(
endPoint: 's3.$region.amazonaws',
accessKey: accessKeyId,
secretKey: secretAccessKey,
sessionToken: sessionToken,
region: 'my region',
signingType: SigningType.V4);
try {
await s3_storage.fPutObject(bucketName, objectKey,filePath);
print('Upload successful!');
} catch (e) {
print('Upload failed: $e');
}
}
this is the error i am getting from AWS
StorageError: The AWS Access Key Id you provided does not exist in our records.
i tried the same code with my orignal keys and its works properly and if i run a script to upload file using the temporary keys it also uploads the file.
issues is when i try to use the temporary keys it gives me the above error in Flutter
I am calling an API to get temporary accessKey, SecretAccessKey, SessionToken, for uploading the image and video to s3 bucket in Flutter. i have tried almost all the packages from pub dev and most of them are deprecated.
Future<void> uploadToS3({
required String accessKeyId,
required String secretAccessKey,
required String sessionToken,
required String filePath,
required String bucketName,
required String region,
required String objectKey,
}) async {
final s3_storage = S3Storage(
endPoint: 's3.$region.amazonaws',
accessKey: accessKeyId,
secretKey: secretAccessKey,
sessionToken: sessionToken,
region: 'my region',
signingType: SigningType.V4);
try {
await s3_storage.fPutObject(bucketName, objectKey,filePath);
print('Upload successful!');
} catch (e) {
print('Upload failed: $e');
}
}
this is the error i am getting from AWS
StorageError: The AWS Access Key Id you provided does not exist in our records.
i tried the same code with my orignal keys and its works properly and if i run a script to upload file using the temporary keys it also uploads the file.
issues is when i try to use the temporary keys it gives me the above error in Flutter
Share edited Nov 21, 2024 at 14:26 Irfan Karim asked Nov 19, 2024 at 18:23 Irfan KarimIrfan Karim 134 bronze badges 2 |1 Answer
Reset to default 0You shouldn't put AWS access key id & secret key in client-side application - in this case, your Flutter app. They are EXTREMELY sensitive information. You can do this task via helping of Backend team.
From my current project, we are doing like this:
1. Generate S3 signature
Backend creates an API that helps mobile create a S3 signature. Link to read: https://docs.aws.amazon/IAM/latest/UserGuide/reference_sigv-create-signed-request.html
Then BE will returns you a response something like this:
const S3SignatureResponse({
required this.acl,
required this.contentType,
required this.key,
required this.policy,
required this.xAmzAlgorithm,
required this.xAmzCredential,
required this.xAmzDate,
required this.xAmzSignature,
});
final String key;
final String acl;
final String contentType;
final String policy;
final String xAmzAlgorithm;
final String xAmzCredential;
final String xAmzDate;
final String xAmzSignature;
2. Upload file to S3
Example, your S3 URL is https://haha-dev-storage.s3.amazonaws
.
You call a POST API to upload file to S3:
Future<void> uploadS3(S3SignatureResponse body, String filePath) async {
final baseUrl = dotenv.env['S3_URL']!;
final option = BaseOptions(baseUrl: baseUrl);
final dio = Dio(option);
dio.interceptors.add(LoggerInterceptor());
final formData = FormData.fromMap({
'key': body.key,
'policy': body.policy,
'x-amz-credential': body.xAmzCredential,
'x-amz-algorithm': body.xAmzAlgorithm,
'x-amz-signature': body.xAmzSignature,
'x-amz-date': body.xAmzDate,
'acl': body.acl,
'content-type': body.contentType,
'file': await MultipartFile.fromFile(filePath),
});
final _ = await dio.post(
dotenv.env['S3_URL']!,
data: formData,
);
return;
}
本文标签: amazon web servicesAWS S3 bucket image upload issue in flutterStack Overflow
版权声明:本文标题:amazon web services - AWS S3 bucket image upload issue in flutter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742406285a2468886.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
region: 'my region',
beregion: region,
– Richard Heap Commented Nov 21, 2024 at 19:54