我有一个基本的Sailsjs应用程序,我正在学习,我已设法使用以下代码使用Cloudinary获取图像上传器:
/**
* FileController
*
* @description :: Server-side logic for managing files
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/
var cloudinary = require('cloudinary');
module.exports = {
upload: function(req, res) {
console.log('Request params = ' + req.params);
var uploadFile = req.file('uploadFile');
if(uploadFile)
{
console.log('Upload file = ' + uploadFile.fd);
}
uploadFile.upload(function onUploadComplete(err, files) {
if(err) {
return res.serverError(err);
}
else if(files.length === 0) {
return res.badRequest('No file was uploaded.');
}
else {
var imageFile = files[0];
console.log("image path = " + imageFile.fd);
console.log('Uploading to Cloudinary, file with path = ' + imageFile.fd + '....');
// -----------------------------------------------------------------------------
// Using Cloudinary CDN to handle image uploading
// -----------------------------------------------------------------------------
cloudinary.uploader.upload(imageFile.fd, function(result) {
console.log('Finished uploading to Cloudinary, response = %j', result);
res.json(result);
});
}
});
}
};
这样做的唯一问题是我基本上上传了两次图像 .
我的iOS应用程序第一次将图像上传到我的Sailsjs(Nodejs框架)服务器 .
完成上传到我的服务器后,它会从文件路径中获取上传的文件,然后将其上传到Cloudinary CDN .
这是两次上传......
我觉得这不是正确的做法,但我没有看到任何其他方式 .
有可能以某种方式从我的iOS应用程序获取我发送到我的Sailsjs服务器的POST参数并直接上传到Cloudinary吗?
我已经尝试使用 req.file
,但它说 undefined
,我认为这是因为该文件从我的iOS应用程序流式传输到服务器,并且在达到我的Sailsjs控制器上传功能时无法立即使用 .
Cloudinary确实提供了iOS SDK,但这会绕过我的服务器,这意味着我无法将用户帐户绑定到 Profiles 图片(图片的网址),对吧?
更新1
嗯,根据这个文件确定:
http://cloudinary.com/documentation/node_image_upload
它说要生成签名:
上面提到的上传示例允许您的服务器端节点代码将图像上传到Cloudinary . 在此流程中,如果您有一个允许用户上传图像的Web表单,则图像数据首先会发送到您的服务器,然后才会上传到Cloudinary . 更高效和强大的选项是允许您的用户将图像直接从浏览器上传到Cloudinary,而不是通过您的服务器 . 该方法允许更快的上载和更好的用户体验 . 它还可以减少服务器的负载,并降低Node.js应用程序的复杂性 . 直接从浏览器上传是使用Cloudinary的jQuery插件完成的 . 要确保应用程序授权所有上载,必须首先在服务器端Node.js代码中生成安全签名 .
需要更多调查......
Hurmmmm,这带来了将图像上传到Amazon S3的痛苦回忆,这是Cloudinary使用的 .
我记得的步骤是:
-
在服务器上请求已签名的上传URL
-
服务器将包含签名URL的JSON返回到iOS应用程序
-
iOS应用程序创建一个分块请求流,将图像上传到服务器
T_T
1 回答
如果您愿意,Cloudinary还支持直接(客户端)上传而无需签名 . 有关更多信息:http://cloudinary.com/blog/direct_upload_made_easy_from_browser_or_mobile_app_to_the_cloud