开发者社区> 问答> 正文

Java-SDK之如何实现管理文件(六)?


简单拷贝

  1. // endpoint以杭州为例,其它region请按实际情况填写
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建OSSClient实例
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. // 拷贝Object
  9. CopyObjectResult result = ossClient.copyObject("<srcBucketName>", "<srcKey>", "<destBucketName>", "<destKey>");
  10. System.out.println("ETag: " + result.getETag() + " LastModified: " + result.getLastModified());
  11. // 关闭client
  12. ossClient.shutdown();


通过CopyObjectRequest拷贝


也可以通过 CopyObjectRequest 实现Object的拷贝:
  1. // endpoint以杭州为例,其它region请按实际情况填写
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // 创建OSSClient实例
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. // 创建CopyObjectRequest对象
  9. CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketName, srcKey, destBucketName, destKey);
  10. // 设置新的Metadata
  11. ObjectMetadata meta = new ObjectMetadata();
  12. meta.setContentType("text/html");
  13. copyObjectRequest.setNewObjectMetadata(meta);
  14. // 复制Object
  15. CopyObjectResult result = ossClient.copyObject(copyObjectRequest);
  16. System.out.println("ETag: " + result.getETag() + " LastModified: " + result.getLastModified());
  17. // 关闭client
  18. ossClient.shutdown();


拷贝大文件


CopyObject只能copy小于1GB的文件,大文件需要使用分片拷贝(Upload Part Copy)。分片拷贝更详细的说明,请参考 Upload Part Copy。分片拷贝分为三步:
  • 初始化分片拷贝任务OSSClient.initiateMultipartUpload;
  • 分片拷贝OSSClient.uploadPartCopy,除最后一个分片外,其它的分片大小都要大于100KB;
  • 提交分片拷贝任务OSSClient.completeMultipartUpload。

提示:
  • 分片拷贝的完整代码请参考:GitHub
  1. String sourceBucketName = "<sourceBucketName>";
  2. String sourceKey = "<sourceKey>";
  3. String targetBucketName = "<targetBucketName>";
  4. String targetKey = "<targetKey>";
  5. // 得到被拷贝object大小
  6. ObjectMetadata objectMetadata = ossClient.getObjectMetadata(sourceBucketName, sourceKey);
  7. long contentLength = objectMetadata.getContentLength();
  8. // 分片大小,10MB
  9. long partSize = 1024 * 1024 * 10;
  10. // 计算分块数目
  11. int partCount = (int) (contentLength / partSize);
  12. if (contentLength % partSize != 0) {
  13.     partCount++;
  14. }
  15. System.out.println("total part count:" + partCount);
  16. // 初始化拷贝任务
  17. InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(targetBucketName, targetKey);
  18. InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
  19. String uploadId = initiateMultipartUploadResult.getUploadId();
  20. // 分片拷贝
  21. List<PartETag> partETags = new ArrayList<PartETag>();
  22. for (int i = 0; i < partCount; i++) {
  23.      // 计算每个分块的大小
  24.     long skipBytes = partSize * i;
  25.     long size = partSize < contentLength - skipBytes ? partSize : contentLength - skipBytes;
  26.     // 创建UploadPartCopyRequest
  27.     UploadPartCopyRequest uploadPartCopyRequest =
  28.         new UploadPartCopyRequest(sourceBucketName, sourceKey, targetBucketName, targetKey);
  29.     uploadPartCopyRequest.setUploadId(uploadId);
  30.     uploadPartCopyRequest.setPartSize(size);
  31.     uploadPartCopyRequest.setBeginIndex(skipBytes);
  32.     uploadPartCopyRequest.setPartNumber(i + 1);
  33.     UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
  34.     // 将返回的PartETag保存到List中
  35.     partETags.add(uploadPartCopyResult.getPartETag());
  36. }
  37. // 提交分片拷贝任务
  38. CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(
  39.                     targetBucketName, targetKey, uploadId, partETags);
  40. ossClient.completeMultipartUpload(completeMultipartUploadRequest);

提示:
  • 分片拷贝时,可以指定目标object的元信息,通过InitiateMultipartUploadRequest指定;
  • 分片拷贝也支持限定条件,通过UploadPartCopyRequest指定。


解冻归档文件


归档(Archive)类型的Object,解冻之后(Restore)后才能读取。详细说明请参看 归档存储。归档文件的状态变换过程如下:
  • 归档类型的文件初始时处于冷冻状态;
  • 提交解冻(Restore)操作后,服务端执行解冻,文件处于解冻中;
  • 完成解冻后,可以读取Object;
  • 解冻状态默认持续1天,最多延长7天,之后文件又回到冷冻状态。
  1. ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, key);
  2. // check whether the object is archive class
  3. StorageClass storageClass = objectMetadata.getObjectStorageClass();
  4. if (storageClass == StorageClass.Archive) {
  5.     // restore object
  6.     ossClient.restoreObject(bucketName, key);
  7.     // wait for restore completed
  8.     do {
  9.         Thread.sleep(1000);
  10.         objectMetadata = ossClient.getObjectMetadata(bucketName, key);
  11.     } while (!objectMetadata.isRestoreCompleted());
  12. }
  13. // get restored object
  14. OSSObject ossObject = ossClient.getObject(bucketName, key);
  15. ossObject.getObjectContent().close();

提示:
  • 上述示例的完整代码请参看 GitHUb
  • 非归档类型的文件,不要调用 restoreObject 接口。

展开
收起
青衫无名 2017-10-18 11:01:48 1776 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
跨平台的云服务SDK需要什么 立即下载
如何通过 Serverless 提高 Java 微服务治理效 立即下载
一个跨平台的云服务SDK需要什么 立即下载