【问题标题】:PHP Amazon AWS S3 - Upload object using Pre-Signed URLPHP Amazon AWS S3 - 使用预签名 URL 上传对象
【发布时间】:2017-02-15 09:47:01
【问题描述】:

我正在尝试生成一个 URL,其他系统可以使用该 URL 将文件上传到我的 S3 存储桶。我查看了文档和类似问题,但是找不到正确的解决方案。

我尝试过多种方式创建 PreSigned URL($this->s3 是 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');

当尝试简单地访问 URL 时,我收到以下错误:

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

我也试过这样: $key = 'recording_test2.mp3'; $url = $this->bucket.'/'.$key;

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

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

但是,这会生成格式为 s3.amazonaws.com/bucket/key... 的 URL,这会产生有关无效端点的错误,并且应使用格式为 bucket.s3.amazonaws.com/key... 的端点。手动更改 URL 会产生与上述相同的无效签名错误。

我看不出我做错了什么或以任何其他方式生成 PreSigned URL?

感谢您的帮助

【问题讨论】:

  • 此外,在错误详细信息中,有 StringToSign 字段,这取决于我是打开 URL,还是在文件上传的表单中使用它 - 不确定是否必须这样做做什么?如果是这样,我如何确保 URL 适合将文件上传到存储桶?
  • 不确定您是否还有同样的问题,但您应该使用 put 请求访问链接,而不是 get 或 post 方法

标签: php amazon-web-services amazon-s3


【解决方案1】:

创建签名 URL 时,不应使用 Body 密钥。生成的签名 URL 包括要上传的正文的签名。然后,当您尝试使用 URL 上传文件时,它无法将您使用的空字符串与文件内容匹配。

另外,如果您使用 Curl 来放置文件,我建议使用 \CURLFile,并记得使用 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 来进行 PUT 请求。

【讨论】:

    猜你喜欢
    • 2021-02-15
    • 2014-11-17
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 2021-09-29
    • 2018-12-27
    • 2023-04-09
    • 2019-07-27
    相关资源
    最近更新 更多