Open berlin2 opened 5 years ago
This is what I came up with:
const aws_sha_256 = 'AWS4-HMAC-SHA256';
mysign() {
String bucket = 'name_of_bucket.s3.amazonaws.com';
String image = Uri.encodeComponent('xyz.jpg');
String date = SigV4.generateDatetime();
String date1 = date.substring(0, 8);
String region = 'us-east-2';
int expires = 86400; // in seconds. 86400 = 24hours, 604800 = 7days
String credential = '$aws_access_key_id/$date1/$region/s3/aws4_request';
//https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
final params = {
'X-Amz-Algorithm': aws_sha_256,
'X-Amz-Credential': credential,
'X-Amz-Date': date,
'X-Amz-Expires': expires.toString(),
'X-Amz-SignedHeaders': 'host',
'X-Amz-Security-Token': aws_session_token
};
final canonicalRequest = '''GET
/$image
${SigV4.buildCanonicalQueryString(params)}
host:$bucket
host
UNSIGNED-PAYLOAD''';
final hashedCanonicalRequest = SigV4.hashCanonicalRequest(canonicalRequest);
final credentialScope = SigV4.buildCredentialScope(date, region, 's3');
final stringToSign = SigV4.buildStringToSign(date, credentialScope, hashedCanonicalRequest);
final signingKey = SigV4.calculateSigningKey(aws_secret_access_key, date1, region, 's3');
final signature = SigV4.calculateSignature(signingKey, stringToSign);
String url = 'https://$bucket/$image?';
url = url + 'X-Amz-Algorithm=$aws_sha_256&';
url = url + 'X-Amz-Credential=${Uri.encodeComponent(credential)}&';
url = url + 'X-Amz-Date=$date&';
url = url + 'X-Amz-Expires=$expires&';
url = url + 'X-Amz-SignedHeaders=host&';
url = url + 'X-Amz-Security-Token=${Uri.encodeComponent(aws_session_token)}&';
url = url + 'X-Amz-Signature=$signature';
print(url);
}
@berlin2 works like a charm, thanks!
Do you have an example for generating a presigned URL for a S3 Object Get with a temporary session id?