简单拷贝
- // endpoint以杭州为例,其它region请按实际情况填写
- String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
- // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建
- String accessKeyId = "<yourAccessKeyId>";
- String accessKeySecret = "<yourAccessKeySecret>";
- // 创建OSSClient实例
- OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
- // 拷贝Object
- CopyObjectResult result = ossClient.copyObject("<srcBucketName>", "<srcKey>", "<destBucketName>", "<destKey>");
- System.out.println("ETag: " + result.getETag() + " LastModified: " + result.getLastModified());
- // 关闭client
- ossClient.shutdown();
通过CopyObjectRequest拷贝
也可以通过 CopyObjectRequest 实现Object的拷贝:
- // endpoint以杭州为例,其它region请按实际情况填写
- String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
- // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建
- String accessKeyId = "<yourAccessKeyId>";
- String accessKeySecret = "<yourAccessKeySecret>";
- // 创建OSSClient实例
- OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
- // 创建CopyObjectRequest对象
- CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketName, srcKey, destBucketName, destKey);
- // 设置新的Metadata
- ObjectMetadata meta = new ObjectMetadata();
- meta.setContentType("text/html");
- copyObjectRequest.setNewObjectMetadata(meta);
- // 复制Object
- CopyObjectResult result = ossClient.copyObject(copyObjectRequest);
- System.out.println("ETag: " + result.getETag() + " LastModified: " + result.getLastModified());
- // 关闭client
- ossClient.shutdown();
拷贝大文件
CopyObject只能copy小于1GB的文件,大文件需要使用分片拷贝(Upload Part Copy)。分片拷贝更详细的说明,请参考
Upload Part Copy。分片拷贝分为三步:
- 初始化分片拷贝任务OSSClient.initiateMultipartUpload;
- 分片拷贝OSSClient.uploadPartCopy,除最后一个分片外,其它的分片大小都要大于100KB;
- 提交分片拷贝任务OSSClient.completeMultipartUpload。
提示:
- String sourceBucketName = "<sourceBucketName>";
- String sourceKey = "<sourceKey>";
- String targetBucketName = "<targetBucketName>";
- String targetKey = "<targetKey>";
- // 得到被拷贝object大小
- ObjectMetadata objectMetadata = ossClient.getObjectMetadata(sourceBucketName, sourceKey);
- long contentLength = objectMetadata.getContentLength();
- // 分片大小,10MB
- long partSize = 1024 * 1024 * 10;
- // 计算分块数目
- int partCount = (int) (contentLength / partSize);
- if (contentLength % partSize != 0) {
- partCount++;
- }
- System.out.println("total part count:" + partCount);
- // 初始化拷贝任务
- InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(targetBucketName, targetKey);
- InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
- String uploadId = initiateMultipartUploadResult.getUploadId();
- // 分片拷贝
- List<PartETag> partETags = new ArrayList<PartETag>();
- for (int i = 0; i < partCount; i++) {
- // 计算每个分块的大小
- long skipBytes = partSize * i;
- long size = partSize < contentLength - skipBytes ? partSize : contentLength - skipBytes;
- // 创建UploadPartCopyRequest
- UploadPartCopyRequest uploadPartCopyRequest =
- new UploadPartCopyRequest(sourceBucketName, sourceKey, targetBucketName, targetKey);
- uploadPartCopyRequest.setUploadId(uploadId);
- uploadPartCopyRequest.setPartSize(size);
- uploadPartCopyRequest.setBeginIndex(skipBytes);
- uploadPartCopyRequest.setPartNumber(i + 1);
- UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
- // 将返回的PartETag保存到List中
- partETags.add(uploadPartCopyResult.getPartETag());
- }
- // 提交分片拷贝任务
- CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(
- targetBucketName, targetKey, uploadId, partETags);
- ossClient.completeMultipartUpload(completeMultipartUploadRequest);
提示:
- 分片拷贝时,可以指定目标object的元信息,通过InitiateMultipartUploadRequest指定;
- 分片拷贝也支持限定条件,通过UploadPartCopyRequest指定。
解冻归档文件
归档(Archive)类型的Object,解冻之后(Restore)后才能读取。详细说明请参看
归档存储。归档文件的状态变换过程如下:
- 归档类型的文件初始时处于冷冻状态;
- 提交解冻(Restore)操作后,服务端执行解冻,文件处于解冻中;
- 完成解冻后,可以读取Object;
- 解冻状态默认持续1天,最多延长7天,之后文件又回到冷冻状态。
- ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, key);
- // check whether the object is archive class
- StorageClass storageClass = objectMetadata.getObjectStorageClass();
- if (storageClass == StorageClass.Archive) {
- // restore object
- ossClient.restoreObject(bucketName, key);
- // wait for restore completed
- do {
- Thread.sleep(1000);
- objectMetadata = ossClient.getObjectMetadata(bucketName, key);
- } while (!objectMetadata.isRestoreCompleted());
- }
- // get restored object
- OSSObject ossObject = ossClient.getObject(bucketName, key);
- ossObject.getObjectContent().close();
提示:
- 上述示例的完整代码请参看 GitHUb;
- 非归档类型的文件,不要调用 restoreObject 接口。