5

我有以下代码,其中消息是 JSON 字符串。我正在尝试使用消息的 md5 作为目标文件名将其上传到 s3。我收到“505”状态码。我是 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 :(

4

4 回答 4

5

你的代码看起来不错。

确保您的日期/时间正确。 ntpdate -s pool.ntp.org.

于 2012-05-01T22:21:55.290 回答
5

快速说明,我也遇到了这个问题,但我的错误是我的文件名中有一个空格。

var req = client.put('/tmp/x xx.txt', reqHeader);

我包装了文件名,像这样

var req = client.put(encodeURIComponent('/tmp/x xx.txt'))

于 2012-05-11T18:54:57.753 回答
3

您的错误很可能在这里:

req = client.put destination.toLowerCase + '.txt', reqHeader

您可能想要调用destination.toLowerCase

req = client.put destination.toLowerCase() + '.txt', reqHeader

另一方面,我认为这完全没有必要——它已经是小写的了。

附带说明一下,您可能想研究单元测试——这是捕捉这些错误的好方法!如果我是你,我会添加一个函数,比如getFileName

getFileName = (contents) ->
  crypto.createHash('md5').update(contents).digest('hex') + '.txt'

现在,您可以使用 nodeunit、mocha、jasmine 或任何其他出色的测试实用程序轻松测试此功能,并确保它始终返回您期望的结果——如果不是,请帮助您立即发现错误所在。

我也可以衷心推荐 node 的调试器,它也可以帮助你捕捉这些 bug。

于 2012-04-23T14:18:48.487 回答
1

当我使用我自己的 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 的建议检查您的 ID/密钥/存储桶和日期/时间。

于 2012-05-01T23:16:20.903 回答