i'm trying to upload a file (an image for my tests) to my s3 bucket with a pre-signed post generated with AWS SDK PHP. Firstable i generate the pre-signed post, then i manually create the request with given PostObjectV4 datas with Postman or via a simple html form... After filling everything, the request result in Access Denied :-(. The user associated with the client to generate the PostObjectV4 has Allowed s3:PutObject policy on the corresponding bucket.
I've already tried to :
- Set my bucket as public write, and it works ! This indicates me a problem of permission / policy... Unfortunately my bucket don't have to be public...
- Upload a file via aws command line, it works too
PHP code of pre-signed post generation (datas are in $postObject) :
$assetAwsS3Key = $this->getAssetAwsS3Key($asset);
$options = [
['starts-with', '$key', 'myDir/'],
];
// Optional: configure expiration time string
$expires = '+24 hours';
// Set some defaults for form input fields
$formInputs = ['acl' => 'private'];
$postObject = new PostObjectV4(
$this->buildAwsS3UserClient(),
$this->awsBucketName,
$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();
return ['formAttributes' => $formAttributes, 'formInputs' => $formInputs];
My user (used for client generation) policy :
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::awsBucketName/*"
]
}
]
}
My simple html form for test upload:
<form method="post" action="https://my-bucket.s3.eu-west-3.amazonaws.com" enctype="multipart/form-data">
<input type="hidden" name="key" value="myKey/sources/myImg.jpg" /><br />
<input type="file" name="file" /> <br />
<input type="hidden" name="X-Amz-Credential" value="MYUSERACCESSKEY/20190510/eu-west-3/s3/aws4_request" />
<input type="hidden" name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
<input type="hidden" name="X-Amz-Date" value="20190510T132109Z" />
<input type="hidden" name="Policy" value='MYBASE64ENCODEDPOLICY' />
<input type="hidden" name="X-Amz-Signature" value="MYSIGNATURE" />
<input type="submit" name="submit"/>
</form>
Do you have an idea/clue why this upload fail while the bucket isn't public write-access allowed ?
Many thanks in advance !