Skip to content

bradyxiao/QCloudCSharpSDK

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

  1. 工程中包含两个项目:COSXML, Demo
  2. COSXML是COS 服务类库,提供COS 服务各种API
  3. Demo是COSXML示例
  4. COSXML依赖Newtonsoft.Json(12.0.1)

快速入门

初始化

在执行任何和 COS 服务相关请求之前,都需要先实例化CosXmlConfig , QCloudCredentialProvider , CosXmlServer3个对象. 其中

  • 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);

创建存储桶

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());
}

查询存储桶列表

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());
}

上传对象

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());
}

查询对象列表

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());
}

下载对象

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());
}

删除对象

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());
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages