douliao1911 2019-05-15 15:26
浏览 455

如何在放入S3预签名URL时修复'403 Forbidden'

I am trying to put an image onto an S3 bucket using a presigned url. I'm using SAM to host the site locally and the PUT request works correctly. However when running the command on my prod server I get a '403 Forbidden' error.

Worth noting the OPTIONS request works fine, get 200 code, but next when the PUT is sent get 403 code.

Things I have already tried:

  • Tried setting the content type as my image type
  • Tried putting header content type in
  • Tried setting the COMS to allow GET, POST, PUT, DELETE
  • Tried setting the access for my lambda role to have admin access

Nothing worked still get '403 Forbidden'

JQuery Ajax:

$.ajax({
    url: presignedUrl,
    type: 'PUT',
    data: image,
    contentType: image.type,
    processData: false,
    success: function (response) {
        // window.location = '/Prod/';
    }
});

Creating presigned url:

$cmd = $this->client->getCommand('PutObject', [
    'Bucket' => env('AWS_BUCKET'),
    'Key' => 'images/' . $request->input('image_name'),
]);

return $this->client->createPresignedRequest($cmd, '+20 minutes')->getUri();

General from '403 Forbidden':

Request Method: PUT
Status Code: 403 Forbidden
Referrer Policy: no-referrer-when-downgrade

Response Headers from '403 Forbidden':

Access-Control-Allow-Methods: GET, POST, PUT, HEAD
Access-Control-Allow-Origin: *
Connection: close
Content-Type: application/xml
Date: Wed, 15 May 2019 15:02:35 GMT
Server: AmazonS3
Transfer-Encoding: chunked
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
x-amz-id-2: e5WZfJAQk24kl7kBoF+HU8+AOiR7ivTIcUZ71dZl0Ssged03RThlCRtku+AmhRRUwFe1p63cL4Q=
x-amz-request-id: 4767484BEBE4EC07

Request Headers from '403 Forbidden':

Accept: */*
Content-Type: image/jpeg
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36

Locally the put request works correctly and has no errors.

  • 写回答

1条回答 默认 最新

  • duandaiqin6080 2019-05-15 16:21
    关注

    Eventually fixed the issue.

    I had specified the credentials in the S3 Client, removing them allowed the role of the lambda function to access S3 correctly.

    Incorrect:

            $this->client = new S3Client([
                'credentials' => [
                    'key'    => env('AWS_ACCESS_KEY_ID'),
                    'secret' => env('AWS_SECRET_ACCESS_KEY'),
                ],
                'region' => env('AWS_DEFAULT_REGION'),
                'version' => 'latest',
            ]);
    

    Correct:

            $this->client = new S3Client([
                'region' => env('AWS_DEFAULT_REGION'),
                'version' => 'latest',
            ]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集