ios - Sails Js 从 POST 参数中获取文件数据并上传到 Cloudinary

标签 ios node.js sails.js cloudinary

我有一个基本的 Sailsjs 应用程序,我正在使用它来学习,并且我已经使用以下代码设法让一个图像 uploader 与 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 Controller 上传功能时。

Cloudinary 确实提供了一个 iOS SDK,但这会绕过我的服务器,这意味着我不能将用户帐户绑定(bind)到个人资料图片(图片的 URL),对吗?

更新1

嗯,根据这个文档没问题:

http://cloudinary.com/documentation/node_image_upload

它说生成一个签名:

The upload samples mentioned above allows your server-side Node code to upload images to Cloudinary. In this flow, if you have a web form that allows your users to upload images, the image data is first sent to your server and only then uploaded to Cloudinary.

A more efficient and powerful option is to allow your users to upload images directly from the browser to Cloudinary instead of going through your servers. This method allows for faster uploading and better user experience. It also reduces load from your servers and reduces the complexity of your Node.js applications.

Uploading directly from the browser is done using Cloudinary's jQuery plugin. To ensure that all uploads were authorized by your application, a secure signature must first be generated in your server-side Node.js code.

需要更多调查...

嗯嗯,这让我想起了将图像上传到 Cloudinary 使用的 Amazon S3 的痛苦记忆。

我记得的步骤是:

  1. 在服务器上请求一个签名的上传 URL
  2. 服务器将包含签名 URL 的 JSON 返回给 iOS 应用
  3. iOS 应用创建分块请求流以将图像上传到服务器

T_T

最佳答案

如果您愿意,Cloudinary 还支持无需签名的直接(客户端)上传。了解更多信息: http://cloudinary.com/blog/direct_upload_made_easy_from_browser_or_mobile_app_to_the_cloud

关于ios - Sails Js 从 POST 参数中获取文件数据并上传到 Cloudinary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31558838/

相关文章:

ios - 我的 Swift 获取请求返回具有错误关系的对象

javascript - 期望函数在 Jest 中抛出异常

node.js - MongoDB地理空间索引,如何将其与数组元素一起使用?

node.js - Docker 容器中 Sailsjs 应用程序的问题

ios - Swift 从 Int 的数组(事件/非事件状态)构建十六进制并获取整数形式的十六进制

ios - 从指针接收图像

iphone - static T const 和 static const T 的区别

node.js - 使用 npm 安装某物时出现错误 : EACCES: permission denied, mkdir

node.js - Sails.js 一对多的表现

javascript - SailsJS 仅用于 API 进程(?)