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 What have you tried? What didn't work? What error did you get? – Richard Heap Commented Nov 19, 2024 at 22:56
  • Shouldn't region: 'my region', be region: region, – Richard Heap Commented Nov 21, 2024 at 19:54
Add a comment  | 

1 Answer 1

Reset to default 0

You 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