internal static async Task GetObject(CosXmlServer cosXml, string cosKey) { TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); String cosPath = cosKey; //对象在存储桶中的位置标识符,即称对象键 string localDir = System.IO.Path.GetTempPath(); //本地文件夹 string localFileName = "my-local-temp-file"; //指定本地保存的文件名 // 下载对象 COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath, localDir, localFileName); downloadTask.progressCallback = delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }; try { COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = await transferManager.DownloadAsync(downloadTask); Console.WriteLine(result.GetResultInfo()); string eTag = result.eTag; } catch (Exception e) { Console.WriteLine("CosException: " + e); } }
internal static async Task <String> PutObject(CosXmlServer cosXml) { string cosKey = "cosKey"; //.cssg-snippet-body-start:[transfer-upload-file] // 初始化 TransferConfig TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); String cosPath = cosKey; //对象在存储桶中的位置标识符,即称对象键 String srcPath = @"本地绝对路径"; //本地文件绝对路径 // 上传对象 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)); }; try { COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = await transferManager.UploadAsync(uploadTask); Console.WriteLine(result.GetResultInfo()); string eTag = result.eTag; } catch (Exception e) { Console.WriteLine("CosException: " + e); } return(cosKey); }
/// <summary> /// 初始化腾讯云配置 /// </summary> private void CreateCosXML() { CoreEngine.Current.Logger.Info($"主程序引擎:加载COS配置文件 CosXmlConfig"); //初始化 CosXmlConfig string appid = GlobalConfig.AppID; //设置腾讯云账户的账户标识 APPID string region = GlobalConfig.Region; //设置一个默认的存储桶地域 CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(GlobalConfig.ConnectionTimeoutMs) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(GlobalConfig.ReadWriteTimeoutMs) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid(appid) //设置腾讯云账户的账户标识 APPID .SetRegion(region) //设置一个默认的存储桶地域 .SetDebugLog(true) //显示日志 .Build(); //创建 CosXmlConfig 对象 //初始化 QCloudCredentialProvider,COS SDK 中提供了3种方式:永久密钥、临时密钥、自定义 QCloudCredentialProvider cosCredentialProvider = null; //方式1, 永久密钥 string secretId = new SecretEtt().SecretId; //"云 API 密钥 SecretId"; string secretKey = new SecretEtt().SecretKey; //"云 API 密钥 SecretKey"; long durationSecond = 600; //每次请求签名有效时长,单位为秒 cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CoreEngine.Current.Logger.Info($"主程序引擎:初始化COS"); //初始化 CosXmlServer CosXml = new CosXmlServer(config, cosCredentialProvider); }
public WeatherForecastController(ILogger <WeatherForecastController> logger, CosXmlServer cosXmlServer, IOptions <CosXmlOptions> cosXmlOptions) { _cosXmlServer = cosXmlServer; _cosXmlOptions = cosXmlOptions.Value; _logger = logger; }
public void testCustomHost() { string customHost = "www.my.custom.host.com"; // test host CosXmlConfig configWithHost = new CosXmlConfig.Builder() .setHost(customHost) .Build(); string bucket = "bucket-125000"; CosXmlServer cosXml = new CosXmlServer(configWithHost, null); GetBucketRequest bucketRequest = new GetBucketRequest(bucket); try { cosXml.GetBucket(bucketRequest); } catch (Exception e) { // ignore } Assert.AreEqual(customHost, bucketRequest.GetHost()); GetObjectRequest request = new GetObjectRequest(bucket, "aKey", null, null); try { cosXml.GetObject(request); } catch (Exception e) { // ignore } Assert.AreEqual(customHost, request.GetHost()); GetServiceRequest serviceRequest = new GetServiceRequest(); Assert.AreEqual("service.cos.myqcloud.com", serviceRequest.GetHost()); }
public QCloudCOS(string appid, string region, string secretId, string secretKey, string bucket) { _bucket = bucket; //初始化 CosXmlConfig //string appid = "1258572948";//设置腾讯云账户的账户标识 APPID //string region = "ap-chengdu"; //设置一个默认的存储桶地域 // string secretId = "AKIDKi1vlbNKDvSqOtPONBsPkfdOyNdhGWxS"; //"云 API 密钥 SecretId"; // string secretKey = "Vq14wcIe1wt3HNK1ksCMj4LVBlEcgls3"; //"云 API 密钥 SecretKey"; config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(5000) //设置连接超时时间,单位 毫秒 ,默认 45000ms .SetReadWriteTimeoutMs(5000) //设置读写超时时间,单位 毫秒 ,默认 45000ms .IsHttps(true) //设置默认 https 请求 .SetAppid(appid) //设置腾讯云账户的账户标识 APPID .SetRegion(region) //设置一个默认的存储桶地域 .SetDebugLog(true) //显示日志 .Build(); //创建 CosXmlConfig 对象 //方式1, 永久密钥 long durationSecond = 600; //secretKey 有效时长,单位为 秒 cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); //初始化 CosXmlServer cosXml = new CosXmlServer(config, cosCredentialProvider); }
internal static void DeleteBucket(CosXmlServer cosXml) { DeleteBucketRequest request = new DeleteBucketRequest(bucket); DeleteBucketResult result = cosXml.DeleteBucket(request); Console.WriteLine(result.GetResultInfo()); }
internal static void DeleteObject(CosXmlServer cosXml, string cosKey) { DeleteObjectRequest request = new DeleteObjectRequest(bucket, cosKey); DeleteObjectResult result = cosXml.DeleteObject(request); Console.WriteLine(result.GetResultInfo()); }
/// <summary> /// 上传指定文件到对象存储的CoursesExport目录中 /// </summary> /// <param name="fileName">文件名</param> /// <param name="path">本地文件的绝对路径</param> /// <returns>下载地址</returns> public static String UploadFile(string fileName, string path) { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid("***REMOVED***") //设置腾讯云账户的账户标识 APPID .SetRegion("***REMOVED***") //设置一个默认的存储桶地域 .Build(); string AppDirectory = CQ.Api.AppDirectory; string secretId = ini.Read(AppDirectory + @"\CosSecret.ini", "CosSecret", "SecretId", ""); //云 API 密钥 SecretId string secretKey = ini.Read(AppDirectory + @"\CosSecret.ini", "CosSecret", "SecretKey", ""); //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "chajian-***REMOVED***"; //存储桶,格式:BucketName-APPID string key = "***REMOVED***" + fileName; //对象在存储桶中的位置,即称对象键 string srcPath = path; //本地文件绝对路径 if (!File.Exists(srcPath)) { // 如果不存在目标文件,创建一个临时的测试文件 File.WriteAllBytes(srcPath, new byte[1024]); } 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 = {0:##.##}%", completed * 100.0 / total)); }); //执行请求 PutObjectResult result = cosXml.PutObject(request); //对象的 eTag string eTag = result.eTag; return("***REMOVED***CoursesExport/" + fileName); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 CQ.Log.Warning("CosClientException: ", clientEx); return(""); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 CQ.Log.Warning("CosServerException: ", serverEx.GetInfo()); return(""); } }
public void putBucketLifecycle() { //.cssg-snippet-body-start:[put-bucket-lifecycle] CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid("1253653367") //设置腾讯云账户的账户标识 APPID .SetRegion("ap-guangzhou") //设置一个默认的存储桶地域 .Build(); string secretId = Environment.GetEnvironmentVariable("COS_KEY"); //云 API 密钥 SecretId string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "bucket-cssg-test-dotnet-1253653367"; //格式:BucketName-APPID PutBucketLifecycleRequest request = new PutBucketLifecycleRequest(bucket); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置 lifecycle LifecycleConfiguration.Rule rule = new 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 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); Assert.Null(clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.Null(serverEx); } //.cssg-snippet-body-end }
public CosServerWrapObject(IOptions <AbpTencentCloudCOSOptions> abpTencentCloudCOSOptions, IOptions <AbpTencentCloudCommonOptions> abpTencentCloudCommonOptions) { _abpTencentCloudCommonOptions = abpTencentCloudCommonOptions.Value; _abpTencentCloudCOSOptions = abpTencentCloudCOSOptions.Value; CosXmlServer = new CosXmlServer(BuildConfig(), GetCredentialProvider()); }
/// <summary> /// 获取bukect中的所有文件 /// </summary> /// <param name="bucketName"></param> /// <param name="config"></param> /// <param name="cosCredentialProvider"></param> /// <returns></returns> public static List <CloudFile> ListObjects() { if (BosConfig == null) { return(null); } //初始化 CosXmlServer CosXmlServer cosXml = getCosXmlServer(); GetBucketRequest request = new GetBucketRequest(BosConfig.BucketName); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); request.SetMaxKeys(1 + ""); //执行请求 GetBucketResult result = cosXml.GetBucket(request); ListBucket listBucket = result.listBucket; List <ListBucket.Contents> contents = listBucket.contentsList; List <CloudFile> cloudFiles = new List <CloudFile>(); foreach (ListBucket.Contents content in contents) { CloudFile cloudFile = new CloudFile { Key = content.key, Size = content.size, LastModified = content.lastModified }; cloudFiles.Add(cloudFile); } while (result.listBucket.nextMarker != null) { request.SetMarker(result.listBucket.nextMarker); //执行请求 result = cosXml.GetBucket(request); foreach (ListBucket.Contents content in result.listBucket.contentsList) { CloudFile cloudFile = new CloudFile { Key = content.key, Size = content.size, LastModified = content.lastModified }; cloudFiles.Add(cloudFile); } } Console.WriteLine(cloudFiles); return(cloudFiles); }
public void putBucketReplication() { //.cssg-snippet-body-start:[put-bucket-replication] CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid("1253653367") //设置腾讯云账户的账户标识 APPID .SetRegion("ap-guangzhou") //设置一个默认的存储桶地域 .Build(); string secretId = Environment.GetEnvironmentVariable("COS_KEY"); //云 API 密钥 SecretId string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); string bucket = "bucket-cssg-test-dotnet-1253653367"; //格式:BucketName-APPID string ownerUin = "1278687956"; //发起者身份标示: OwnerUin string subUin = "1278687956"; //发起者身份标示: SubUin PutBucketReplicationRequest request = new PutBucketReplicationRequest(bucket); //设置 replication PutBucketReplicationRequest.RuleStruct ruleStruct = new PutBucketReplicationRequest.RuleStruct(); ruleStruct.id = "replication_01"; //用来标注具体 Rule 的名称 ruleStruct.isEnable = true; //标识 Rule 是否生效 :true, 生效; false, 不生效 ruleStruct.appid = "1253653367"; //APPID ruleStruct.region = "ap-beijing"; //目标存储桶地域信息 ruleStruct.bucket = "bucket-cssg-assist-1253653367"; //格式:BucketName-APPID ruleStruct.prefix = "34"; //前缀匹配策略 List <PutBucketReplicationRequest.RuleStruct> ruleStructs = new List <PutBucketReplicationRequest.RuleStruct>(); ruleStructs.Add(ruleStruct); request.SetReplicationConfiguration(ownerUin, subUin, ruleStructs); // 使用同步方法 try { PutBucketReplicationResult result = cosXml.PutBucketReplication(request); Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); Assert.Null(clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.Null(serverEx); } //.cssg-snippet-body-end }
public override void DeleteFile(IVirgoFile file) { var ossSettings = _config.FileUploadOptions.Settings.Where(x => x.Key.ToLower() == "tencos").Select(x => x.Value).FirstOrDefault(); FileHandlerOptions groupInfo = null; if (string.IsNullOrEmpty(file.ExtraInfo)) { groupInfo = ossSettings?.FirstOrDefault(); } else { groupInfo = ossSettings?.Where(x => x.GroupName.ToLower() == file.ExtraInfo.ToLower()).FirstOrDefault(); if (groupInfo == null) { groupInfo = ossSettings?.FirstOrDefault(); } } if (groupInfo == null) { return; } try { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(false) //设置默认 HTTPS 请求 .SetAppid(groupInfo.AppId) //设置腾讯云账户的账户标识 APPID .SetRegion(groupInfo.GroupName) .Build(); long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(groupInfo.Secret, groupInfo.Key, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); string bucket = groupInfo.GroupLocation + "-" + groupInfo.AppId; string key = file.FileName; //对象键 DeleteObjectRequest request = new DeleteObjectRequest(bucket, key); //执行请求 DeleteObjectResult result = cosXml.DeleteObject(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } return; }
static void Main(string[] args) { string secretId = Environment.GetEnvironmentVariable("COS_KEY"); // 腾讯云 SecretId string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); // 腾讯云 SecretKey string region = "ap-guangzhou"; // 存储桶所在地域 // 普通初始化方式 CosXmlConfig config = new CosXmlConfig.Builder() .SetRegion(region) .SetDebugLog(true) .Build(); // TCE 初始化方式 // string domain = "your.domain"; // 替换成您的 Domain // string endpoint = String.Format("cos.{0}.{1}", region, domain); // CosXmlConfig config = new CosXmlConfig.Builder() // .setEndpointSuffix(endpoint) // .SetRegion(region) // .SetDebugLog(true) // .Build(); long keyDurationSecond = 600; QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, keyDurationSecond); // service 初始化完成 CosXmlServer cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { // 创建存储痛 Console.WriteLine(" ======= Put Bucket ======"); putBucket(cosXml); // 上传对象 Console.WriteLine(" ======= Put Object ======"); string cosKey = putObject(cosXml); // 删除对象 Console.WriteLine(" ======= Delete Object ======"); deleteObject(cosXml, cosKey); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx.Message); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } finally { // 删除存储桶 Console.WriteLine(" ======= Delete Bucket ======"); deleteBucket(cosXml); } Console.WriteLine(" ======= Program End. ======"); }
/// <summary> /// 查询指定路径下面的对象 /// </summary> /// <param name="tEST"></param> /// <returns></returns> public ResultModel <ListBucket> GetBucketResult(string tEST) { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(SetConnectionTimeoutMs) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(SetReadWriteTimeoutMs) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(IsHttps) //设置默认 HTTPS 请求 .SetAppid(SetAppid) //设置腾讯云账户的账户标识 APPID .SetRegion(SetRegion) //设置一个默认的存储桶地域 .Build(); string secretId = SecretId; //云 API 密钥 SecretId string secretKey = SecretKey; //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); ResultModel <ListBucket> resu = new ResultModel <ListBucket>(); try { string bucket = "yuanguhl"; //格式:BucketName-APPID GetBucketRequest request = new GetBucketRequest(bucket); request.Region = "ap-beijing"; //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //获取 a/ 下的对象 if (!string.IsNullOrEmpty(tEST)) { request.SetPrefix(tEST); } //执行请求 GetBucketResult result = cosXml.GetBucket(request); //请求成功 resu.date = result.listBucket; resu.success = true; return(resu); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 resu.success = false; resu.message = clientEx.InnerException.ToString(); return(resu); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 resu.success = false; resu.message = serverEx.InnerException.ToString(); return(resu); } }
/// <summary> /// 高级接口上传对象 /// </summary> /// <returns></returns> public async Task TransferUploadFile(string filePath) { CosXmlConfig config = new CosXmlConfig.Builder() .SetRegion(_cosConfig.Region) //设置一个默认的存储桶地域 .Build(); string secretId = _cosConfig.SecretId; //云 API 密钥 SecretId string secretKey = _cosConfig.SecretKey; //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); // 初始化 TransferConfig TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); string bucket = _cosConfig.Bucket; //存储桶,格式:BucketName-APPID string cosPath = _cosConfig.UploadKey; //对象在存储桶中的位置标识符,即称对象键 // 上传对象 COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath); uploadTask.SetSrcPath(filePath); //本地文件绝对路径 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()); } }; await transferManager.UploadAsync(uploadTask); }
internal static string PutObject(CosXmlServer cosXml) { string cosKey = "cosKey"; byte[] tmpData = new byte[1024]; PutObjectRequest request = new PutObjectRequest(bucket, cosKey, tmpData); PutObjectResult result = cosXml.PutObject(request); Console.WriteLine(result.GetResultInfo()); return(cosKey); }
public void putBucketAcl() { //.cssg-snippet-body-start:[put-bucket-acl] CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid("1253653367") //设置腾讯云账户的账户标识 APPID .SetRegion("ap-guangzhou") //设置一个默认的存储桶地域 .Build(); string secretId = Environment.GetEnvironmentVariable("COS_KEY"); //云 API 密钥 SecretId string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "bucket-cssg-test-dotnet-1253653367"; //格式: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); Assert.Null(clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.Null(serverEx); } //.cssg-snippet-body-end }
public void download(string download_dir, string key = "md5list.json") { //初始化 CosXmlConfig string appid = "1255334966"; //设置腾讯云账户的账户标识 APPID string region = "ap-beijing"; //设置一个默认的存储桶地域 CosXmlConfig config = new CosXmlConfig.Builder() .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid(appid) //设置腾讯云账户的账户标识 APPID .SetRegion(region) //设置一个默认的存储桶地域 .SetDebugLog(true) //显示日志 .Build(); //创建 CosXmlConfig 对象 //方式1, 永久密钥 string secretId = Properties.Resources.TencentID; //"云 API 密钥 SecretId"; string secretKey = Properties.Resources.TencentKey; //"云 API 密钥 SecretKey"; long durationSecond = 1000; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); //初始化 CosXmlServer CosXmlServer cosXml = new CosXmlServer(config, cosCredentialProvider); try { string bucket = "thuai-1255334966"; //存储桶,格式:BucketName-APPID string localDir = System.IO.Path.GetDirectoryName(download_dir); //本地文件夹 <-------------- string localFileName = System.IO.Path.GetFileName(download_dir); //指定本地保存的文件名 <-------------- GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName); //设置签名有效时长 //request.SetSign(DateTimeOffset.UtcNow.ToString(), 1000); //设置进度回调 Dictionary <string, string> test = request.GetRequestHeaders(); request.SetCosProgressCallback(delegate(long completed, long total) { //Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }); //执行请求 GetObjectResult result = cosXml.GetObject(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { throw clientEx; } catch (COSXML.CosException.CosServerException serverEx) { throw serverEx; } }
/// <summary> /// 查询存储桶列表 /// </summary> public ResultModel <ListAllMyBuckets> GetChucunto() { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(SetConnectionTimeoutMs) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(SetReadWriteTimeoutMs) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(IsHttps) //设置默认 HTTPS 请求 .SetAppid(SetAppid) //设置腾讯云账户的账户标识 APPID .SetRegion(SetRegion) //设置一个默认的存储桶地域 .Build(); string secretId = SecretId; //云 API 密钥 SecretId string secretKey = SecretKey; //云 API 密钥 SecretKey long durationSecond = DurationSecond; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); ResultModel <ListAllMyBuckets> result = new ResultModel <ListAllMyBuckets>(); try { GetServiceRequest request = new GetServiceRequest(); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //执行请求 GetServiceResult cosresult = cosXml.GetService(request); result.date = cosresult.listAllMyBuckets; result.success = true; result.code = 200; result.message = cosresult.GetResultInfo(); //请求成功 return(result); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 result.message = clientEx.Message; result.success = false; return(result); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 result.message = serverEx.Message; result.success = false; return(result); } }
public void AddTencentCos(string path, OnProgressCallback postpath, string name) { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(SetConnectionTimeoutMs) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(SetReadWriteTimeoutMs) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(IsHttps) //设置默认 HTTPS 请求 .SetAppid(SetAppid) //设置腾讯云账户的账户标识 APPID .SetRegion(SetRegion) //设置一个默认的存储桶地域 .Build(); string secretId = SecretId; //云 API 密钥 SecretId string secretKey = SecretKey; //云 API 密钥 SecretKey long durationSecond = 60000; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "yuanguhl"; //存储桶,格式:BucketName-APPID string key = $"{DateTime.Now.ToString("yy")}/Add/" + name; //对象在存储桶中的位置,即称对象键 string srcPath = path; //本地文件绝对路径 if (!File.Exists(srcPath)) { // 如果不存在目标文件,创建一个临时的测试文件 File.WriteAllBytes(srcPath, new byte[1024]); } PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath); request.Region = "ap-beijing"; //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置进度回调 request.SetCosProgressCallback(postpath); //执行请求 PostObjectResult result = cosXml.PostObject(request); //请求成功 } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 throw clientEx; } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 throw serverEx; } }
public void TestHostEndpoint() { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionLimit(512) .SetEndpointSuffix("cos.accelerate.myqcloud.com") .Build(); CosXmlServer cosXml = new CosXmlServer(config, null); string bucket = "bucket-125000"; GetObjectRequest request = new GetObjectRequest(bucket, "aKey", null, null); try { cosXml.GetObject(request); } catch (Exception) { // ignore } Assert.AreEqual(bucket + "." + config.endpointSuffix, request.GetHost()); GetBucketRequest bucketRequest = new GetBucketRequest(bucket); try { cosXml.GetBucket(bucketRequest); } catch (Exception) { // ignore } Assert.AreEqual(bucket + "." + config.endpointSuffix, bucketRequest.GetHost()); // test service request GetServiceRequest serviceRequest = new GetServiceRequest(); Assert.AreEqual("service.cos.myqcloud.com", serviceRequest.GetHost()); string serviceHost = "service.cos.csp.com"; serviceRequest.host = serviceHost; Assert.AreEqual(serviceHost, serviceRequest.GetHost()); }
public void GetFileDow(string keyw, string name, OnProgressCallback Dwjindu, string path) { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(SetConnectionTimeoutMs) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(SetReadWriteTimeoutMs) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(IsHttps) //设置默认 HTTPS 请求 .SetAppid(SetAppid) //设置腾讯云账户的账户标识 APPID .SetRegion(SetRegion) //设置一个默认的存储桶地域 .Build(); string secretId = SecretId; //云 API 密钥 SecretId string secretKey = SecretKey; //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "yuanguhl"; //存储桶,格式:BucketName-APPID string key = keyw; //对象在存储桶中的位置,即称对象键 string localDir = path; //本地文件夹 string localFileName = name; //指定本地保存的文件名 GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName); request.Region = "ap-beijing"; //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置进度回调 request.SetCosProgressCallback(Dwjindu); //执行请求 GetObjectResult result = cosXml.GetObject(request); //请求成功 } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 throw clientEx; } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 throw serverEx; } }
public void optionObject() { //.cssg-snippet-body-start:[option-object] CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid("1253653367") //设置腾讯云账户的账户标识 APPID .SetRegion("ap-guangzhou") //设置一个默认的存储桶地域 .Build(); string secretId = Environment.GetEnvironmentVariable("COS_KEY"); //云 API 密钥 SecretId string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "bucket-cssg-test-dotnet-1253653367"; //存储桶,格式:BucketName-APPID string key = "object4dotnet"; //对象在存储桶中的位置,即称对象键 string origin = "http://cloud.tencent.com"; string accessMthod = "PUT"; OptionObjectRequest request = new OptionObjectRequest(bucket, key, origin, accessMthod); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //执行请求 OptionObjectResult result = cosXml.OptionObject(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); Assert.Null(clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.Null(serverEx); } //.cssg-snippet-body-end }
internal static void UploadDirectory(CosXmlServer cosXml) { //.cssg-snippet-body-start:[transfer-upload-file] // 初始化 TransferConfig TransferConfig transferConfig = new TransferConfig(); // 初始化 TransferManager TransferManager transferManager = new TransferManager(cosXml, transferConfig); //本地文件夹绝对路径 String dir = @"本地文件夹绝对路径"; var files = System.IO.Directory.GetFiles(dir); var tasks = new List <Task>(); foreach (var file in files) { Console.WriteLine("Enqueue Upload: " + file); //对象在存储桶中的位置标识符,即称对象键 String cosPath = new FileInfo(file).Name; // 上传对象 COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath); uploadTask.SetSrcPath(file); tasks.Add(transferManager.UploadAsync(uploadTask)); } try { // Wait for all the tasks to finish. Task.WaitAll(tasks.ToArray()); // We should never get to this point Console.WriteLine("Upload Directory Complete"); } catch (AggregateException e) { Console.WriteLine("\nThe following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)"); for (int j = 0; j < e.InnerExceptions.Count; j++) { Console.WriteLine("\n-------------------------------------------------\n{0}", e.InnerExceptions[j].ToString()); } } }
/// <summary> /// 创建腾讯云服务器SDK访问对象 /// </summary> /// <param name="appid">appid</param> /// <param name="region">地区</param> /// <param name="secretId">secretId</param> /// <param name="secretKey">secretKey</param> /// <param name="durationSecond">有效时长,单位为 秒</param> /// <returns></returns> private static CosXmlServer CreateCosXmlServer(string appid, string region, string secretId, string secretKey, long durationSecond = 600) { //初始化 CosXmlConfig CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) .SetReadWriteTimeoutMs(40000) .IsHttps(true) .SetAppid(appid) .SetRegion(region) .SetDebugLog(true) .Build(); QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); //初始化 CosXmlServer _cosXml = new CosXmlServer(config, cosCredentialProvider); return(CosXml); }
public void getBucket() { //.cssg-snippet-body-start:[get-bucket] CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid("1253653367") //设置腾讯云账户的账户标识 APPID .SetRegion("ap-guangzhou") //设置一个默认的存储桶地域 .Build(); string secretId = Environment.GetEnvironmentVariable("COS_KEY"); //云 API 密钥 SecretId string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "bucket-cssg-test-dotnet-1253653367"; //格式:BucketName-APPID GetBucketRequest request = new GetBucketRequest(bucket); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //获取 a/ 下的对象 request.SetPrefix("a/"); //执行请求 GetBucketResult result = cosXml.GetBucket(request); //bucket的相关信息 ListBucket info = result.listBucket; } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); Assert.Null(clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.Null(serverEx); } //.cssg-snippet-body-end }
public QCloud() { LoadConfigs(); var config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位 毫秒 ,默认 45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位 毫秒 ,默认 45000ms .IsHttps(true) //设置默认 https 请求 .SetAppid(appid) //设置腾讯云账户的账户标识 APPID .SetRegion(region) //设置一个默认的存储桶地域 .SetDebugLog(true) //显示日志 .Build(); //创建 CosXmlConfig 对象 cosCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); cosSserver = new CosXmlServer(config, cosCredentialProvider); }
public static CosXml InitializeCosConfig() { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid(AppId) //设置腾讯云账户的账户标识 APPID .SetRegion(CosConfig.REGION) //设置一个默认的存储桶地域 .Build(); long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(SecretId, SecretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); return(cosXml); }