开发者指南

API 文档

SDK 文档

上传与复制对象

最近更新时间:2020-08-20 15:55:37

简介

本文档提供关于对象的上传、复制操作相关的 API 概览以及 SDK 示例代码。

简单操作

API 操作名 操作描述
PUT Object 简单上传对象 上传一个对象至存储桶
POST Object 表单上传对象 使用表单请求上传对象
PUT Object - Copy 设置对象复制(修改对象属性) 复制文件到目标路径

分块操作

API 操作名 操作描述
List Multipart Uploads 查询分块上传 查询正在进行中的分块上传信息
Initiate Multipart Upload 初始化分块上传 初始化分块上传操作
Upload Part 上传分块 分块上传对象
Upload Part - Copy 复制分块 将其他对象复制为一个分块
List Parts 查询已上传块 查询特定分块上传操作中的已上传的块
Complete Multipart Upload 完成分块上传 完成整个文件的分块上传
Abort Multipart Upload 终止分块上传 终止一个分块上传操作并删除已上传的块

SDK API 参考

SDK 所有接口的具体参数与方法说明,请参考 SDK API

高级接口(推荐)

上传对象

高级接口封装了简单上传、分块上传接口,根据文件大小智能的选择上传方式,同时支持续传功能。

示例代码一: 上传本地文件

// 初始化 TransferConfig
TransferConfig transferConfig = new TransferConfig();

// 初始化 TransferManager
TransferManager transferManager = new TransferManager(cosXml, transferConfig);

String bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键
String srcPath = @"temp-source-file";//本地文件绝对路径

// 上传对象
COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);
uploadTask.SetSrcPath(srcPath);

uploadTask.progressCallback = delegate (long completed, long total)
{
    Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
};
uploadTask.successCallback = delegate (CosResult cosResult) 
{
    COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = cosResult 
      as COSXML.Transfer.COSXMLUploadTask.UploadTaskResult;
    Console.WriteLine(result.GetResultInfo());
    string eTag = result.eTag;
};
uploadTask.failCallback = delegate (CosClientException clientEx, CosServerException serverEx) 
{
    if (clientEx != null)
    {
        Console.WriteLine("CosClientException: " + clientEx);
    }
    if (serverEx != null)
    {
        Console.WriteLine("CosServerException: " + serverEx.GetInfo());
    }
};
transferManager.Upload(uploadTask);
说明:

  • 更多完整示例,请前往 GitHub 查看。
  • 上传之后,您可以用同样的 Key 生成文件下载链接,具体使用方法见 生成预签名链接 文档。但注意如果您的文件是私有读权限,那么下载链接只有一定的有效期。

示例代码二: 上传二进制数据

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string cosPath = "exampleObject"; // 对象键
  byte[] data = new byte[1024]; // 二进制数据
  PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, data);

  cosXml.PutObject(putObjectRequest);
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

  • 更多完整示例,请前往 GitHub 查看。
  • 上传之后,您可以用同样的 Key 生成文件下载链接,具体使用方法见 生成预签名链接 文档。但注意如果您的文件是私有读权限,那么下载链接只有一定的有效期。

示例代码三: 上传暂停、继续与取消

对于上传任务,可以通过以下方式暂停:

uploadTask.Pause();

暂停之后,可以通过以下方式续传:

uploadTask.Resume();

也通过以下方式取消上传:

uploadTask.Cancel();
说明:

  • 更多完整示例,请前往 GitHub 查看。

示例代码四: 批量上传

TransferConfig transferConfig = new TransferConfig();

// 初始化 TransferManager
TransferManager transferManager = new TransferManager(cosXml, transferConfig);

string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID

for (int i = 0; i < 5; i++) {
  // 上传对象
  string cosPath = "exampleobject" + i; //对象在存储桶中的位置标识符,即称对象键
  string srcPath = @"temp-source-file";//本地文件绝对路径
  COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath); 
  uploadTask.SetSrcPath(srcPath);
  transferManager.Upload(uploadTask);
}

