简体   繁体   English

如何使用Cloudinary API列出Cloudinary中的所有Public_Id?

[英]How to List all Public_Ids in Cloudinary using Cloudinary API?

I have tried my best to make this code work but, Alas! 我已经尽力使此代码起作用,但是,but! Something is definitely wrong. 肯定有问题。 I am tried to list all public_ids in Cloudinary. 我试图列出Cloudinary中的所有public_id。 but it always prints, null. 但它总是打印,为null。 Below is the code - 下面是代码-

import java.util.HashMap;
import java.util.Map;

import com.cloudinary.Api;
import com.cloudinary.Cloudinary;
import com.cloudinary.utils.ObjectUtils;

public class AllResources {

    @SuppressWarnings("unchecked")
    public static void main(String[] Args) {

        Map<String, String> config = new HashMap<>();
        config.put("cloud_name", "*******");
        config.put("api_key", "**************");
        config.put("api_secret", "***************************");
        Cloudinary c = new Cloudinary(config);
        Api api = c.api();

        try {           
            Map<String, Object> result = api.resources(ObjectUtils.asMap());
            System.out.println(result.get("public_id"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

You can do something like the following: 您可以执行以下操作:

String nextCursor = null;
do {
 result = api.resources(ObjectUtils.asMap("max_results", 500, "next_cursor", nextCursor));
 nextCursor = result.get("next_cursor");

 for (Map.Entry<String, Object> data : result.entrySet()) { 
   String key = data.getKey().toString(); 
   Object value = data.getValue(); 
   System.out.println(key + value); 
 }
} while (nextCursor != null);

Itay's code is pseudo code / won't compile. Itay的代码是伪代码/无法编译。 Here is a working version: 这是一个工作版本:

Cloudinary cloudinaryApi = new Cloudinary(
        ObjectUtils.asMap(
            "cloud_name", "YOUR CLOUD NAME", 
            "api_key", "YOUR API KEY", 
            "api_secret", "YOUR SECRET KEY"));
ApiResponse result = null;
String nextCursor = null;
do {
    try {
        result = cloudinaryApi.api().resources(ObjectUtils.asMap("max_results", 500, "next_cursor", nextCursor));
        nextCursor = result.containsKey("next_cursor") ? result.get("next_cursor").toString() : null;

        if(result.containsKey("resources")) {
            List<Map<String,Object>> resources = (ArrayList<Map<String,Object>>) result.get("resources");
            for (Map<String,Object> resource : resources) {
                if(resource.containsKey("public_id")) {
                    String publicId = resource.get("public_id").toString();
                    System.out.println(publicId);
                }
            }
        }
    }
    catch (Exception e) {
        nextCursor = null;
        LOG.error(e.getMessage());
    }
} while (nextCursor != null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM