如何从node.js@heroku上传图像到cloudinary?

时间:2016-01-22 02:38:12

标签: node.js heroku cloudinary

如何使用multer从heroku上的node.js上载图像(无权写入磁盘)到cloudinary?是否可以从memoryStorage上传缓冲区对象?

1 个答案:

答案 0 :(得分:1)

几天后我弄明白该怎么做!您需要使用https://github.com/heldr/datauri将缓冲区编码为数据URI。这是它的样子:

var Datauri = require('datauri');
var multer = require('multer');
var memoryStorage = multer.memoryStorage();
var memoryUpload = multer({
 storage: memoryStorage,
 limits: {fileSize: 500000, files: 1}
}).single('image');

cloudinary.config({
  cloud_name: '',
  api_key: '',
  api_secret: ''
});
var dUri = new Datauri();

...

dUri.format(path.extname(req.file.originalname).toString(),req.file.buffer);
cloudinary.uploader.upload(dUri.content, function (err, i) {
  if (err) {
    ...
  } else {
    ...
  }
});