AWS S3 Presigned POST to URL query string for curl

2019-09-14 08:12发布

问题:

trying to generate a pre-signed query string to POST to an S3 bucket using the AWS SDK for PHP. Getting the following error:

The request signature we calculated does not match the signature you provided.

Here's the php file to generate the URL, adapted from here.

<?php
require('./aws/aws-autoloader.php');
$client = new \Aws\S3\S3Client([
    'version' => 'latest',
    'region' => 'us-east-1',
     'credentials' => [
        'key'    => '[KEY]',
        'secret' => '[SECRET]',
    ],
]);
$bucket = '[mybucket]';

// Set some defaults for form input fields
$formInputs = ['acl' => 'public-read'];

// Construct an array of conditions for policy
$options = [
    ['acl' => 'bucket-owner-full-control'],
    ['bucket' => $bucket],
];

// Optional: configure expiration time string
$expires = '+2 hours';

$postObject = new \Aws\S3\PostObjectV4(
    $client,
    $bucket,
    $formInputs,
    $options,
    $expires
);


// Get attributes to set on an HTML form, e.g., action, method, enctype
$formAttributes = $postObject->getFormAttributes();

// Get form input fields. This will include anything set as a form input in
// the constructor, the provided JSON policy, your AWS Access Key ID, and an
// auth signature.
$formInputs = $postObject->getFormInputs();
echo "https://[mybucket].s3.amazonaws.com/?".http_build_query($formInputs)."&X-Amz-Expires=7200&X-Amz-SignedHeaders=host";
 ?>

And the curl command:

curl --request POST --upload-file "file.gif" -k [query string here]