示例代码五:创建目录

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string cosPath = "dir/"; // 对象键
  PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, new byte[0]);

  cosXml.PutObject(putObjectRequest);
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

  • 更多完整示例,请前往 GitHub 查看。
  • 上传之后,您可以用同样的 Key 生成文件下载链接,具体使用方法见 生成预签名链接 文档。但注意如果您的文件是私有读权限,那么下载链接只有一定的有效期。

复制对象

高级接口封装了简单复制、分块复制接口的异步请求,并支持暂停、恢复以及取消复制请求。

示例代码

string sourceAppid = "1250000000"; //账号 appid
string sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶
string sourceRegion = "COS_REGION"; //源对象的存储桶所在的地域
string sourceKey = "sourceObject"; //源对象键
//构造源对象属性
CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket, 
    sourceRegion, sourceKey);

string bucket = "examplebucket-1250000000"; //目标存储桶,格式:BucketName-APPID
string key = "exampleobject"; //目标对象的对象键

COSXMLCopyTask copytask = new COSXMLCopyTask(bucket, key, copySource);

copytask.successCallback = delegate (CosResult cosResult) 
{
    COSXML.Transfer.COSXMLCopyTask.CopyTaskResult result = cosResult 
      as COSXML.Transfer.COSXMLCopyTask.CopyTaskResult;
    Console.WriteLine(result.GetResultInfo());
    string eTag = result.eTag;
};
copytask.failCallback = delegate (CosClientException clientEx, CosServerException serverEx) 
{
    if (clientEx != null)
    {
        Console.WriteLine("CosClientException: " + clientEx);
    }
    if (serverEx != null)
    {
        Console.WriteLine("CosServerException: " + serverEx.GetInfo());
    }
};
transferManager.Copy(copytask);
说明:

更多完整示例,请前往 GitHub 查看。

简单操作

简单上传对象

功能说明

PUT Object 接口可以上传一个对象至指定存储桶中,该操作需要请求者对存储桶有 WRITE 权限。最大支持上传不超过5GB的对象,5GB以上对象请使用 分块上传高级接口 上传。

注意:

  1. Key(文件名)不能以/结尾,否则会被识别为文件夹。
  2. 每个主账号(即同一个 APPID),存储桶的 ACL 规则数量最多为1000条,对象 ACL 规则数量不限制。如果您不需要进行对象 ACL 控制,请在上传时不要设置,默认继承存储桶权限。

示例代码

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  string srcPath = @"temp-source-file";//本地文件绝对路径

  PutObjectRequest request = new PutObjectRequest(bucket, key, srcPath);
  //设置进度回调
  request.SetCosProgressCallback(delegate (long completed, long total)
  {
    Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
  });
  //执行请求
  PutObjectResult result = cosXml.PutObject(request);
  //对象的 eTag
  string eTag = result.eTag;
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

  • 更多完整示例,请前往 GitHub 查看。
  • 上传之后,您可以用同样的 Key 生成文件下载链接,具体使用方法见 生成预签名链接 文档。但注意如果您的文件是私有读权限,那么下载链接只有一定的有效期。

表单上传对象

功能说明

使用表单请求上传对象。

示例代码

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  string srcPath = @"temp-source-file";//本地文件绝对路径
  PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath);
  //设置进度回调
  request.SetCosProgressCallback(delegate (long completed, long total)
  {
    Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
  });
  //执行请求
  PostObjectResult result = cosXml.PostObject(request);
  //请求成功
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

复制对象(修改属性)

复制文件到目标路径(PUT Object-Copy)。

示例代码一: 复制对象时保留对象属性

