对象存储服务 XML C# SDK 操作成功会返回各个 API 对应的类型,失败会抛 CosClientException 和 CosServerException。其中 CosClientException 是指客户端异常,如参数为空,网络连接失败等; CosServerException 是指服务端处理一些不符合要求的客户端请求所返回的错误,如访问不存在的文件,没有访问文件的权限等。详细请查看 SDK 异常说明。
SDK 中为各个 API 请求提供了同步和异步两种请求方法。
在执行任何和 COS 服务相关请求之前,都需要先实例化CosXmlConfig , QCloudCredentialProvider , CosXmlServer
3个对象. 其中
CosXmlConfig
提供了配置 SDK 的接口.QCloudCredentialProvider
提供了设置密钥信息接口.CosXmlServer
提供了各种 COS API 服务接口
//初始化 CosXmlConfig
CosXmlConfig config = new CosXmlConfig.Builder()
.SetConnectionTimeoutMs(60000) //设置连接超时时间,单位 毫秒 ,默认 45000ms
.SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位 毫秒 ,默认 45000ms
.IsHttps(true) //设置默认 https 请求
.SetAppid(appid) //设置腾讯云账户的账户标识 APPID
.SetRegion(region) //设置一个默认的存储桶地域
.SetDebugLog(true) //显示日志
.Build(); //创建 CosXmlConfig 对象
//初始化 QCloudCredentialProvider ,SDK中提供了3种方式:永久密钥 、 临时密钥 、 自定义
QCloudCredentialProvider cosCredentialProvider = null;
/**
//方式1, 永久密钥
string secretId = "云 API 密钥 SecretId";
string secretKey = "云 API 密钥 SecretKey";
long durationSecond = 600; //有效时长,单位为 秒
cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond);
*/
//方式2, 临时密钥 **(推荐)**
string tmpSecretId = "临时密钥 SecretId";
string tmpSecretKey = "临时密钥 SecretKey";
string tmpToken = "临时密钥 token";
long tmpExpireTime = 1546862502;//临时密钥有效截止时间
cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(tmpSecretId,tmpSecretKey,tmpExpireTime,tmpToken);
/**
//方式3,自定义方式提供密钥信息, 继承 QCloudCredentialProvider 并 重写 GetQCloudCredentials() 方法
public class MyQCloudCredentialProvider : QCloudCredentialProvider
{
public override QCloudCredentials GetQCloudCredentials()
{
string secretId = "密钥 SecretId";
string secretKey = "密钥 SecretKey";
string keyTime = "密钥 有效期间"; //1546862502;1546863102
return new QCloudCredentials(secretId, secretKey, keyTime);
}
public override void Refresh()
{
//更新 密钥信息
}
}
cosCredentialProvider = new MyQCloudCredentialProvider();
*/
//初始化 CosXmlServer
CosXmlServer cosXml = new CosXmlServer(config, cosCredentialProvider);
用来获取指定账号下所有存储桶列表(Bucket list).
GetServiceResult GetService(GetServiceRequest request);
void GetService(GetServiceRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
GetServiceRequest request = new GetServiceRequest();
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
GetServiceResult result = cosXml.GetService(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
GetServiceRequest request = new GetServiceRequest();
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.GetService(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
GetServiceResult result = cosResult as GetServiceResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 GetServiceResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
listAllMyBuckets | ListAllMyBuckets | 返回指定账号下的存储桶列表的信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
创建一个存储桶 (Put Bucket).
PutBucketResult PutBucket(PutBucketRequest request);
void PutBucket(PutBucketRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
PutBucketRequest request = new PutBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
PutBucketResult result = cosXml.PutBucket(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
PutBucketRequest request = new PutBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.PutBucket(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
PutBucketResult result = cosResult as PutBucketResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 PutBucketResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
删除指定的存储桶 (Delete Bucket).
DeleteBucketResult DeleteBucket(DeleteBucketRequest request);
void DeleteBucket(DeleteBucketRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
DeleteBucketRequest request = new DeleteBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
DeleteBucketResult result = cosXml.DeleteBucket(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
DeleteBucketRequest request = new DeleteBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.DeleteBucket(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
DeleteBucketResult result = cosResult as DeleteBucketResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 DeleteBucketResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
检索指定存储桶是否存在 (Head Bucket).
HeadBucketResult HeadBucket(HeadBucketRequest request);
void HeadBucket(HeadBucketRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
HeadBucketRequest request = new HeadBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
HeadBucketResult result = cosXml.HeadBucket(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
HeadBucketRequest request = new HeadBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.HeadBucket(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
HeadBucketResult result = cosResult as HeadBucketResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 HeadBucketResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
获取指定存储桶中所有的对象 (Object List).
GetBucketResult GetBucket(GetBucketRequest request);
void GetBucket(GetBucketRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
GetBucketRequest request = new GetBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
GetBucketResult result = cosXml.GetBucket(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
GetBucketRequest request = new GetBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.GetBucket(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
GetBucketResult result = cosResult as GetBucketResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 GetBucketResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
listBucket | ListBucket | 返回 Bucket 对象列表信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
获取指定存储桶所在的地域信息 (Get Bucket Location).
GetBucketLocationResult GetBucketLocation(GetBucketLocationRequest request);
void GetBucketLocation(GetBucketLocationRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
GetBucketLocationRequest request = new GetBucketLocationRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
GetBucketLocationResult result = cosXml.GetBucketLocation(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
GetBucketLocationRequest request = new GetBucketLocationRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.GetBucketLocation(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
GetBucketLocationResult result = cosResult as GetBucketLocationResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 GetBucketLocationResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
locationConstraint | LocationConstraint | 返回 Bucket 地域信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
设置指定存储桶访问权限控制列表 (ACL) (Put Bucket ACL).
PutBucketACLResult PutBucketACL(PutBucketACLRequest request);
void PutBucketACL(PutBucketACLRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
PutBucketACLRequest request = new PutBucketACLRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置私有读写权限
request.SetCosACL(CosACL.PRIVATE);
//授予1131975903账号读权限
COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount();
readAccount.AddGrantAccount("1131975903", "1131975903");
request.setXCosGrantRead(readAccount);
//执行请求
PutBucketACLResult result = cosXml.PutBucketACL(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
PutBucketACLRequest request = new PutBucketACLRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置私有读写权限
request.SetCosACL(CosACL.PRIVATE);
//授予1131975903账号读权限
COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount();
readAccount.AddGrantAccount("1131975903", "1131975903");
request.setXCosGrantRead(readAccount);
//执行请求
cosXml.PutBucketACL(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
PutBucketACLResult result = cosResult as PutBucketACLResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
cosAcl | SetCosAcl | 设置存储桶的acl权限 | string |
grandtAccout | SetXCosGrantRead 或 SetXCosGrantWrite 或 SetXCosReadWrite | 授予用户读写权限 | GrantAccount |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 PutBucketACLResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
获取指定存储桶的访问权限控制列表 (ACL) (Get Bucket ACL).
GetBucketACLResult GetBucketACL(GetBucketACLRequest request);
void GetBucketACL(GetBucketACLRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
GetBucketACLRequest request = new GetBucketACLRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
GetBucketACLResult result = cosXml.GetBucketACL(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
GetBucketACLRequest request = new GetBucketACLRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.GetBucketACL(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
GetBucketACLResult result = cosResult as GetBucketACLResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 GetBucketACLResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
accessControlPolicy | AccessControlPolicy | 返回 Bucket 访问权限列表信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
设置指定存储桶的跨域访问配置信息 (CORS) (Put Bucket CORS).
PutBucketCORSResult PutBucketCORS(PutBucketCORSRequest request);
void PutBucketCORS(PutBucketCORSRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
PutBucketCORSRequest request = new PutBucketCORSRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置跨域访问配置CORS
COSXML.Model.Tag.CORSConfiguration.CORSRule corsRule = new COSXML.Model.Tag.CORSConfiguration.CORSRule();
corsRule.id = "corsconfigureId";
corsRule.maxAgeSeconds = 6000;
corsRule.allowedOrigin = "http://cloud.tencent.com";
corsRule.allowedMethods = new List<string>();
corsRule.allowedMethods.Add("PUT");
corsRule.allowedHeaders = new List<string>();
corsRule.allowedHeaders.Add("Host");
corsRule.exposeHeaders = new List<string>();
corsRule.exposeHeaders.Add("x-cos-meta-x1");
request.SetCORSRule(corsRule);
//执行请求
PutBucketCORSResult result = cosXml.PutBucketCORS(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
PutBucketCORSRequest request = new PutBucketCORSRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置跨域访问配置CORS
COSXML.Model.Tag.CORSConfiguration.CORSRule corsRule = new COSXML.Model.Tag.CORSConfiguration.CORSRule();
corsRule.id = "corsconfigureId";
corsRule.maxAgeSeconds = 6000;
corsRule.allowedOrigin = "http://cloud.tencent.com";
corsRule.allowedMethods = new List<string>();
corsRule.allowedMethods.Add("PUT");
corsRule.allowedHeaders = new List<string>();
corsRule.allowedHeaders.Add("Host");
corsRule.exposeHeaders = new List<string>();
corsRule.exposeHeaders.Add("x-cos-meta-x1");
request.SetCORSRule(corsRule);
cosXml.PutBucketCORS(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
PutBucketCORSResult result = cosResult as PutBucketCORSResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
corsRule | SetCORSRule | 设置存储桶的跨域访问配 | CORSConfiguration.CORSRule |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 PutBucketCORSResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
获取指定存储桶的跨域访问配置信息 (CORS) (Get Bucket CORS).
GetBucketCORSResult GetBucketCORS(GetBucketCORSRequest request);
void GetBucketCORS(GetBucketCORSRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
GetBucketCORSRequest request = new GetBucketCORSRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
GetBucketCORSResult result = cosXml.GetBucketCORS(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
GetBucketCORSRequest request = new GetBucketCORSRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.GetBucketCORS(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
GetBucketCORSResult result = cosResult as GetBucketCORSResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 GetBucketCORSResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
corsConfiguration | CORSConfiguration | 返回 Bucket 跨域资源共享配置的信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
删除指定存储桶的跨域访问配置 (CORS) (Delete Bucket CORS).
DeleteBucketCORSResult DeleteBucketCORS(DeleteBucketCORSRequest request);
void DeleteBucketCORS(DeleteBucketCORSRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
DeleteBucketCORSRequest request = new DeleteBucketCORSRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
DeleteBucketCORSResult result = cosXml.DeleteBucketCORS(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
DeleteBucketCORSRequest request = new DeleteBucketCORSRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.DeleteBucketCORS(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
DeleteBucketCORSResult result = cosResult as DeleteBucketCORSResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 DeleteBucketCORSResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
设置指定存储桶的生命周期配置信息 (Lifecycle) (Put Bucket LifeCycle).
PutBucketLifecycleResult PutBucketLifecycle(PutBucketLifecycleRequest request);
void PutBucketLifecycle(PutBucketLifecycleRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
PutBucketLifecycleRequest request = new PutBucketLifecycleRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置 lifecycle
COSXML.Model.Tag.LifecycleConfiguration.Rule rule = new COSXML.Model.Tag.LifecycleConfiguration.Rule();
rule.id = "lfiecycleConfigureId";
rule.status = "Enabled"; //Enabled,Disabled
rule.filter = new COSXML.Model.Tag.LifecycleConfiguration.Filter();
rule.filter.prefix = "2/";
//指定分片过期删除操作
rule.abortIncompleteMultiUpload = new COSXML.Model.Tag.LifecycleConfiguration.AbortIncompleteMultiUpload();
rule.abortIncompleteMultiUpload.daysAfterInitiation = 2;
request.SetRule(rule);
//执行请求
PutBucketLifecycleResult result = cosXml.PutBucketLifecycle(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
PutBucketLifecycleRequest request = new PutBucketLifecycleRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置 lifecycle
COSXML.Model.Tag.LifecycleConfiguration.Rule rule = new COSXML.Model.Tag.LifecycleConfiguration.Rule();
rule.id = "lfiecycleConfigureId";
rule.status = "Enabled"; //Enabled,Disabled
rule.filter = new COSXML.Model.Tag.LifecycleConfiguration.Filter();
rule.filter.prefix = "2/";
rule.abortIncompleteMultiUpload = new COSXML.Model.Tag.LifecycleConfiguration.AbortIncompleteMultiUpload();
rule.abortIncompleteMultiUpload.daysAfterInitiation = 2;
request.SetRule(rule);
//执行请求
cosXml.PutBucketLifecycle(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
PutBucketLifecycleResult result = cosResult as PutBucketLifecycleResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
rule | SetRule | 设置存储桶的生命周期配置 | LifecycleConfiguration.Rule |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 PutBucketLifecycleResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
获取指定存储桶的的生命周期 (Lifecycle) (Get Bucket Lifecycle).
GetBucketLifecycleResult GetBucketLifecycle(GetBucketLifecycleRequest request);
void GetBucketLifecycle(GetBucketLifecycleRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
GetBucketLifecycleRequest request = new GetBucketLifecycleRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
GetBucketLifecycleResult result = cosXml.GetBucketLifecycle(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
GetBucketLifecycleRequest request = new GetBucketLifecycleRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.GetBucketLifecycle(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
GetBucketLifecycleResult result = cosResult as GetBucketLifecycleResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 GetBucketLifecycleResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
lifecycleConfiguration | LifecycleConfiguration | 返回 Bucket 的生命周期配置信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
删除指定存储桶的的生命周期 (Lifecycle) (Delete Bucket Lifecycle).
DeleteBucketLifecycleResult DeleteBucketLifecycle(DeleteBucketLifecycleRequest request);
void DeleteBucketLifecycle(DeleteBucketLifecycleRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
DeleteBucketLifecycleRequest request = new DeleteBucketLifecycleRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
DeleteBucketLifecycleResult result = cosXml.DeleteBucketLifecycle(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
DeleteBucketLifecycleRequest request = new DeleteBucketLifecycleRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.DeleteBucketLifecycle(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
DeleteBucketLifecycleResult result = cosResult as DeleteBucketLifecycleResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 DeleteBucketLifecycleResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
查询指定存储桶中正在进行的分片上传 (List Multipart Uploads).
ListMultiUploadsResult ListMultiUploads(ListMultiUploadsRequest request);
void ListMultiUploads(ListMultiUploadsRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //格式:bucketname-appid
ListMultiUploadsRequest request = new ListMultiUploadsRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
ListMultiUploadsResult result = cosXml.ListMultiUploads(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //格式:bucketname-appid
ListMultiUploadsRequest request = new ListMultiUploadsRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.ListMultiUploads(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
ListMultiUploadsResult result = cosResult as ListMultiUploadsResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 ListMultiUploadsResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
listMultipartUploads | ListMultipartUploads | 返回 Bucket 中所有正在进行分块上传的信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
上传对象到指定的存储桶中 (Put Object).
PutObjectResult PutObject(PutObjectRequest request);
void PutObject(PutObjectRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string srcPath = @"F:\test.txt";//本地文件路径
PutObjectRequest request = new PutObjectRequest(bucket, key, srcPath);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置进度回调
request.SetCosProgressCallback(delegate(long completed, long total)
{
Console.WriteLine(String.Format("progress = {1:##.##}%", completed * 100.0 / total));
});
//执行请求
PutObjectResult result = cosXml.PutObject(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string srcPath = @"F:\test.txt"; //本地文件路径
PutObjectRequest request = new PutObjectRequest(bucket, key, srcPath);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置进度回调
request.SetCosProgressCallback(delegate(long completed, long total)
{
Console.WriteLine(String.Format("progress = {1:##.##}%", completed * 100.0 / total));
});
//执行请求
cosXml.PutObject(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
PutObjectResult result = cosResult as PutObjectResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
srcPath | 构造方法 | 用于上传到 cos 的本地文件的绝对路径 | string |
data | 构造方法 | 用于上传到 cos 的 byte 数组 | byte[] |
progressCallback | SetCosProgressCallback | 设置上传进度回调 | Callback.OnProgressCallback |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 PutObjectResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
eTag | string | 返回 对象 的 eTag |
操作失败会抛出 CosClientException 或 CosServerException 异常
分片上传对象可包括的操作:
分片上传对象: 初始化分片上传, 上传分片块, 完成所有分片块上传.
分片续传:查询已上传的分片块, 上传分片块,完成所有分片块上传.
删除已上传分片块.
初始化分片上传,获取对应的uploadId. (Initiate Multipart Upload).
InitMultipartUploadResult InitMultipartUpload(InitMultipartUploadRequest request);
void InitMultipartUpload(InitMultipartUploadRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
InitMultipartUploadRequest request = new InitMultipartUploadRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
InitMultipartUploadResult result = cosXml.InitMultipartUpload(request);
//请求成功
string uploadId = result.initMultipartUpload.uploadId; //用于后续分片上传的 uploadId
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
InitMultipartUploadRequest request = new InitMultipartUploadRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.InitMultipartUpload(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
InitMultipartUploadResult result = cosResult as InitMultipartUploadResult;
string uploadId = result.initMultipartUpload.uploadId; //用于后续分片上传的 uploadId
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 InitMultipartUploadResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
initMultipartUpload | InitiateMultipartUpload | 返回 对象 初始化分片上传的 uploadId |
操作失败会抛出 CosClientException 或 CosServerException 异常
查询指定uploadId 已上传的分片块 (List Parts).
ListPartsResult ListParts(ListPartsRequest request);
void ListParts(ListPartsRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string uploadId ="xxxxxxxx"; //初始化分片上传返回的uploadId
ListPartsRequest request = new ListPartsRequest(bucket, key, uploadId);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
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.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string uploadId ="xxxxxxxx"; //初始化分片上传返回的uploadId
ListPartsRequest request = new ListPartsRequest(bucket, key, uploadId);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.ListParts(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
ListPartsResult result = cosResult as ListPartsResult;
//列举已上传的分片块
List<COSXML.Model.Tag.ListParts.Part> alreadyUploadParts = result.listParts.parts;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
uploadId | 构造方法 或 SetUploadId | 标识指定分片上传的 uploadId | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 ListPartsResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
listParts | ListParts | 返回指定 uploadId 分块上传中的已上传的块信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
上传分片块 (Upload Part).
UploadPartResult UploadPart(UploadPartRequest request);
void UploadPart(UploadPartRequest, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string uploadId ="xxxxxxxx"; //初始化分片上传返回的uploadId
int partNumber = 1; //分片块编号,必须从1开始递增
string srcPath = @"F:\test.txt"; //本地文件路径
UploadPartRequest request = new UploadPartRequest(bucket, key, partNumber, uploadId, srcPath);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置进度回调
request.SetCosProgressCallback(delegate(long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
});
//执行请求
UploadPartResult result = cosXml.UploadPart(request);
//请求成功
//获取返回分片块的eTag,用于后续CompleteMultiUploads
string eTag = result.eTag;
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string uploadId ="xxxxxxxx"; //初始化分片上传返回的uploadId
int partNumber = 1; //分片块编号,必须从1开始递增
string srcPath = @"F:\test.txt"; //本地文件路径
UploadPartRequest request = new UploadPartRequest(bucket, key, partNumber, uploadId, srcPath);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置进度回调
request.SetCosProgressCallback(delegate(long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
});
//执行请求
cosXml.UploadPart(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
UploadPartResult result = cosResult as UploadPartResult;
//获取返回分片块的eTag,用于后续CompleteMultiUploads
string eTag = result.eTag;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
uploadId | 构造方法 或 SetUploadId | 标识指定分片上传的 uploadId | string |
partNumber | 构造方法 或 SetPartNumber | 标识指定分片的编号,必须 >= 1 | int |
srcPath | 构造方法 | 用于上传到 cos 的本地文件的绝对路径 | string |
data | 构造方法 | 用于上传到 cos 的 byte 数组 | byte[] |
progressCallback | SetCosProgressCallback | 设置上传进度回调 | Callback.OnProgressCallback |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 UploadPartResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
eTag | string | 返回 对象的分片块的 eTag |
操作失败会抛出 CosClientException 或 CosServerException 异常
实现完成整个分块上传 (Complete Multipart Upload).
CompleteMultipartUploadResult CompleteMultiUpload(CompleteMultipartUploadRequest request);
void CompleteMultiUpload(CompleteMultipartUploadRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string uploadId ="xxxxxxxx"; //初始化分片上传返回的uploadId
CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucket, key, uploadId);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置已上传的parts,必须有序,按照partNumber递增
request.SetPartNumberAndETag(1, "partNumber1 eTag");
//执行请求
CompleteMultipartUploadResult result = cosXml.CompleteMultiUpload(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string uploadId ="xxxxxxxx"; //初始化分片上传返回的uploadId
CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucket, key, uploadId);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置已上传的parts,必须有序,按照partNumber递增
request.SetPartNumberAndETag(1, "partNumber1 eTag");
//执行请求
cosXml.CompleteMultiUpload(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
CompleteMultipartUploadResult result = result as CompleteMultipartUploadResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
uploadId | 构造方法 或 SetUploadId | 标识指定分片上传的 uploadId | string |
partNumber | SetPartNumberAndETag | 标识指定分片块的编号,必须 >= 1 | int |
eTag | SetPartNumberAndETag | 标识指定分片块的上传返回的 eTag | string |
partNumberAndETags | SetPartNumberAndETag | 标识分片块的编号和上传返回的 eTag | Dictionary<int, string> |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 CompleteMultipartUploadResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
CompleteResult | CompleteMultipartUploadResult | 返回所有分片块上传成功信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
舍弃一个分块上传并删除已上传的块(Abort Multipart Upload).
AbortMultipartUploadResult AbortMultiUpload(AbortMultipartUploadRequest request);
void AbortMultiUpload(AbortMultipartUploadRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string uploadId ="xxxxxxxx"; //初始化分片上传返回的uploadId
AbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucket, key, uploadId);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
AbortMultipartUploadResult result = cosXml.AbortMultiUpload(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string uploadId ="xxxxxxxx"; //初始化分片上传返回的uploadId
AbortMultipartUploadRequest request = new AbortMultipartUploadRequest(bucket, key, uploadId);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.AbortMultiUpload(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
AbortMultipartUploadResult result = result as AbortMultipartUploadResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
uploadId | 构造方法 或 SetUploadId | 标识指定分片上传的 uploadId | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 AbortMultipartUploadResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
使用表单形式上传对象 (Post Object).
PostObjectResult PostObject(PostObjectRequest request);
void PostObject(PostObjectRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string srcPath = @"F:\test.txt";//本地文件路径
PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置进度回调
request.SetCosProgressCallback(delegate(long completed, long total)
{
Console.WriteLine(String.Format("progress = {1:##.##}%", completed * 100.0 / total));
});
//执行请求
PostObjectResult result = cosXml.PostObject(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string srcPath = @"F:\test.txt"; //本地文件路径
PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置进度回调
request.SetCosProgressCallback(delegate(long completed, long total)
{
Console.WriteLine(String.Format("progress = {1:##.##}%", completed * 100.0 / total));
});
//执行请求
cosXml.PostObject(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
PostObjectResult result = cosResult as PostObjectResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
srcPath | 构造方法 | 用于上传到 cos 的本地文件的绝对路径 | string |
data | 构造方法 | 用于上传到 cos 的 byte 数组 | byte[] |
progressCallback | SetCosProgressCallback | 设置上传进度回调 | Callback.OnProgressCallback |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 PostObjectResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
eTag | string | 返回 对象 的 eTag |
操作失败会抛出 CosClientException 或 CosServerException 异常
查询存储桶中是否存在指定的对象 (Head Object).
HeadObjectResult HeadObject(HeadObjectRequest request);
void HeadObject(HeadObjectRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
HeadObjectRequest request = new HeadObjectRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
HeadObjectResult result = cosXml.HeadObject(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
HeadObjectRequest request = new HeadObjectRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.HeadObject(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
HeadObjectResult result = cosResult as HeadObjectResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 HeadObjectResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
eTag | string | 返回 对象 的 eTag |
操作失败会抛出 CosClientException 或 CosServerException 异常
下载对象到本地 (Get Object).
GetObjectResult GetObject(GetObjectRequest request);
void GetObject(GetObjectRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string localDir = @"F:\";//下载到本地指定文件夹
string localFileName = "test.txt"; //指定本地保存的文件名
GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置进度回调
request.SetCosProgressCallback(delegate(long completed, long total)
{
Console.WriteLine(String.Format("progress = {1:##.##}%", completed * 100.0 / total));
});
//执行请求
GetObjectResult result = cosXml.GetObject(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string localDir = @"F:\";//下载到本地指定文件夹
string localFileName = "test.txt"; //指定本地保存的文件名
GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置进度回调
request.SetCosProgressCallback(delegate(long completed, long total)
{
Console.WriteLine(String.Format("progress = {1:##.##}%", completed * 100.0 / total));
});
//执行请求
cosXml.GetObject(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
GetObjectResult result = cosResult as GetObjectResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
localDir | 构造方法 | 下载对象到本地保存的绝对文件夹路径 | string |
localFileName | 构造方法 | 下载对象到本地保存的文件名 | string |
progressCallback | SetCosProgressCallback | 设置下载进度回调 | Callback.OnProgressCallback |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 GetObjectResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
eTag | string | 返回 对象 的 eTag |
操作失败会抛出 CosClientException 或 CosServerException 异常
复制对象操作包括:简单复制(适用于 1M 到 5G);分片复制(适用于大文件拷贝,如 5G)
将一个对象复制到另一个对象 (Put Object Copy).
CopyObjectResult CopyObject(CopyObjectRequest request);
void CopyObject(CopyObjectRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string sourceAppid = "1253960454"; //账号 appid
string sourceBucket = "source-1253960454"; //"源对象所在的存储桶
string sourceRegion = "ap-beijing"; //源对象的存储桶所在的地域
string sourceKey = "test.txt"; //源对象键
//构造源对象属性
COSXML.Model.Tag.CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket, sourceRegion, sourceKey);
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "copy_test.txt"; //对象在存储桶中的位置,即称对象键.
CopyObjectRequest request = new CopyObjectRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置拷贝源
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.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string sourceAppid = "1253960454"; //账号 appid
string sourceBucket = "source-1253960454"; //"源对象所在的存储桶
string sourceRegion = "ap-beijing"; //源对象的存储桶所在的地域
string sourceKey = "test.txt"; //源对象键
//构造源对象属性
COSXML.Model.Tag.CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket, sourceRegion, sourceKey);
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "copy_test.txt"; //对象在存储桶中的位置,即称对象键.
CopyObjectRequest request = new CopyObjectRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置拷贝源
request.SetCopySource(copySource);
//设置是否拷贝还是更新,此处是拷贝
request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.COPY);
//执行请求
cosXml.CopyObject(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
CopyObjectResult result = cosResult as CopyObjectResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
copySource | SetCopySource | 复制的数据源路径描述 | CopySourceStruct |
metaDataDirective | SetCopyMetaDataDirective | 是否拷贝源文件的元数据或者更新源文件的元数据 | CosMetaDataDirective |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 CopyObjectResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
copyObject | CopyObject | 返回成功复制的对象信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
分片复制对象操作包含: 1、初始化分片上传,得到uploadId,详细请查看 InitMultipartUpload ; 2、根据uploadId,进行分片复制; 3、完成所有分片复制,详细请查看 CompleteMultiUpload .
UploadPartCopyResult PartCopy(UploadPartCopyRequest request);
void PartCopy(UploadPartCopyRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string sourceAppid = "1253960454"; //账号 appid
string sourceBucket = "source-1253960454"; //"源对象所在的存储桶
string sourceRegion = "ap-beijing"; //源对象的存储桶所在的地域
string sourceKey = "test.txt"; //源对象键
//构造源对象属性
COSXML.Model.Tag.CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket, sourceRegion, sourceKey);
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "copy_test.txt"; //对象在存储桶中的位置,即称对象键.
string uploadId = "1505706248ca8373f8a5cd52cb129f4bcf85e11dc8833df34f4f5bcc456c99c42cd1ffa2f9 "; //初始化分片上传的 uploadId
int partNumber = 1; // partNumber >= 1
UploadPartCopyRequest request = new UploadPartCopyRequest(bucket, key, partNumber, uploadId);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置拷贝源
request.SetCopySource(copySource);
//设置复制分片块
request.SetCopyRange(0, 1024 * 1024);
//执行请求
UploadPartCopyResult result = cosXml.PartCopy(request);
//请求成功
//获取该分片块返回的eTag,用于CompleteMultiUpload
string eTag = result.copyObject.eTag;
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string sourceAppid = "1253960454"; //账号 appid
string sourceBucket = "source-1253960454"; //"源对象所在的存储桶
string sourceRegion = "ap-beijing"; //源对象的存储桶所在的地域
string sourceKey = "test.txt"; //源对象键
//构造源对象属性
COSXML.Model.Tag.CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket, sourceRegion, sourceKey);
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "copy_test.txt"; //对象在存储桶中的位置,即称对象键.
string uploadId = "1505706248ca8373f8a5cd52cb129f4bcf85e11dc8833df34f4f5bcc456c99c42cd1ffa2f9 "; //初始化分片上传的 uploadId
int partNumber = 1; // partNumber >= 1
UploadPartCopyRequest request = new UploadPartCopyRequest(bucket, key, partNumber, uploadId);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置拷贝源
request.SetCopySource(copySource);
//设置是否拷贝还是更新,此处是拷贝
request.SetCopyMetaDataDirective(COSXML.Common.CosMetaDataDirective.COPY);
//执行请求
cosXml.PartCopy(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
UploadPartCopyResult getObjectResult = result as UploadPartCopyResult;
//获取该分片块返回的eTag,用于CompleteMultiUpload
string eTag = result.copyObject.eTag;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
partNumber | 构造方法 | 分片块的编号, partNumber >= 1 | int |
uploadId | 构造方法 | 标识分片复制的 uploadId | string |
copySource | SetCopySource | 复制的数据源路径描述 | CopySourceStruct |
start | SetCopyRange | 复制的内容的范围的起始位置 | long |
end | SetCopyRange | 复制的内容范围的结束位置 | long |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 UploadPartCopyResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
copyObject | CopyObject | 返回 成功复制的 对象分片块信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
删除指定的对象 (Delete Object).
DeleteObjectResult DeleteObject(DeleteObjectRequest request);
void DeleteObject(DeleteObjectRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
DeleteObjectResult result = cosXml.DeleteObject(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.DeleteObject(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
DeleteObjectResult getObjectResult = result as DeleteObjectResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 DeleteObjectResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
删除指定存储桶中多个对象 (Delete Multi Objects).
DeleteMultiObjectResult DeleteMultiObjects(DeleteMultiObjectRequest request);
void DeleteMultiObjects(DeleteMultiObjectRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置返回结果形式
request.SetDeleteQuiet(false);
//删除多个对象
List<string> keys = new List<string>();
keys.Add("test1.txt");
keys.Add("test2.txt");
request.SetObjectKeys(keys);
//执行请求
DeleteMultiObjectResult result = cosXml.DeleteMultiObjects(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置返回结果形式
request.SetDeleteQuiet(false);
//删除多个对象
List<string> keys = new List<string>();
keys.Add("test1.txt");
keys.Add("test2.txt");
request.SetObjectKeys(keys);
//执行请求
cosXml.DeleteMultiObjects(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
DeleteMultiObjectResult result = cosResult as DeleteMultiObjectResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
quiet | SetDeleteQuiet | 设定批量上传返回结果模式, true: 返回每个 key 的删除情况,false:只返回删除失败的 key 的情况 | bool |
key | SetDeleteKey | 需要删除 cos 上 Object 的对象键 | string |
versionId | SetDeleteKey | 需要删除 cos 上 Object 的指定版本 | string |
keys | SetObjectKeys | 需要删除 cos 上 Object 的对象键集合 | List<string> |
versionIdAndKey | SetObjectKeys | 需要删除 cos 上 Object 的对象键和指定版本的集合 | Dictionary<string, string> |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 DeleteMultiObjectResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
deleteResult | DeleteResult | 返回批量删除 对象 的信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
设置对象访问权限控制列表 (ACL) (Put Object ACL).
PutObjectACLResult PutObjectACL(PutObjectACLRequest request);
void PutObjectACL(PutObjectACLRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
PutObjectACLRequest request = new PutObjectACLRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置私有读写权限
request.SetCosACL(CosACL.PRIVATE);
//授予1131975903账号读权限
COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount();
readAccount.AddGrantAccount("1131975903", "1131975903");
request.setXCosGrantRead(readAccount);
//执行请求
PutObjectACLResult result = cosXml.PutObjectACL(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
PutObjectACLRequest request = new PutObjectACLRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置私有读写权限
request.SetCosACL(CosACL.PRIVATE);
//授予1131975903账号读权限
COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount();
readAccount.AddGrantAccount("1131975903", "1131975903");
request.setXCosGrantRead(readAccount);
//执行请求
cosXml.PutObjectACL(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
PutObjectACLResult result = cosResult as PutObjectACLResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
cosAcl | SetCosAcl | 设置存储桶的acl权限 | string |
grandtAccout | SetXCosGrantRead 或 SetXCosGrantWrite 或 SetXCosReadWrite | 授予用户读写权限 | GrantAccount |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 PutObjectACLResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
操作失败会抛出 CosClientException 或 CosServerException 异常
获取对象访问权限控制列表 (ACL) (Get Object ACL).
GetObjectACLResult GetObjectACL(GetObjectACLRequest request);
void GetObjectACL(GetObjectACLRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);
try
{
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
GetObjectACLRequest request = new GetObjectACLRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
GetObjectACLResult result = cosXml.GetObjectACL(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
/**
//异步方法
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
GetObjectACLRequest request = new GetObjectACLRequest(bucket, key);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//执行请求
cosXml.GetObjectACL(request,
delegate(COSXML.Model.CosResult cosResult)
{
//请求成功
GetObjectACLResult result = cosResult as GetObjectACLResult;
Console.WriteLine(result.GetResultInfo());
},
delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
{
//请求失败
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
});
*/
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucket | 构造方法 | 存储桶名称,格式:bucketname-appid | string |
key | 构造方法 或 SetCosPath | 存储于 cos 上 Object 的对象键 | string |
signStartTimeSecond | SetSign | 签名有效期起始时间 | long |
durationSecond | SetSign | 签名有效期时长 | long |
headerKeys | SetSign | 签名是否校验header | List<string> |
queryParameterKeys | SetSign | 签名是否校验请求url中查询参数 | List<string> |
通过 GetObjectACLResult 返回请求结果.
成员变量 | 类型 | 描述 |
---|---|---|
httpCode | int | HTTP Code, [200, 300)之间表示操作成功,否则表示操作失败 |
accessControlPolicy | AccessControlPolicy | 返回 对象 的访问权限列表信息 |
操作失败会抛出 CosClientException 或 CosServerException 异常
TransferManager、COSXMLUploadTask 封装了简单上传、分片上传接口的异步请求
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string srcPath = @"F:\test.txt"; //文件的本地位置
COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, null, key)
{
//进度回调
successCallback = delegate (CosResult cosResult)
{
Console.WriteLine(String.Format("progress ={0:##.##}%",completed * 100.0 / total));
},
//成功回调
successCallback = delegate (CosResult cosResult)
{
COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = cosResult as COSXML.Transfer.COSXMLUploadTask.UploadTaskResult;
Console.WriteLine(result.GetResultInfo());
},
//失败回调
failCallback = delegate (CosClientException clientEx, CosServerException serverEx)
{
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
}
};
//设置上传源
long offset = 0; //从源文件0位置开始
long sendContentLength = -1; //上传整个文件, (或者上传部分内容, sendContentLength > -1)
uploadTask.SetSrcPath(srcPath, offset, sendContentLength);
//执行请求
transferManager.Upload(uploadTask);
TransferManager、COSXMLDownloadTask 封装了下载接口的异步请求
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "test.txt"; //对象在存储桶中的位置,即称对象键.
string localDir = @"F:\"; //下载文件到本地保存的文件夹路径
string localFileNmae = "test.txt"; //下载文件到本地的文件名
COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, null, key, localDir, localFileName)
{
//进度回调
successCallback = delegate (CosResult cosResult)
{
Console.WriteLine(String.Format("progress ={0:##.##}%",completed * 100.0 / total));
},
//成功回调
successCallback = delegate (CosResult cosResult)
{
COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = cosResult as COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult;
Console.WriteLine(result.GetResultInfo());
},
//失败回调
failCallback = delegate (CosClientException clientEx, CosServerException serverEx)
{
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
}
};
//执行请求
transferManager.Download(downloadTask);
TransferManager、COSXMLCopyTask 封装了简单复制、分片复制接口的异步请求
string sourceAppid = "1253960454"; //账号 appid
string sourceBucket = "source-1253960454"; //"源对象所在的存储桶
string sourceRegion = "ap-beijing"; //源对象的存储桶所在的地域
string sourceKey = "test.txt"; //源对象键
//构造源对象属性
COSXML.Model.Tag.CopySourceStruct copySource = new CopySourceStruct(sourceAppid, sourceBucket, sourceRegion, sourceKey);
string bucket = "test-1253960454"; //存储桶,格式:bucketname-appid
string key = "copy_test.txt"; //对象在存储桶中的位置,即称对象键.
COSXMLCopyTask copyTask = new COSXMLCopyTask(bucket, null, key, copySource)
{
//成功回调
successCallback = delegate (CosResult cosResult)
{
COSXML.Transfer.COSXMLCopyTask.CopyTaskResult result = cosResult as COSXML.Transfer.COSXMLCopyTask.CopyTaskResult;
Console.WriteLine(result.GetResultInfo());
},
//失败回调
failCallback = delegate (CosClientException clientEx, CosServerException serverEx)
{
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx.Message);
}
else if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
}
};
//执行请求
transferManager.Copy(copyTask);
SDK 中,若是调用接口操作 cos 对象失败,会抛出 CosXmlClientException 异常 或者 CosXmlServiceException 异常
客户端异常,用于指因为客户端原因导致无法和服务端完成正常的交互而导致的失败, 如客户端无法连接到服务端,无法解析服务端返回的数据, 读取本地文件发生 IO 异常等。 CosClientException 集成自 System.ApplicationException, 使用方法同 System.ApplicationException, 同时添加一个额外的成员 errorCode,如下:
成员 | 描述 | 类型 |
---|---|---|
errorCode | 客户端错误码,如 10000 表示参数检验失败 | int |
CosServerException 服务异常, 用于指交互正常完成,但是操作失败的场景。例如客户端访问一个不存在 Bucket, 删除一个不存在的文件,没有权限进行某个操作, 服务端故障异常等。 CosServerException 包含了服务端返回的状态码, requestid, 出错明细等。捕获异常后, 建议对整个异常进行打印, 异常包含了必须的排查因素。以下是异常成员变量的描述:
成员 | 描述 | 类型 |
---|---|---|
requestId | 请求 ID,用于表示一个请求,对于排查问题十分重要. | string |
statusCode | response 的 status 状态码,4xx 是指请求因客户端而失败,5xx 是服务端异常导致的失败。 请参照 COS错误信息。 | string |
errorCode | 请求失败时 body 返回的 Error Code 请参照 COS错误信息. | string |
errorMessage | 请求失败时 body 返回的 Error Message 请参照 COS错误信息. | string |