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

时间:2015-07-29 12:53:28

标签: java api cloudinary

我尽力使这段代码正常工作,但是,唉!有些事情肯定是错的。我试图列出Cloudinary中的所有public_ids。但它总是打印,null。以下是代码 -

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();
        }
    }

}

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

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);

答案 1 :(得分:0)

Itay的代码是伪代码/不会编译。这是一个工作版本:

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);