【问题标题】:Specifying "folder" option does not upload image to folder in Cloudinary指定“文件夹”选项不会将图像上传到 Cloudinary 中的文件夹
【发布时间】:2018-01-28 00:51:15
【问题描述】:

我在上传到正确的文件夹时遇到了一些困难。如果我指定:

cloudinary.uploader.upload(filePath, {
  folder: 'test-directory'
});

我正在关注 Node 集成指南:http://cloudinary.com/documentation/image_upload_api_reference#upload

我提前创建了“测试目录”。图片上传成功,但总是进入我的媒体库的根目录。

我在我的帐户设置中启用了“自动创建文件夹”。

这是来自 Cloudinary 的上传响应示例:

{
  public_id: 'we1yjvlpxc1qtuf1oaqe',
  version: 1503236713,
  signature: '37edbc2b19ea72b75298acce8075f9e8ddb12d09',
  width: 675,
  height: 37,
  format: 'jpg',
  resource_type: 'image',
  created_at: '2017-08-20T13:45:13Z',
  tags: [],
  bytes: 602,
  type: 'upload',
  etag: 'b5522ae652340881b213c46c035d0aed',
  url: 'http://res.cloudinary.com/alsoicode/image/upload/v1503236713/we1yjvlpxc1qtuf1oaqe.jpg',
  secure_url: 'https://res.cloudinary.com/alsoicode/image/upload/v1503236713/we1yjvlpxc1qtuf1oaqe.jpg',
  original_filename: 'test_r6_c1'
}

我也尝试将文件夹名称添加到 public_id 选项,但这也没有奏效。

我做错了什么?

【问题讨论】:

  • 你能分享上传响应中返回的public_id吗?
  • @Maor.G 我已经添加了完整的上传响应。
  • Cloudinary 似乎也不尊重use_filename 等其他选项,因为上传的资源都有自动生成的文件名。上传成功,但未应用任何选项。

标签: node.js cloudinary


【解决方案1】:

添加v2 以使上传参数的顺序为(文件、选项、回调) -

cloudinary.v2.uploader.upload(filePath, {
 folder: 'test-directory',
 use_filename: true
});

没有它,顺序是(文件、回调、选项)-

cloudinary.uploader.upload(filePath,function(res){console.log(res);},{
 folder: 'test-directory',
 use_filename: true
})

【讨论】:

  • 就是这样。谢谢!我不确定我是如何设法错过文档中的.v2.,但它就在那里。我衷心感谢您的帮助。
【解决方案2】:
  • 上传参数的顺序为(文件、选项、回调)*

    cloudinary.uploader.upload(
              file,
              {
                  folder: 'directory',
                  use_filename: true
              },
              function (error, result) {
                  if (result.url) {
                      resolve({ url: result.url })
                  } else {
                      reject({ message: "image upload fail", error })
                  }
              },
    
          );
    

【讨论】:

  • 该顺序仅对v2 是正确的,正如上述答案中提到的@Maor.G。您可以先使用var cloudinary = require('cloudinary').v2,然后使用cloudinary.uploader.upload(file, options, callback),而不是在每次调用cloudinary 时显式添加v2
  • @ekt yes 上面的答案是没有它,顺序是(文件、回调、选项),我的答案是上传参数顺序为(文件、选项、回调)
猜你喜欢
  • 2021-04-18
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 2013-12-01
相关资源
最近更新 更多