agilord/aws_client

S3 selectObjectContent `SignatureDoesNotMatch` error

wcheek opened this issue · 0 comments

wcheek commented

I'm trying to use S3 Select to get filtered data from an S3 object using my Flutter app.

I'm loading temporary IAM credentials from a successful Cognito login using Amplify Auth. These credentials work with the S3 Client to listObjectsV2 and getObject, but there is an issue with selectObjectContent. From scouring StackOverflow, this error usually occurs with bad credentials, but the credentials are working for everything but this particular method.

This error occurs even when I try to selectObjectContent on a bucket that I don't have permission to access, and for objects that don't exist. Something is wrong with the signed request.

import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:aws_client/s3_2006_03_01.dart';

/// Getting credentials from my session
    final cognitoPlugin = Amplify.Auth.getPlugin(AmplifyAuthCognito.pluginKey);
    final result = await cognitoPlugin.fetchAuthSession();
    final accessKeyId = result.credentialsResult.value.accessKeyId;
    final secretAccessKey = result.credentialsResult.value.secretAccessKey;
    final sessionToken = result.credentialsResult.value.sessionToken;
    final expiration = result.credentialsResult.value.expiration;

/// Create s3 client
    s3Client = S3(
      region: region,
      credentials: AwsClientCredentials(
        accessKey: accessKeyId,
        secretKey: secretAccessKey,
        sessionToken: sessionToken,
        // expiration: expiration,
      ),
    );

/// Try to selectObjectContent
    final response = await s3Client.selectObjectContent(
      key: fileKey,
      bucket: ftpBucketName,
      expression: r"Select * from S3Object",
      expressionType: ExpressionType.sql,
      inputSerialization: InputSerialization(
        // compressionType: CompressionType.gzip,
        csv: CSVInput(
          fileHeaderInfo: FileHeaderInfo.use,
          recordDelimiter: r"\n",
          fieldDelimiter: ",",
          quoteCharacter: '"',
          quoteEscapeCharacter: '"',
          comments: "#",
        ),
      ),
      outputSerialization: OutputSerialization(
        csv: CSVOutput(
          quoteFields: QuoteFields.asneeded,
          recordDelimiter: r"\n",
          fieldDelimiter: ",",
          quoteCharacter: '"',
          quoteEscapeCharacter: '"',
        ),
      ),
    );

Output:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: SignatureDoesNotMatch null: The request signature we calculated does not match the signature you provided. Check your key and signing method.
#0      RestXmlProtocol.sendRaw (package:aws_client/src/shared/src/protocol/rest_xml.dart:100:9)
<asynchronous suspension>
#1      S3.selectObjectContent (package:aws_client/src/generated/s3/v2006_03_01.dart:11444:21)
<asynchronous suspension>
#2      AWSUtils.selectDataFromFile (package:web_app/network/aws.dart:99:22)
<asynchronous suspension>
#3      _FileViewerState._loadCSV (package:web_app/ui/file_list_page.dart:83:22)
<asynchronous suspension>