コード例 #1
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;
                }
            }