推特 阿里云技术文档正文

示例 - 获取照片列表_Java SDK_SDK手册_智能云相册

admin 阿里云技术文档 2020-02-11 230 0
阿里云服务器优惠

示例 - 获取照片列表

以下为获取照片列表的完整代码,以展示SDK的使用方式。为了更好理解示例代码,请首先阅读同步数据一文。

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.cloudphoto.model.v20170711.ListPhotosRequest;
  5. import com.aliyuncs.cloudphoto.model.v20170711.ListPhotosResponse;
  6. import com.aliyuncs.cloudphoto.model.v20170711.ListPhotosResponse.Photo;
  7. import com.aliyuncs.exceptions.ClientException;
  8. import com.aliyuncs.profile.DefaultProfile;
  9. import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials;
  10. public class ListPhotosDemo {
  11. public static void main(String[] args) throws ClientException {
  12. // 从您的业务服务器处获取到临时访问凭证和其他访问云相册的信息
  13. Credentials credentials = getCredentialFromServer();
  14. String storeName = "<your_photo_store_name>";
  15. String cloudPhotoRegion = "cn-shanghai";
  16. // Step1: 创建DefaultAcsClient对象
  17. DefaultProfile profile = DefaultProfile.getProfile(cloudPhotoRegion, credentials.getAccessKeyId(),
  18. credentials.getAccessKeySecret(), credentials.getSecurityToken());
  19. DefaultAcsClient acsClient = new DefaultAcsClient(profile);
  20. // Step2: 从老数据开始分批获取照片数据
  21. String cursor = "0";
  22. List<Long> photoIds = new ArrayList<Long>();
  23. while (!"EOF".equalsIgnoreCase(cursor)) {
  24. ListPhotosRequest request = new ListPhotosRequest();
  25. request.setStoreName(storeName);
  26. request.setCursor(cursor);
  27. request.setDirection("forward");
  28. // 只获取正常状态的照片,每批10条数据。
  29. request.setState("active");
  30. request.setSize(10);
  31. // Step3: 发送请求并接收响应,如果没有抛异常则表示请求成功
  32. ListPhotosResponse response = acsClient.getAcsResponse(request);
  33. List<Photo> photos = response.getPhotos();
  34. for (ListPhotosResponse.Photo photo : photos) {
  35. photoIds.add(photo.getId());
  36. }
  37. // 服务端返回获取下一批数据的游标,调整游标开始获取下一批数据。
  38. cursor = response.getNextCursor();
  39. }
  40. System.out.println("Total PhotoId count = " + photoIds.size());
  41. }
  42. }

如上述示例代码所示,使用SDK可以快速接入智能云相册。

  1. 构建DefaultAcsClient对象时需要先从您的业务服务器处获取STS凭证,在STS凭证有效期内可以重用这一对象。
  2. 构建请求Request对象,需要根据不同功能提供相应的参数,发送请求就是一个方法调用,参数加签和网络请求发送都由SDK来处理。
  3. 最后是读取和解析响应,需要根据不同的功能做不同的处理。
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论

-----