拷贝文件
在同一个区域(杭州,深圳,青岛等)中,用户可以对有操作权限的文件进行复制操作。
拷贝一个文件
通过 copyObject 方法我们可以拷贝一个文件,代码如下:
- using Aliyun.OSS;
- // 初始化OssClient
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- /// <summary>
- /// 拷贝文件
- /// </summary>
- /// <param name="sourceBucket">原文件所在存储空间的名称</param>
- /// <param name="sourceKey">原文件的名称</param>
- /// <param name="targetBucket">目标文件所在存储空间的名称</param>
- /// <param name="targetKey">目标文件的名称</param>
- public void CopyObect(string sourceBucket, string sourceKey, string targetBucket, string targetKey)
- {
- try
- {
- var metadata = new ObjectMetadata();
- metadata.AddHeader(Util.HttpHeaders.ContentType, "text/html");
- var req = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey)
- {
- NewObjectMetadata = metadata
- };
- var ret = client.CopyObject(req);
- Console.WriteLine("Copy object succeeded");
- Console.WriteLine("文件的ETag:{0}", ret.ETag);
- }
- catch (Exception ex)
- {
- Console.WriteLine("Copy object failed. {0}", ex.Message);
- }
- }
说明:
- 完整代码参考GitHub。
- 使用该方法拷贝的文件必须小于1G,否则会报错。若文件大于1G,使用下面的Upload Part Copy。
拷贝大文件
初始化Multipart Upload
我们使用 initiateMultipartUpload 方法来初始化一个分片上传事件:
- using Aliyun.OSS;
- // 初始化OssClient
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- public void InitiateMultipartUpload(string bucketName, string key)
- {
- try
- {
- // 开始Multipart Upload
- var request = new InitiateMultipartUploadRequest(bucketName, key);
- var result = client.InitiateMultipartUpload(request);
- // 打印UploadId
- Console.WriteLine("Init multipart upload succeeded");
- Console.WriteLine("Upload Id:{0}", result.UploadId);
- }
- catch (Exception ex)
- {
- Console.WriteLine("Init multipart upload failed. {0}", ex.Message);
- }
- }
说明: 完整代码参考
GitHub。
Upload Part Copy拷贝上传
Upload Part Copy 通过从一个已经存在文件的中拷贝数据来上传一个新文件。当拷贝一个大于500MB的文件,建议使用Upload Part Copy的方式来进行拷贝。
- using Aliyun.OSS;
- // 初始化OssClient
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- public void UploadPartCopy(string sourceBucket, string sourceKey, string targetBucket, string targetKey, string uploadId)
- {
- try
- {
- // 计算总共的分片个数
- var metadata = client.GetObjectMetadata(sourceBucket, sourceKey);
- var fileSize = metadata.ContentLength;
- var partCount = (int)fileSize / partSize;
- if (fileSize % partSize != 0)
- {
- partCount++;
- }
- // 开始分片拷贝
- var partETags = new List<PartETag>();
- for (var i = 0; i < partCount; i++)
- {
- var skipBytes = (long)partSize * i;
- var size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
- var request = new UploadPartCopyRequest(targetBucket, targetKey, sourceBucket, sourceKey, uploadId)
- {
- PartSize = size,
- PartNumber = i + 1,
- BeginIndex = skipBytes
- };
- var result = client.UploadPartCopy(request);
- partETags.Add(result.PartETag);
- }
- Console.WriteLine("Upload part copy succeeded");
- }
- catch (Exception ex)
- {
- Console.WriteLine("Upload part copy failed. {0}", ex.Message);
- }
说明:
- 完整代码参考GitHub。
- 以上程序调用uploadPartCopy方法来拷贝每一个分片。
- 与UploadPart要求基本一致,需要通过BeginIndex来定位到此次上传片开头所对应的位置,同时需要通过SourceKey来指定拷贝的文件。