开发者社区> 问答> 正文

如何使用云相册中的Java SDK上传照片



前提条件


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

完整示例代码


相关介绍请见代码中的注释 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.");
        }
    }
}

展开
收起
云栖大讲堂 2017-10-27 13:09:27 1894 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
从 SDK 到编解码:视频直播架构解析 立即下载
跨平台的云服务SDK需要什么 立即下载
一个跨平台的云服务SDK需要什么 立即下载