将缓冲区上传到Google云存储 [英] Uploading a buffer to google cloud storage

查看:104
本文介绍了将缓冲区上传到Google云存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将(从表单上传的文件的)缓冲区保存到Google Cloud存储中,但是Google Node SDK似乎只允许上传具有给定路径的文件(读/写流).

I'm trying to save a Buffer (of a file uploaded from a form) to Google Cloud storage, but it seems like the Google Node SDK only allows files with a given path to be uploaded (Read / Write streams).

这就是我用于AWS(S3)的功能-Google节点SDK中的其他相似之处吗?:

This is what I have used for AWS (S3) - is the anything else similar in the Google node SDK?:

var fileContents = new Buffer('buffer');

var params = {
  Bucket: //bucket name
  Key: //file name
  ContentType: // Set mimetype
  Body: fileContents 
};

s3.putObject(params, function(err, data) {
// Do something 
});

到目前为止,我发现这样做的唯一方法是将缓冲区写入磁盘,使用SDK上传文件(指定新文件的路径),然后在文件成功上传后将其删除-缺点是这是整个过程显着较慢,而使用Google存储似乎是不可行的.上传缓冲区是否有解决方法?

The only way that I have found to do it so far is write the buffer to disk, upload the file using the SDK (specifying the path to the new file) and then delete the file once it's uploaded successfully - the downside to this is that the whole process is significantly slower, to where it seems to be unfeasible to use Google storage. Is there any work around / way to upload a buffer?

推荐答案

我们有一个有关更轻松地支持此问题的问题: https://github.com/GoogleCloudPlatform/gcloud-node/issues/1179

We have an issue about supporting this more easily: https://github.com/GoogleCloudPlatform/gcloud-node/issues/1179

但是现在,您可以尝试:

But for now, you can try:

file.createWriteStream()
  .on('error', function(err) {})
  .on('finish', function() {})
  .end(fileContents);

这篇关于将缓冲区上传到Google云存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