每当我尝试将图像上传到Amazon Web Services S3存储桶时出现400错误

时间:2017-07-31 18:27:12

标签: node.js amazon-web-services amazon-s3 knox-amazon-s3-client

您好,

请耐心等待我,因为我是新手。

我尝试使用NodeJS Knox客户端将图像文件上传到Amazon Web Services S3存储桶,但每次尝试时都会出现400错误。我认为这可能是授权问题,因为我无法在NodeJS中的代码中发现任何错误。我在上传后将knox客户端对象打印到控制台,我将其中的一部分放入其中,不确定它是否告诉我AWS没有授权我:

代理商:
   特工{
     domain:null,
     _events:{free:[Function]},
     _eventsCount:1,
     _maxListeners:undefined,
     defaultPort:443,
     协议:' https:',
     选项:{path:null},
     请求:{},
     插座:
      {' photogridjose.s3.amazonaws.com:443 :::::::::':
         [TLSSocket {
             _tlsOptions:
              {pipe:null,
                secureContext:SecureContext {context:SecureContext {},
singleUse:true},
                isServer:false,
                requestCert:true,
           * rejectUnauthorized:true,
                session:undefined,
                NPNProtocols:undefined,
                ALPNProtocols:undefined,
                requestOCSP:undefined},
             _secureEstablished:false,
             _securePending:false,
             _newSessionPending:false,
             _controlReleased:true,
             _SNICallback:null,
             servername:null,
             npnProtocol:null,
             alpnProtocol:null,
         
授权:false,*
             authorizationError:null,
             加密:真,
             _events:
              {close:
                 [[功能],
                   {[功能:绑定onceWrapper]监听器:[功能]},
                   [功能:onClose]],
                结束:
                 [{[Function:bound OnceWrapper] listener:[Function:onend]},
                   {[功能:绑定onceWrapper]监听器:[功能:onHangUp]}],
                完成:[功能:onSocketFinish],
                _socketEnd:[功能:onSocketEnd],
                secureConnect:{[Function:bound onceWrapper] listener:[Function]},
                connect:{[Function:bound OnceWrapper] listener:[Function]},
                安全:[功能],
                free:[功能:onFree],
                agentRemove:[功能:onRemove]},
             _eventsCount:9,
             连接:真,
             _hadError:false,
             _handle:
              TLSWrap {
                bytesRead:0,
                _externalStream:[外部],
                fd:-1,
                _parent:
                 TCP {
                   bytesRead:0,
                   _externalStream:[外部],
                   fd:-1,
                   阅读:[Getter / Setter],
                   拥有者:[通函],
                   onread:null,
                   onconnection:null,
                   writeQueueSize:0},
                _parentWrap:undefined,
                _secureContext:SecureContext {context:SecureContext {},
singleUse:true},
                阅读:假,
                拥有者:[通函],
                onread:[功能:onread],


我使用了AWSAccessKeyId& AWS提供的AWSSecretKey并更改了S3存储桶的策略,因此它设置为每个人都可读/写。

这是我的存储桶策略文档:

{
    "版本":" 2012-10-17",
    " Id":" Policy150032 *******",
    "陈述":[
        {
            " Sid":" Stmt1500 *********",
            "效果":"允许",
            "校长":" *",
            "行动":[
                " s3:GetObject",
                " s3:PutObject",
                " *"
            ],
            "资源":" arn:aws:s3 ::: Myphotogrid / *"
        }
    ]
}


这是我的CORS文件:

< CORSConfiguration xmlns =" http://s3.amazonaws.com/doc/2006-03-01/">
< CORSRule>
    < AllowedOrigin> *< / AllowedOrigin>
    < AllowedMethod> GET< / AllowedMethod>
    < AllowedMethod> POST< / AllowedMethod>
    < AllowedMethod> PUT< / AllowedMethod>
    < AllowedHeader>授权< / AllowedHeader>
< / CORSRule>
< / CORSConfiguration>

这是该交易的S3 Bucket的日志:

<错误>
<代码>存取遭拒< /代码>
<消息>拒绝访问< /消息>
<&的requestId GT; 37329C57F01BEC29< /&的requestId GT;
< HOSTID> zYNm + 4zorTHrNnmHBAYPVpFfkJJ3VL1qtJ + PI9vJPRHhCb7vGhhav4YNRlbbakRJlpyab6Gfcn0 = < / HOSTID>
< /错误>

这里有没有人有使用AWS S3存储桶的经验?

1 个答案:

答案 0 :(得分:0)

制作一个新的S3存储桶并删除旧存储桶,确保权限适合公众读取/写入存储桶,并选择最接近我所在位置的服务器来存储它,这两者都解决了400&然后我得到了307个错误。我现在获得了200个成功的响应,并且文件正在上传到我的S3存储桶而没有任何问题。