NodeJS:通过Knox上传纯文本到S3,我得到statusCode = 505?
我有以下代码,其中消息是一个JSONstring。 我试图上传到s3与消息的md5作为目标文件名。 我得到一个'505'statusCode。 我是NodeJS新手,不确定我在这里做错了什么?
knoxInitParams = 'key': awsKey 'secret': awsPrivateKey 'bucket': bucket client = knox.createClient knoxInitParams buff = new Buffer message reqHeader = 'Content-Length': buff.length 'Content-Type': 'text/plain' 'x-amz-acl': 'private' req = client.put '/tmp/xxx.txt', reqHeader req.on 'response', (res) -> console.log res.statusCode console.log res.headers if res.statusCode is 200 console.log res.url req.on 'error', (err) -> console.error "S3 Error: ", err req.end buff
编辑:改变目的地硬编码它,下面的答复指出,这是造成这个问题。 但是,我现在正在得到一个403 🙁
你的代码看起来很好。
确保你的date/时间是正确的。 ntpdate -s pool.ntp.org
。
快速的说明,我也遇到了这个问题,但我的错误是,我有我的文件名空间。
var req = client.put('/tmp/x xx.txt', reqHeader);
我用这个包裹了文件名
var req = client.put(encodeURIComponent('/tmp/x xx.txt'))
很可能你的bug在这里:
req = client.put destination.toLowerCase + '.txt', reqHeader
你可能想要调用destination.toLowerCase
:
req = client.put destination.toLowerCase() + '.txt', reqHeader
另一方面,我认为这完全没有必要 – 已经是小写了。
在附注中,您可能需要考虑unit testing – 这是捕获这类错误的好方法! 如果我是你,我会添加一个函数,比如getFileName
:
getFileName = (contents) -> crypto.createHash('md5').update(contents).digest('hex') + '.txt'
现在你可以使用nodeunit,mocha,jasmine或其他任何一个优秀的testing工具轻松地testing这个函数,并且确保它总是返回你期望的结果 – 如果没有的话,可以帮助你立即注意到错误的位置。
我也可以衷心推荐节点的debugging器,这也可以帮助你捕捉这些错误。
当我用我自己的S3帐户尝试你的确切代码时,它工作正常:
$ coffee test 200 { 'x-amz-id-2': 'f5C32nQHlE0WI8jtNFEZykRFAdrM8ZdBzgeAxc23bnJ2Ti4bYKmcY3pX/KpEzyZg', 'x-amz-request-id': 'B41AACFF85661C2E', date: 'Tue, 01 May 2012 23:15:39 GMT', etag: '"44b25eb6d36a88713b7260d8db15b24b"', 'content-length': '0', server: 'AmazonS3' }
按照@skrewler的build议检查您的ID /密钥/存储桶和date/时间。