使用 node.js 上的 knox 将八位字节流从请求流式传输到 S3

发布于 2024-10-18 11:15:09 字数 832 浏览 4 评论 0原文

我正在尝试使用 node.js 上的 knox 将八位字节流直接流式传输到 S3。 octet-stream 是从浏览器上传的 XHR 文件。我以为我可以将请求流式传输到 putStream 中,一切都会正常工作,但遗憾的是没有。

这是我的代码:

var client = knox.createClient({ 
           // AWS credentials here
         });
if (req.headers['content-type'].match(/application\/octet-stream/i)) {

  var filename = '/'+req.headers['x-file-name'];

  client.putStream(req, filename, function(err, res){
    // TODO: Catch errors
    body = '{"success":"true"}'
    res.writeHead(200, 
      { 'Content-Type':'text/html'
      , 'Content-Length':body.length
      })
    res.end(body)
  });

}

以及我收到的错误:

TypeError: Bad argument
    at Object.stat (fs.js:354:11)
    at Client.putStream (./lib/knox/client.js:181:6)

I'm trying to stream an octet-stream straight to S3 using knox on node.js. The octet-stream is an XHR file upload from the browser. I assumed that I could just stream the request into putStream and everything would just work, but alas no.

Here's my code:

var client = knox.createClient({ 
           // AWS credentials here
         });
if (req.headers['content-type'].match(/application\/octet-stream/i)) {

  var filename = '/'+req.headers['x-file-name'];

  client.putStream(req, filename, function(err, res){
    // TODO: Catch errors
    body = '{"success":"true"}'
    res.writeHead(200, 
      { 'Content-Type':'text/html'
      , 'Content-Length':body.length
      })
    res.end(body)
  });

}

And the error I receive:

TypeError: Bad argument
    at Object.stat (fs.js:354:11)
    at Client.putStream (./lib/knox/client.js:181:6)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌能量女王 2024-10-25 11:15:09

我正在做这样的事情:

app.post('/uploadAmazon', function(req, res) {  
var params = req.query;

var request = client.request("PUT", '/' + req.header('x-file-name') + '?partNumber=' + params.partNumber 
        + '&uploadId=' + params.uploadId, {
    'Content-Length' : req.header('Content-Length')
} );

req.on('data', function(data){
    request.write(data);
});


request.on('response', function(response) {
    console.log('Partial ' + params.partNumber + ' statusCode: ' + response.statusCode);
    if (response.statusCode== 200) {
        uploadMap[params.id].currentSize++;
        uploadMap[params.id].completeXmlArray[+(params.partNumber) - 1] = '<Part><PartNumber>' + params.partNumber + '</PartNumber><ETag>' + response.headers.etag + '</ETag></Part>' ; 

        if (uploadMap[params.id].currentSize == uploadMap[params.id].totalSize) {
            uploadMap[params.id].uploadId = params.uploadId;
            completeSend(uploadMap[params.id]);
        }
    }
}).end();

res.end();

});

假设我从帖子中收到文件名、部件号和 uploadId。

I'm doing something like this:

app.post('/uploadAmazon', function(req, res) {  
var params = req.query;

var request = client.request("PUT", '/' + req.header('x-file-name') + '?partNumber=' + params.partNumber 
        + '&uploadId=' + params.uploadId, {
    'Content-Length' : req.header('Content-Length')
} );

req.on('data', function(data){
    request.write(data);
});


request.on('response', function(response) {
    console.log('Partial ' + params.partNumber + ' statusCode: ' + response.statusCode);
    if (response.statusCode== 200) {
        uploadMap[params.id].currentSize++;
        uploadMap[params.id].completeXmlArray[+(params.partNumber) - 1] = '<Part><PartNumber>' + params.partNumber + '</PartNumber><ETag>' + response.headers.etag + '</ETag></Part>' ; 

        if (uploadMap[params.id].currentSize == uploadMap[params.id].totalSize) {
            uploadMap[params.id].uploadId = params.uploadId;
            completeSend(uploadMap[params.id]);
        }
    }
}).end();

res.end();

});

Assuming that I receive the file name, part number and uploadId from the post.

那支青花 2024-10-25 11:15:09

我相信 client.putStream 接受 4 个参数,如下所示:

client.putStream(stream, filepath, {
  'Content-Length': file.length,
  'Content-Type': 'application/octet-stream',
  'x-amz-acl': 'private'
}, function(err, res) {
  ...
});

I believe client.putStream accepts 4 params, like this:

client.putStream(stream, filepath, {
  'Content-Length': file.length,
  'Content-Type': 'application/octet-stream',
  'x-amz-acl': 'private'
}, function(err, res) {
  ...
});
长伴 2024-10-25 11:15:09

如果您使用的 Node.js 版本早于 0.4.5,请升级。

查看 util 模块 并使用 util.pump 复制文件输入流到输出流。如果必须先下载文件,只需使用文件中的 ReadStream 作为输入流。

另外,请查看 util.pump 的 Javascript 代码 因为我怀疑你还没有完全掌握异步 I/O 在 Node.js 中的工作原理。

If you are using a version of node.js much older than 0.4.5 then upgrade.

Look in the util module and use util.pump to copy the file from the input stream to the output stream. If the file has to be downloaded first, just use a ReadStream from the file as the input stream.

Also, do have a look at the Javascript code for util.pump because I suspect that you haven't quite grasped how async I/O works in node.js.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文