Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header Issue with S3 PUT Command

I'm trying to do a server-less upload to S3 using DropzoneJS. I'm having issues with the AWS Presigned URL specifically, where it's indicating that the x-amz-acl header is unsigned.

Javascript:

var dz = new Dropzone("div#upload", {
  url: "tbd",
  paramName: "file", // The name that will be used to transfer the file
  maxFilesize: 2, // MB
  accept: this.getUploadUrl,
  method: 'put',
  sending: function(file, xhr) {
    var _send = xhr.send;
      xhr.setRequestHeader('x-amz-acl', 'public-read');
      xhr.send = function() {
        _send.call(xhr, file);
      }
    },
});

dz.on('processing', function(file) {
  // change url before sending
  this.options.url = file.uploadUrl;
});

function getUploadUrl(file, cb) {
  var params = {
    fileName: file.name,
    fileType: file.type,
  };
  $.getJSON('signput.php', params).done(function(data) {
    var decodedUri = decodeURIComponent(data['signedRequest']);
    if (!data.signedRequest) {
      return cb('Failed to receive an upload url');
    }

    console.log(decodedUri);
    file.uploadUrl = decodedUri;
    cb();
  }).fail(function() {
    return cb('Failed to receive an upload url');
  });
}

PHP (called to get presigned url):

$fileName = $_GET['fileName'];

$s3Client = new Aws\S3\S3Client([
'version'     => '2006-03-01',
'region'      => 'us-west-2',
'credentials' => [
    'key'    => '__MY__KEY__',
    'secret' => '__MY__SECRET__',
],]);

$cmd = $s3Client->getCommand('PutObject', [
    'Bucket' => '__MY__BUCKET__',
    'Key'    => $fileName
]);

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

// Get the actual presigned-url
$url = (string) $request->getUri();

$urlArray['signedRequest'] = $url;
$urlArray = json_encode($urlArray);
echo $urlArray;

I've also tried setting x-amz-acl to public-read in the Dropzone headers and S3 getCommand, but it's not working.

The error I get:

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code>
<Message>There were headers present in the request which were not signed</Message>
<HeadersNotSigned>x-amz-acl</HeadersNotSigned>
</Error>
like image 761
Lauren Avatar asked Aug 22 '26 20:08

Lauren


1 Answers

There was one issue - I needed to move the ACL => 'public-read' from the JS code into the signing request.

The Dropzone sending function turns into this:

sending: function(file, xhr) {
    var _send = xhr.send;
    xhr.send = function() {
      _send.call(xhr, file);
    }
  }

And the PHP signing requests turns into:

$cmd = $s3Client->getCommand('PutObject', [
  'Bucket' => '__MY__BUCKET__',
  'Key'    => $fileName,
  'ACL'   => 'public-read'
]);

Thanks to Michael for pointing me in the right direction.

Final code for reference...

Javascript:

var dz = new Dropzone("div#upload", {
  url: "tbd",
  paramName: "file", // The name that will be used to transfer the file
  maxFilesize: 2, // MB
  accept: this.getUploadUrl,
  method: 'put',
  sending: function(file, xhr) {
      var _send = xhr.send;
      xhr.send = function() {
        _send.call(xhr, file);
      }
    },
});

dz.on('processing', function(file) {
  // change url before sending
  this.options.url = file.uploadUrl;
});

function getUploadUrl(file, cb) {
  var params = {
    fileName: file.name,
    fileType: file.type,
  };
  $.getJSON('signput.php', params).done(function(data) {
    var decodedUri = decodeURIComponent(data['signedRequest']);
    if (!data.signedRequest) {
      return cb('Failed to receive an upload url');
    }

    file.uploadUrl = decodedUri;
    cb();
  }).fail(function() {
    return cb('Failed to receive an upload url');
  });
}

PHP:

$fileName = $_GET['fileName'];

$s3Client = new Aws\S3\S3Client([
'version'     => '2006-03-01',
'region'      => 'us-west-2',
'credentials' => [
    'key'    => '__MY_KEY__',
    'secret' => '__MY_SECRET__,
],]);


$cmd = $s3Client->getCommand('PutObject', [
  'Bucket' => '__MY_BUCKET__',
  'Key'    => $fileName,
  'ACL'   => 'public-read'
]);

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

// Get the actual presigned-url
$url = (string) $request->getUri();

$urlArray['signedRequest'] = $url;
$urlArray = json_encode($urlArray);
echo $urlArray;
like image 182
Lauren Avatar answered Aug 24 '26 15:08

Lauren