拷贝文件分为拷贝小文件和拷贝大文件。
拷贝小文件
对于小于 1GB 的文件,您可以通过 client.CopyObject 方法将文件从一个存储空间(源存储空间)复制到同一地域的另一个存储空间(目标存储空间)。此方法参数指定方式如下
参数指定方式 | 描述 |
---|---|
CopyObjectOutcome OssClient::CopyObject(const CopyObjectRequest &request) | 允许指定目标文件的元信息和拷贝的限制条件。如果拷贝操作的源文件地址和目标文件地址相同,则直接替换源文件的元信息。 |
说明 此操作的限制条件如下:
- 用户有源文件的读写权限。
- 不支持跨地域拷贝。例如,不支持将杭州存储空间里的文件拷贝到青岛。
- 文件的大小不能超过1GB。
以下代码用于拷贝小文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main( void ) { /* 初始化OSS账号信息 */ std::string AccessKeyId = "yourAccessKeyId"; std::string AccessKeySecret = "yourAccessKeySecret"; std::string Endpoint = "yourEndpoint"; std::string SourceBucketName = "yourSourceBucketName"; std::string CopyBucketName = "yourCopyBucketName"; std::string SourceObjectName = "yourSourceObjectName"; std::string CopyObjectName = "yourCopyObjectName"; /* 初始化网络等资源 */ InitializeSdk(); ClientConfiguration conf; OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf); CopyObjectRequest request(CopyBucketName, CopyObjectName); request.setCopySource(SourceBucketName, SourceObjectName); /* 拷贝文件 */ auto outcome = client.CopyObject(request); if (!outcome.isSuccess()) { /* 异常处理 */ std::cout << "CopyObject fail" << ",code:" << outcome.error().Code() << ",message:" << outcome.error().Message() << ",requestId:" << outcome.error().RequestId() << std::endl; ShutdownSdk(); return -1; } /* 释放网络等资源 */ ShutdownSdk(); return 0 ; } |
拷贝大文件
对于大于1GB的文件,需要使用分片拷贝(UploadPartCopy)。分片拷贝分为三步:
- 通过 client.InitiateMultipartUpload 初始化分片拷贝任务。
- 通过 client.UploadPartCopy 进行分片拷贝。除最后一个分片外,其它分片都要大于 100KB。
- 通过 client.CompleteMultipartUpload 提交分片拷贝任务。
说明 拷贝大文件也不支持跨地域拷贝。例如,不支持将北京存储空间里的文件拷贝到青岛。
以下代码用于拷贝大文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main( void ) { /* 初始化OSS账号信息 */ std::string AccessKeyId = "yourAccessKeyId"; std::string AccessKeySecret = "yourAccessKeySecret"; std::string Endpoint = "yourEndpoint"; std::string SourceBucketName = "yourSourceBucketName"; std::string CopyBucketName = "yourCopyBucketName"; std::string SourceObjectName = "yourSourceObjectName"; std::string CopyObjectName = "yourCopyObjectName"; /* 初始化网络等资源 */ InitializeSdk(); ClientConfiguration conf; OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf); auto getObjectMetaReq = GetObjectMetaRequest(SourceBucketName, SourceObjectName); auto getObjectMetaResult = GetObjectMeta(getObjectMetaReq); if (!getObjectMetaResult.isSuccess()) { std::cout << "GetObjectMeta fail" << ",code:" << outcome.error().Code() << ",message:" << outcome.error().Message() << ",requestId:" << outcome.error().RequestId() << std::endl; ShutdownSdk(); return -1; } /* 获取被拷贝文件大小 */ auto objectSize = getObjectMetaResult.result().ContentLength(); /* 拷贝大文件 */ InitiateMultipartUploadRequest initUploadRequest(CopyBucketName, CopyObjectName); /* 初始化分片拷贝事件 */ auto uploadIdResult = client.InitiateMultipartUpload(initUploadRequest); auto uploadId = uploadIdResult.result().UploadId(); int64_t partSize = 100 * 1024; PartList partETagList; int partCount = static_cast<int>(objectSize / partSize); /* 计算分片个数*/ if (objectSize % part Size != 0) { partCount++; } /* 对每一个分片进行拷贝 */ for (int i = 1; i <= partCount; i++) { auto skipBytes = partSize * (i - 1); auto size = (partSize < objectSize - skipBytes) ? partSize : (objectSize - skipBytes); auto uploadPartCopyReq = UploadPartCopyRequest(CopyBucketName, CopyObjectName, SourceBucketName, SourceObjectName,uploadId, i + 1); uploadPartCopyReq.setCopySourceRange(skipBytes, skipBytes + size -1); auto uploadPartOutcome = client.UploadPartCopy(uploadPartCopyReq); if (uploadPartOutcome.isSuccess()) { Part part(i, uploadPartOutcome.result().ETag()); partETagList.push_back(part); } else { std::cout << "UploadPartCopy fail" << ",code:" << outcome.error().Code() << ",message:" << outcome.error().Message() << ",requestId:" << outcome.error().RequestId() << std::endl; } } /* 完成分片拷贝 */ CompleteMultipartUploadRequest request(CopyBucketName, CopyObjectName, partETagList, uploadId); auto outcome = client.CompleteMultipartUpload(request); if (!outcome.isSuccess()) { /* 异常处理 */ std::cout << "CompleteMultipartUpload fail" << ",code:" << outcome.error().Code() << ",message:" << outcome.error().Message() << ",requestId:" << outcome.error().RequestId() << std::endl; ShutdownSdk(); return -1; } /* 释放网络等资源 */ ShutdownSdk(); return 0 ; } |
分片拷贝的详细说明请参见UploadPartCopy。