【问题标题】:How to List all Public_Ids in Cloudinary using Cloudinary API?如何使用 Cloudinary API 列出 Cloudinary 中的所有 Public_Id?
【发布时间】:2015-10-20 11:10:57
【问题描述】:

我已尽最大努力使这段代码正常工作,但是,唉!肯定有问题。我试图列出 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();
        }
    }

}

【问题讨论】:

    标签: java api cloudinary


    【解决方案1】:

    您可以执行以下操作:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2017-04-20
        • 2021-06-19
        • 2017-02-24
        • 2015-12-06
        • 2012-10-09
        • 2021-09-27
        • 2021-12-12
        • 1970-01-01
        • 2021-11-12
        相关资源
        最近更新 更多