如何从 Node.js 中的 cloudinary 列出文件夹中的所有 images/videos?

How to list all the images/videos within a folder from cloudinary in Node.js?

我是 Node.js 的新手。我已经开始通过从 github. Then I changed my working directory to photo_album and installed all the dependencies required for the app. And then, I did some changes as per my requirements and made it image and video gallery app.

1. When I try to list all the images within a folder named my_photos something like cloudinary support 克隆 cloudinary 示例项目来构建应用程序

cloudinary.api.resources(function(result){console.log(result)}, { type: 'upload', prefix: 'my_photos/' });
然后,我收到了这个错误
/home/fw66/WebstormProjects/cloudinary_npm/samples/photo_album/node_modules/cloudinary/lib/utils.js:1023
          return callback(void 0, result);
                 ^
TypeError: object is not a function
    at /home/fw66/WebstormProjects/cloudinary_npm/samples/photo_album/node_modules/cloudinary/lib/utils.js:1023:18
    at IncomingMessage. (/home/fw66/WebstormProjects/cloudinary_npm/samples/photo_album/node_modules/cloudinary/lib/api.js:103:51)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:944:16
    at process._tickCallback (node.js:458:13)</p>

<p>Process finished with exit code 8
但是当我尝试如下操作时,我得到了所有已上传图像的列表,而不管指定的文件夹是什么。
cloudinary.api.resources().then(function(result){console.log(result)}, { type: 'upload', prefix: 'my_photos/' });
但我只需要且只需要上传到上述文件夹内的图像,而不是外部文件夹。那么,我该怎么做?
2. 当我尝试列出名为 my_videos something 的文件夹中的所有视频时像下面这样
cloudinary.api.resources(function(result){console.log(result)}, { resource_type: 'video', prefix: 'my_videos/' });
然后,我遇到了与上图相同的错误。
当我尝试类似下面的操作时,我会在 api 响应中获取所有已上传图片的列表,而不是视频。
cloudinary.api.resources().then(function(result){console.log(result)}, { resource_type: 'video', prefix: 'my_videos/' });
我不确定我的代码有什么问题。请帮帮我。

谢谢!

我假设您使用的 cloudinary 是使用 require('cloudinary').v2 定义的,在这种情况下,您应该使用稍微不同的语法。

本质上,回调函数应该放在最后并接收 errorresult 参数。另外请注意,一旦您提供了参数哈希,还必须提供 type。例如:

cloudinary.api.resources({type:"upload",prefix:"my_photos/"}, function(error, result){console.log(error, result)})