[英]How to List all Public_Ids in Cloudinary using Cloudinary API?
我已經盡力使此代碼起作用,但是,but! 肯定有問題。 我試圖列出Cloudinary中的所有public_id。 但它總是打印,為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();
}
}
}
您可以執行以下操作:
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的代碼是偽代碼/無法編譯。 這是一個工作版本:
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.