I have tried my best to make this code work but, Alas! Something is definitely wrong. I am tried to list all public_ids in Cloudinary. but it always prints, 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. 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.