简体   繁体   English

PHP Amazon AWS S3-使用预签名URL上传对象

[英]PHP Amazon AWS S3 - Upload object using Pre-Signed URL

I am trying to generate a URL that other system can use to upload a file to my S3 bucket. 我正在尝试生成一个URL,其他系统可以使用该URL将文件上传到我的S3存储桶。 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): 我尝试过多种方式创建PreSigned URL( $this->s3是一个引用,因此S3Client):

1: 1:

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

2: 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: 当尝试简单地访问URL时,出现以下错误:

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'; 我也这样尝试过:$ key ='recording_test2.mp3'; $url = $this->bucket.'/'.$key; $ 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. 但是,这会生成s3.amazonaws.com/bucket/key...格式的URL,这会产生有关无效端点的错误, bucket.s3.amazonaws.com/key...应使用bucket.s3.amazonaws.com/key...格式的端点。 。 Manually changing the URL gives the same invalid signature error as per above. 手动更改URL会产生与上述相同的无效签名错误。

I cannot see what am I doing wrong or any other way to generate the PreSigned URL? 我看不到我在做错什么或以其他任何方式生成PreSigned URL?

Thanks for help 感谢帮助

When creating your signed URL, you should not use the Body key. 创建签名的URL时,不应使用“ Body密钥。 The signed URL generated includes a signature of the body to be uploaded. 生成的签名URL包含要上载的正文的签名。 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. 然后,当您尝试使用URL上传文件时,该文件不能将您正在使用的空字符串与文件内容匹配。

Also, if you are using Curl to put the file, I recommend using \\CURLFile, and remember to use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 另外,如果使用Curl放置文件,建议使用\\ CURLFile,并记住使用curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); for your PUT request. 为您的PUT请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM