5

I am trying to generate a URL that other system can use to upload a file to my S3 bucket. I looked over the documentation and similar issues, however, I cannot find the right solution.

I have tried creating the PreSigned URL multiple ways ($this->s3 is a reference so S3Client):

1:

       $signedUrl = $this->s3->getCommand(
            'PutObject',
            array(
                'Bucket' => $this->bucket,
                'Key' => 'recording_test.mp3',
                'Body' => ''
            )
        )->createPresignedUrl('+2 hours');

2:

    $command = $this->s3->getCommand('PutObject', array('Bucket' => $this->bucket, 'Key' => 'recording_test3.mp3', 'Body' => ''));

    $request = $command->prepare();

    $signedUrl = $this->s3->createPresignedUrl($request, '+2 hours');

When trying to simply access the URL I get following error:

The request signature we calculated does not match the signature you provided. Check your key and signing method.

I have also tried it this way: $key = 'recording_test2.mp3'; $url = $this->bucket.'/'.$key;

    $request = $this->s3->put($url);

    $signedUrl = $this->s3->createPresignedUrl($request, '+2 hours');

However, this generates a URL in format of s3.amazonaws.com/bucket/key..., which yields an error about invalid endpoint, and that endpoint in format bucket.s3.amazonaws.com/key... should be used. Manually changing the URL gives the same invalid signature error as per above.

I cannot see what am I doing wrong or any other way to generate the PreSigned URL?

Thanks for help

2
  • Also, in the error details, there is StringToSign field, which is different depending if I just open the URL, or I use it in a form with file upload - not sure if that has to do with anything? If so, how can I make sure the URL is good for uploading a file to the bucket? Commented Oct 6, 2016 at 15:10
  • not sure if you you still have same issue, but you should access the link with a put request, not get or post method Commented Mar 23, 2017 at 17:17

1 Answer 1

0

When creating your signed URL, you should not use the Body key. The signed URL generated includes a signature of the body to be uploaded. Then, when you try upload a file using the URL, it can't match the empty string you are using to the content of the file.

Also, if you are using Curl to put the file, I recommend using \CURLFile, and remember to use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); for your PUT request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.