try
{
  string sourceAppid = "1250000000"; //账号 appid
  string sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶
  string sourceRegion = "COS_REGION"; //源对象的存储桶所在的地域
  string sourceKey = "sourceObject"; //源对象键
  //构造源对象属性
  CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket, 
    sourceRegion, sourceKey);

  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  CopyObjectRequest request = new CopyObjectRequest(bucket, key);
  //设置拷贝源
  request.SetCopySource(copySource);
  //设置是否拷贝还是更新,此处是拷贝
  request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.COPY);
  //执行请求
  CopyObjectResult result = cosXml.CopyObject(request);
  //请求成功
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

示例代码二: 复制对象时替换对象属性

try
{
  string sourceAppid = "1250000000"; //账号 appid
  string sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶
  string sourceRegion = "COS_REGION"; //源对象的存储桶所在的地域
  string sourceKey = "sourceObject"; //源对象键
  //构造源对象属性
  CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket, 
    sourceRegion, sourceKey);

  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  CopyObjectRequest request = new CopyObjectRequest(bucket, key);
  //设置拷贝源
  request.SetCopySource(copySource);
  //设置是否拷贝还是更新,此处是拷贝
  request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.REPLACED);
  // 替换元数据
  request.SetRequestHeader("Content-Disposition", "attachment; filename=example.jpg");
  //执行请求
  CopyObjectResult result = cosXml.CopyObject(request);
  //请求成功
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

示例代码三: 修改对象元数据

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  string appId = "1250000000"; //账号 appid
  string region = "COS_REGION"; //源对象的存储桶所在的地域
  //构造对象属性
  CopySourceStruct copySource = new CopySourceStruct(appId, bucket, 
    region, key);

  CopyObjectRequest request = new CopyObjectRequest(bucket, key);
  //设置拷贝源
  request.SetCopySource(copySource);
  //设置是否拷贝还是更新,此处是拷贝
  request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.REPLACED);
  // 替换元数据
  request.SetRequestHeader("Content-Disposition", "attachment; filename=example.jpg");
  request.SetRequestHeader("Content-Type", "image/png");
  //执行请求
  CopyObjectResult result = cosXml.CopyObject(request);
  //请求成功
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

示例代码四: 修改对象存储类型

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  string appId = "1250000000"; //账号 appid
  string region = "COS_REGION"; //源对象的存储桶所在的地域
  //构造对象属性
  CopySourceStruct copySource = new CopySourceStruct(appId, bucket, 
    region, key);

  CopyObjectRequest request = new CopyObjectRequest(bucket, key);
  //设置拷贝源
  request.SetCopySource(copySource);
  //设置是否拷贝还是更新,此处是拷贝
  request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.REPLACED);
  // 修改为归档存储
  request.SetCosStorageClass(CosStorageClass.ARCHIVE);
  //执行请求
  CopyObjectResult result = cosXml.CopyObject(request);
  //请求成功
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

分块操作

这里说明下分块上传的流程。

分块上传与复制的流程

  1. 初始化分块上传(Initiate Multipart Upload),得到 UploadId
  2. 使用 UploadId 上传分块(Upload Part),或者复制分块(Upload Part Copy)
  3. 完成分块上传(Complete Multipart Upload)

分块继续上传与复制的流程

  1. 如果没有记录 UploadId,查询分块上传任务(List Multipart Uploads),得到对应文件的 UploadId
  2. 使用 UploadId 列出已上传的分块(List Parts)
  3. 使用 UploadId 上传剩余的分块(Upload Part),或者复制剩余的分块(Upload Part Copy)
  4. 完成分块上传(Complete Multipart Upload)

终止分块上传与复制的流程

  1. 如果没有记录 UploadId,查询分块上传任务(List Multipart Uploads),得到对应文件的 UploadId
  2. 终止分块上传并删除已上传分块(Abort Multipart Upload)

查询分块上传

功能说明

查询指定存储桶中正在进行的分块上传(List Multipart Uploads)。

示例代码

