2016-11-27 57 views
1

我现在用的是NPM documentation的图像上传到谷歌云存储属性图像使用谷歌云存储

app.post('/test', upload.single('image'), function (req, res, next) { 
    }); 
    let bucket = gcs.bucket('bucket-name'); 

    bucket.upload(req.file.path, function(err, file) { 
     if (err) { 
      throw new Error(err); 
     } 
     console.log(file); 
    }); 

    console.log(req.file); 

    // req.body will hold the text fields, if there were any 
}) 

存储有什么方法可以让我指定我的文件自定义名称和文件类型上传?在我的情况下,它应该是一个图像。

回答

2

您可以通过选项来上传方法:

var options = { 
    destination: 'my-image-name.jpg', 
    metadata: { 
    contentType: 'image/jpeg', 
    } 
}; 

bucket.upload(req.file.path, options, function(err, file) { 
}); 
+0

谢谢!与创建写入流而不是使用上传方法相比,是否有任何优势? – user3642173

+0

'bucket.upload'只是一个方便的方法,它会执行'createWriteStream' [检查文档API](https://googlecloudplatform.github.io/google-cloud-node/#/docs/storage/0.5.0/ storage/file?method = createWriteStream) – Erevald

+0

明白了 - 谢谢 – user3642173