前提条件
请首先阅读并了解智能云相册的
上传流程。
完整示例代码
相关介绍请见代码中的注释
import java.io.File;
import java.io.FileInputStream;
import com.aliyun.oss.OSSClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.cloudphoto.model.v20170711.CreatePhotoRequest;
import com.aliyuncs.cloudphoto.model.v20170711.CreatePhotoResponse;
import com.aliyuncs.cloudphoto.model.v20170711.CreateTransactionRequest;
import com.aliyuncs.cloudphoto.model.v20170711.CreateTransactionResponse;
import com.aliyuncs.cloudphoto.model.v20170711.CreateTransactionResponse.Transaction;
import com.aliyuncs.cloudphoto.model.v20170711.CreateTransactionResponse.Transaction.Upload;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials;
import org.apache.commons.codec.digest.DigestUtils;
public class CreatePhotoDemo {
public static void main(String[] args) throws Exception {
// 和业务服务器的对接点
// 从业务服务器获得访问智能云相册临时访问凭证、PhotoStore的名称和智能云相册的服务地域
Credentials credentials = getCredentialFromServer();
String storeName = "<your_photo_store_name>";
String region = "cn-shanghai";
// 初始化客户端 - 在应用生命周期可以只初始化一次,对象可以重用。
// 如果访问凭证过期,需要重新获取
DefaultProfile profile = DefaultProfile.getProfile(region, credentials.getAccessKeyId(),
credentials.getAccessKeySecret(), credentials.getSecurityToken());
DefaultAcsClient acsClient = new DefaultAcsClient(profile);
// 待上传文件的信息,计算其md5
String filepath = "<your_local_file_path>";
File file = new File(filepath);
FileInputStream fin = null;
String md5;
try {
fin = new FileInputStream(file);
md5 = DigestUtils.md5Hex(fin);
fin.close();
} finally {
if (fin != null) {
fin.close();
}
}
// 步骤一:开启上传事务:从上传事务中获取上传文件到OSS的临时密钥和其他创建照片需要的信息
// 1.1 构建请求对象
CreateTransactionRequest request = new CreateTransactionRequest();
request.setMd5(md5);
// 设置文件的扩展名帮助服务端判断文件的类型
request.setExt("jpg");
request.setForce(true);
request.setSize(file.length());
// 指定上传到哪个PhotoStore
request.setStoreName(storeName);
// 1.2 发起请求:
CreateTransactionResponse createTransactionResponse = acsClient.getAcsResponse(request);
Transaction tnx = createTransactionResponse.getTransaction();
Upload upload = tnx.getUpload();
// 步骤二:以下两行代码为构建OSS客户端并上传照片到OSS,本例子采用OSS的简单上传。也可以采用OSS的分块上传,具体参见OSS的文档
OSSClient ossClient = new OSSClient(upload.getOssEndpoint(), upload.getAccessKeyId(),
upload.getAccessKeySecret(), upload.getStsToken());
ossClient.putObject(upload.getBucket(), upload.getObjectKey(), file);
// 步骤三:提交上传事务,完成上传
CreatePhotoRequest createPhotoRequest = new CreatePhotoRequest();
createPhotoRequest.setStoreName(storeName);
createPhotoRequest.setFileId(upload.getFileId());
createPhotoRequest.setSessionId(upload.getSessionId());
createPhotoRequest.setPhotoTitle(file.getName());
createPhotoRequest.setUploadType("manual");
CreatePhotoResponse createPhotoResponse = acsClient.getAcsResponse(createPhotoRequest);
if ("Success".equalsIgnoreCase(createPhotoResponse.getCode())) {
System.out.println("create photo succeeds.");
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。