try
{
  string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
  ListMultiUploadsRequest request = new ListMultiUploadsRequest(bucket);
  //执行请求
  ListMultiUploadsResult result = cosXml.ListMultiUploads(request);
  //请求成功
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

初始化分块上传

功能说明

初始化 Multipart Upload 上传操作,获取对应的 uploadId(Initiate Multipart Upload)。

示例代码

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  InitMultipartUploadRequest request = new InitMultipartUploadRequest(bucket, key);
  //执行请求
  InitMultipartUploadResult result = cosXml.InitMultipartUpload(request);
  //请求成功
  this.uploadId = result.initMultipartUpload.uploadId; //用于后续分块上传的 uploadId
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

上传分块

分块上传对象(Upload Part)。

示例代码

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  string uploadId = "exampleUploadId"; //初始化分块上传返回的uploadId
  int partNumber = 1; //分块编号,必须从1开始递增
  string srcPath = @"temp-source-file";//本地文件绝对路径
  UploadPartRequest request = new UploadPartRequest(bucket, key, partNumber, 
    uploadId, srcPath);
  //设置进度回调
  request.SetCosProgressCallback(delegate (long completed, long total)
  {
    Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
  });
  //执行请求
  UploadPartResult result = cosXml.UploadPart(request);
  //请求成功
  //获取返回分块的eTag,用于后续CompleteMultiUploads
  this.eTag = result.eTag;
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

复制分块

功能说明

将其他对象复制为一个分块 (Upload Part-Copy)。

示例代码

try
{
  string sourceAppid = "1250000000"; //账号 appid
  string sourceBucket = "sourcebucket-1250000000"; //"源对象所在的存储桶
  string sourceRegion = "COS_REGION"; //源对象的存储桶所在的地域
  string sourceKey = "sourceObject"; //源对象键
  //构造源对象属性
  COSXML.Model.Tag.CopySourceStruct copySource = new CopySourceStruct(sourceAppid, 
    sourceBucket, sourceRegion, sourceKey);

  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  string uploadId = this.uploadId; //初始化分块上传返回的uploadId
  int partNumber = 1; //分块编号,必须从1开始递增
  UploadPartCopyRequest request = new UploadPartCopyRequest(bucket, key, 
    partNumber, uploadId);
  //设置拷贝源
  request.SetCopySource(copySource);
  //设置复制分块(指定块的范围,如 0 ~ 1M)
  request.SetCopyRange(0, 1024 * 1024);
  //执行请求
  UploadPartCopyResult result = cosXml.PartCopy(request);
  //请求成功
  //获取返回分块的eTag,用于后续CompleteMultiUploads
  this.eTag = result.copyObject.eTag;
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

查询已上传的分块

功能说明

查询特定分块上传操作中的已上传的块(List Parts)。

示例代码

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  string uploadId = "exampleUploadId"; //初始化分块上传返回的uploadId
  ListPartsRequest request = new ListPartsRequest(bucket, key, uploadId);
  //执行请求
  ListPartsResult result = cosXml.ListParts(request);
  //请求成功
  //列举已上传的分块
  List<COSXML.Model.Tag.ListParts.Part> alreadyUploadParts = result.listParts.parts;
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

完成分块上传

功能说明

完成整个文件的分块上传(Complete Multipart Upload)。

示例代码

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  string uploadId = "exampleUploadId"; //初始化分块上传返回的uploadId
  CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucket, 
    key, uploadId);
  //设置已上传的parts,必须有序,按照partNumber递增
  request.SetPartNumberAndETag(1, this.eTag);
  //执行请求
  CompleteMultipartUploadResult result = cosXml.CompleteMultiUpload(request);
  //请求成功
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

终止分块上传

功能说明

终止一个分块上传操作并删除已上传的块(Abort Multipart Upload)。

示例代码

try
{
  string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
  string key = "exampleobject"; //对象键
  string uploadId = "exampleUploadId"; //初始化分块上传返回的uploadId
  AbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucket, key, uploadId);
  //执行请求
  AbortMultipartUploadResult result = cosXml.AbortMultiUpload(request);
  //请求成功
  Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
  //请求失败
  Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
  //请求失败
  Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:

更多完整示例,请前往 GitHub 查看。

目录