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());
        }
Exemple #2
0
        public TencentCloudClient(TencentCloudCosBlobProviderConfiguration configuration)
        {
            //InitCosXml();
            Client = new Lazy <CosXml>(() =>
            {
                // 初始化 CosXmlConfig
                CosXmlConfig config = new CosXmlConfig.Builder()
                                      .SetConnectionLimit(1024)
                                      .SetConnectionTimeoutMs(60000)
                                      .SetReadWriteTimeoutMs(40000)
                                      .IsHttps(true)
                                      .SetAppid(configuration.AppId)
                                      .SetDebugLog(true)
                                      .SetRegion(configuration.Region)
                                      .Build();

                QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(configuration.SecretId, configuration.SecretKey, 600);

                ////使用临时密钥, 推荐
                ////开发者拥有的项目身份识别 临时ID,用以身份认证
                //string tmpSecretId = "xxxx"; //https://cloud.tencent.com/document/product/436/14048
                ////开发者拥有的项目身份 临时密钥,用以身份认证
                //string tmpSecretKey = "xxxx"; //https://cloud.tencent.com/document/product/436/14048
                ////临时密钥 token
                //string tmpToken = "xxxx"; //https://cloud.tencent.com/document/product/436/14048
                ////临时密钥有效截止时间,UNIX 时间戳
                //long tmpExpireTime = 1546917879; //https://cloud.tencent.com/document/product/436/14048
                //QCloudCredentialProvider cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(tmpSecretId, tmpSecretKey, tmpExpireTime, tmpToken);

                //初始化 CosXmlServer
                return(new CosXmlServer(config, cosCredentialProvider));
            });
        }
        private QCloudServer()
        {
            appid = Environment.GetEnvironmentVariable("APPID");
            string secretId  = Environment.GetEnvironmentVariable("SECRETID");
            string secretKey = Environment.GetEnvironmentVariable("SECRETKEY");

            region = Environment.GetEnvironmentVariable("REGION");
            bucketForBucketTest = Environment.GetEnvironmentVariable("BUCKETONE");
            bucketForObjectTest = Environment.GetEnvironmentVariable("BUCKETTWO");

            if (appid == null)
            {
                appid               = Environment.GetEnvironmentVariable("APPID", EnvironmentVariableTarget.Machine);
                secretId            = Environment.GetEnvironmentVariable("SECRETID", EnvironmentVariableTarget.Machine);
                secretKey           = Environment.GetEnvironmentVariable("SECRETKEY", EnvironmentVariableTarget.Machine);
                region              = Environment.GetEnvironmentVariable("REGION", EnvironmentVariableTarget.Machine);
                bucketForBucketTest = Environment.GetEnvironmentVariable("BUCKETONE", EnvironmentVariableTarget.Machine);
                bucketForObjectTest = Environment.GetEnvironmentVariable("BUCKETTWO", EnvironmentVariableTarget.Machine);
            }

            CosXmlConfig config = new CosXmlConfig.Builder()
                                  .SetAppid(appid)
                                  .SetRegion(region)
                                  .SetDebugLog(true)
                                  .SetConnectionLimit(512)
                                  .Build();


            long keyDurationSecond = 600;
            QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, keyDurationSecond);

            cosXml = new CosXmlServer(config, qCloudCredentialProvider);
        }
Exemple #4
0
        /// <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);
        }
Exemple #5
0
        public void init()
        {
            string region = "ap-guangzhou";

            CosXmlConfig config = new CosXmlConfig.Builder()
                                  .SetRegion(region)
                                  .SetDebugLog(true)
                                  .SetConnectionLimit(512)
                                  .Build();

            QCloudCredentialProvider qCloudCredentialProvider = new DefaultSessionQCloudCredentialProvider(
                "AKIDLT2qvPSOSRq5eM2TR11daHYBfT1rl8fuWgAJhtbo3eFwcbpFnZHAYkgpaFrzvtq4",
                "i19oUWRIvvpqGpiUgwqa3wufzSbDPyaxv0JYArSQFMc=",
                1597907089,
                "YiPWtzGhVPG5HQlOLQdxZrxHOitu1JXL903fda3a64f28616997f861116b2d7d1EyG_RmvCKqArCLtOXxKv9MGtugt4lPcUbESOTHdhmhEyMYgnPpJSLpbQoKPYzg7WwwvjjXqOKQvs_I9iCHyaBXJu_LToqeuQfO4NFpos8I_NWBKxg23xFpvuXHXJ7kDuyudwu7qInonvJpoXAnb_J-7rMY94aVWSg6kolryvO-x5_C3cBa1fIiKTrwkRP4keh0g3_asIwP99DA-WASJ147C_NsZl9fdTlWEBnIeQqB7uRHAZkF-_f03-fBYJofeYGe7FArrfai4_AO57QbTwacfzHk2s-43WuCIhoLud_-k"
                );

            cosXml = new CosXmlServer(config, qCloudCredentialProvider);

            transferManager = new TransferManager(cosXml, new TransferConfig());

            smallFileSrcPath = QCloudServer.CreateFile(TimeUtils.GetCurrentTime(TimeUnit.SECONDS) + ".txt", 1024 * 1024 * 1);
            bigFileSrcPath   = QCloudServer.CreateFile(TimeUtils.GetCurrentTime(TimeUnit.SECONDS) + ".txt", 1024 * 1024 * 10);
            FileInfo      fileInfo      = new FileInfo(smallFileSrcPath);
            DirectoryInfo directoryInfo = fileInfo.Directory;

            localDir      = directoryInfo.FullName;
            localFileName = "local.txt";
        }
 /// 设置全球加速域名
 public void SetAccelerateDomain()
 {
     //.cssg-snippet-body-start:[set-accelerate-domain]
     CosXmlConfig config = new CosXmlConfig.Builder()
                           .setEndpointSuffix("cos.accelerate.myqcloud.com")
                           .Build();
     //.cssg-snippet-body-end
 }
