全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 阿里云办公 培训与认证 物联网

示例 - 上传照片

更新时间:2017-09-07 16:26:30

前提条件

请首先阅读并了解智能云相册的上传流程

完整示例代码

相关介绍请见代码中的注释

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import com.aliyun.oss.OSSClient;
  4. import com.aliyuncs.DefaultAcsClient;
  5. import com.aliyuncs.cloudphoto.model.v20170711.CreatePhotoRequest;
  6. import com.aliyuncs.cloudphoto.model.v20170711.CreatePhotoResponse;
  7. import com.aliyuncs.cloudphoto.model.v20170711.CreateTransactionRequest;
  8. import com.aliyuncs.cloudphoto.model.v20170711.CreateTransactionResponse;
  9. import com.aliyuncs.cloudphoto.model.v20170711.CreateTransactionResponse.Transaction;
  10. import com.aliyuncs.cloudphoto.model.v20170711.CreateTransactionResponse.Transaction.Upload;
  11. import com.aliyuncs.profile.DefaultProfile;
  12. import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials;
  13. import org.apache.commons.codec.digest.DigestUtils;
  14. public class CreatePhotoDemo {
  15. public static void main(String[] args) throws Exception {
  16. // 和业务服务器的对接点
  17. // 从业务服务器获得访问智能云相册临时访问凭证、PhotoStore的名称和智能云相册的服务地域
  18. Credentials credentials = getCredentialFromServer();
  19. String storeName = "<your_photo_store_name>";
  20. String region = "cn-shanghai";
  21. // 初始化客户端 - 在应用生命周期可以只初始化一次,对象可以重用。
  22. // 如果访问凭证过期,需要重新获取
  23. DefaultProfile profile = DefaultProfile.getProfile(region, credentials.getAccessKeyId(),
  24. credentials.getAccessKeySecret(), credentials.getSecurityToken());
  25. DefaultAcsClient acsClient = new DefaultAcsClient(profile);
  26. // 待上传文件的信息,计算其md5
  27. String filepath = "<your_local_file_path>";
  28. File file = new File(filepath);
  29. FileInputStream fin = null;
  30. String md5;
  31. try {
  32. fin = new FileInputStream(file);
  33. md5 = DigestUtils.md5Hex(fin);
  34. fin.close();
  35. } finally {
  36. if (fin != null) {
  37. fin.close();
  38. }
  39. }
  40. // 步骤一:开启上传事务:从上传事务中获取上传文件到OSS的临时密钥和其他创建照片需要的信息
  41. // 1.1 构建请求对象
  42. CreateTransactionRequest request = new CreateTransactionRequest();
  43. request.setMd5(md5);
  44. // 设置文件的扩展名帮助服务端判断文件的类型
  45. request.setExt("jpg");
  46. request.setForce(true);
  47. request.setSize(file.length());
  48. // 指定上传到哪个PhotoStore
  49. request.setStoreName(storeName);
  50. // 1.2 发起请求:
  51. CreateTransactionResponse createTransactionResponse = acsClient.getAcsResponse(request);
  52. Transaction tnx = createTransactionResponse.getTransaction();
  53. Upload upload = tnx.getUpload();
  54. // 步骤二:以下两行代码为构建OSS客户端并上传照片到OSS,本例子采用OSS的简单上传。也可以采用OSS的分块上传,具体参见OSS的文档
  55. OSSClient ossClient = new OSSClient(upload.getOssEndpoint(), upload.getAccessKeyId(),
  56. upload.getAccessKeySecret(), upload.getStsToken());
  57. ossClient.putObject(upload.getBucket(), upload.getObjectKey(), file);
  58. // 步骤三:提交上传事务,完成上传
  59. CreatePhotoRequest createPhotoRequest = new CreatePhotoRequest();
  60. createPhotoRequest.setStoreName(storeName);
  61. createPhotoRequest.setFileId(upload.getFileId());
  62. createPhotoRequest.setSessionId(upload.getSessionId());
  63. createPhotoRequest.setPhotoTitle(file.getName());
  64. createPhotoRequest.setUploadType("manual");
  65. CreatePhotoResponse createPhotoResponse = acsClient.getAcsResponse(createPhotoRequest);
  66. if ("Success".equalsIgnoreCase(createPhotoResponse.getCode())) {
  67. System.out.println("create photo succeeds.");
  68. }
  69. }
  70. }
本文导读目录