示例#1
0
        public static void AsynPostObject(COSXML.CosXml cosXml, string bucket, string key, string srcPath, PostObjectRequest.Policy policy)
        {
            PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath);

            //设置签名有效时长
            request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);

            request.SetCosProgressCallback(delegate(long completed, long total)
            {
                Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total));
            });

            //设置policy
            request.SetPolicy(policy);

            //执行请求
            cosXml.PostObject(request, delegate(CosResult result)
            {
                PostObjectResult getObjectResult = result as PostObjectResult;
                Console.WriteLine(getObjectResult.GetResultInfo());
            }, delegate(CosClientException clientEx, CosServerException serverEx)
            {
                if (clientEx != null)
                {
                    QLog.D("XIAO", clientEx.Message);
                    Console.WriteLine("CosClientException: " + clientEx.StackTrace);
                }
                if (serverEx != null)
                {
                    QLog.D("XIAO", serverEx.Message);
                    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                }
            });
        }
示例#2
0
        public static void PostObject(COSXML.CosXml cosXml, string bucket, string key, string srcPath, PostObjectRequest.Policy policy)
        {
            try
            {
                PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath);
                //设置签名有效时长
                //request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
                List <string> headers = new List <string>();
                headers.Add("Host");
                request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600, headers, null);

                request.SetCosProgressCallback(delegate(long completed, long total)
                {
                    Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total));
                });

                //设置policy
                request.SetPolicy(policy);

                //执行请求
                PostObjectResult result = cosXml.PostObject(request);

                Console.WriteLine(result.GetResultInfo());
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                QLog.D("XIAO", clientEx.Message);
                Console.WriteLine("CosClientException: " + clientEx.StackTrace);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                QLog.D("XIAO", serverEx.Message);
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }
        }
示例#3
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;
            }
        }