Exemple #7
0
        /// <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("");
            }
        }
Exemple #8
0
        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 void SetEndpointSuffix()
 {
     //.cssg-snippet-body-start:[set-endpoint-suffix]
     CosXmlConfig config = new CosXmlConfig.Builder()
                           //请求域名为 [bucketName-APPID].your.domain.com
                           .setEndpointSuffix("your.domain.com")
                           .Build();
     //.cssg-snippet-body-end
 }
Exemple #10
0
        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 void SetCustomDomain()
 {
     //.cssg-snippet-body-start:[set-custom-domain]
     CosXmlConfig config = new CosXmlConfig.Builder()
                           .SetRegion("COS_REGION")    //设置一个默认的存储桶地域
                                                       //请求域名为 your.domain.com
                           .setHost("your.domain.com") //自定义域名
                           .Build();
     //.cssg-snippet-body-end
 }
Exemple #12
0
        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. ======");
        }
Exemple #13
0
        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;
        }
Exemple #14
0
        public QCloudOSSService(IMemoryCache cache, OSSOptions options) : base(cache, options)
        {
            CosXmlConfig config = new CosXmlConfig.Builder()
                                  .IsHttps(options.IsEnableHttps)
                                  .SetRegion(options.Region)
                                  .SetDebugLog(false)
                                  .Build();
            QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(options.AccessKey, options.SecretKey, 600);

            this._client = new CosXmlServer(config, cosCredentialProvider);
        }
Exemple #15
0
        public CosUpload()
        {
            CosXmlConfig config = new CosXmlConfig.Builder()
                                  .SetRegion(Uploader.cosRegion) //设置一个默认的存储桶地域
                                  .Build();
            long durationSecond = 600;                           //每次请求签名有效时长,单位为秒
            QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(Uploader.cosSecretId,
                                                                                                    Uploader.cosSecretKey, durationSecond);

            this.bucket = Uploader.cosBucket;
            this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
        }
Exemple #16
0
        /// <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);
            }
        }
Exemple #17
0
        public static CosXmlConfig getClientConfig()
        {
            CosXmlConfig config = new CosXmlConfig.Builder()
                                  .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位 毫秒 ,默认 45000ms
                                  .SetReadWriteTimeoutMs(40000)  //设置读写超时时间,单位 毫秒 ,默认 45000ms
                                  .IsHttps(true)                 //设置默认 https 请求
                                  .SetAppid(BosConfig.AppId)     //设置腾讯云账户的账户标识 APPID
                                  .SetRegion(BosConfig.Endpoint) //设置一个默认的存储桶地域

                                  .Build();                      //创建 CosXmlConfig 对象

            return(config);
        }
        public CosXml NewService(string newRegion)
        {
            CosXmlConfig config = new CosXmlConfig.Builder()
                                  .SetAppid(appid)
                                  .SetRegion(newRegion)
                                  .SetDebugLog(true)
                                  .SetConnectionLimit(512)
                                  .Build();

            QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, 600);

            return(new CosXmlServer(config, qCloudCredentialProvider));
        }
        /// <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);
        }
Exemple #20
0
            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;
                }
            }
Exemple #21
0
        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
        }
        CopyObjectModel()
        {
            CosXmlConfig config = new CosXmlConfig.Builder()
                                  .SetRegion("COS_REGION") //设置一个默认的存储桶地域
                                  .Build();

            string secretId       = "COS_SECRETID";  //云 API 密钥 SecretId
            string secretKey      = "COS_SECRETKEY"; //云 API 密钥 SecretKey
            long   durationSecond = 600;             //每次请求签名有效时长,单位为秒
            QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
                                                                                                    secretKey, durationSecond);

            this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
        }
Exemple #23
0
        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;
            }
        }
Exemple #24
0
        /// <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);
            }
        }
Exemple #25
0
        private CosXml InitCosXml()
        {
            var cosXmlConfig = new CosXmlConfig.Builder()
                               .IsHttps(false)
                               .SetAppid(_cosConfig.AppId)
                               .SetRegion(_cosConfig.Region)
                               .SetDebugLog(true)
                               .Build();
            var cosCredentialProvider = new DefaultQCloudCredentialProvider(
                _cosConfig.SecretId,
                _cosConfig.SecretKey,
                _cosConfig.DurationSecond);

            return(new CosXmlServer(cosXmlConfig, cosCredentialProvider));
        }
Exemple #26
0
        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());
        }
Exemple #27
0
        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;
            }
        }
Exemple #28
0
        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
        }
Exemple #29
0
        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);
        }
Exemple #30
0
        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
        }