Growler Growler - 5 months ago 76
AngularJS Question

OPTIONS ERR_NAME_NOT_RESOLVED when uploading file to s3

I'm trying to upload an image to S3 using Angular and Laravel 5.1.

Angular first asks my server app for a presigned URL, then proceeds to upload the file using the signed request.

I'm getting the following OPTIONS error when the signature returns and then attempts to upload with it:

OPTIONS https://s3.us-standard.amazonaws.com/my.resources/dog-aromatherapy.jpg?x-a…Signature=b3ef83... net::ERR_NAME_NOT_RESOLVED


my.resources S3 Bucket CORS:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>


Laravel:

public function signature(Request $request)
{

...get Request filename and filetype...

$s3Client = new S3Client([
'region' => 'us-standard',
'version' => 'latest'
]);

$options = [
'Bucket' => getenv('AWS_S3_BUCKET'),
'Key' => $file_name,
'Expires' => 60,
'ContentType' => $file_type,
'ACL' => 'public-read'
];

$cmd = $s3Client->getCommand('PutObject', $options);

$s_request = $s3Client->createPresignedRequest($cmd, '+20 minutes');

$pre_signedUrl = (string) $s_request->getUri();

return response()->json(
[
'signed_request' => $pre_signedUrl,
'url' => 'https://s3.amazonaws.com/' . getenv('AWS_S3_BUCKET') . '/' . $file_name
]
);
}


What gives? I have a valid CORS config on the correct S3 bucket, and am getting a seemingly presigned signature to then allow a file upload...




Clicking into the OPTIONS error, headers are as follows:

x-amz-acl:public-read
X-Amz-Content-Sha256:UNSIGNED-PAYLOAD
X-Amz-Algorithm:AWS4-HMAC-SHA256
X-Amz-Credential:AKIA/20160614/us-standard/s3/aws4_request
X-Amz-Date:20160614T172559Z
X-Amz-SignedHeaders:host
X-Amz-Expires:1200
X-Amz-Signature:b3ef836...

Answer

The error is in this line of header

X-Amz-Credential:AKIA/20160614/us-standard/s3/aws4_request

Change that "us-standard" to "us-east-1" and it will work

Amazon S3 renamed the US Standard region to the US East (N. Virginia) region to be consistent with AWS regional naming conventions.

In this code

$cmd = $s3Client->getCommand('PutObject', $options); 

change options to "params"