Esempio n. 1
0
        public void testDownloadTask()
        {
            long             now     = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
            GetObjectRequest request = new GetObjectRequest(bucket,
                                                            commonKey, localDir, localFileName);

            request.LimitTraffic(8 * 1000 * 1024);
            //执行请求
            COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(request);

            var autoEvent = new AutoResetEvent(false);

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

            downloadTask.successCallback = delegate(CosResult cosResult)
            {
                COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = cosResult as COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult;
                Console.WriteLine(result.GetResultInfo());
                autoEvent.Set();
                Assert.True(cosResult.httpCode == 200);
            };

            downloadTask.failCallback = delegate(CosClientException clientEx, CosServerException serverEx)
            {
                if (clientEx != null)
                {
                    Console.WriteLine("CosClientException: " + clientEx);
                }
                if (serverEx != null)
                {
                    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
                }
                autoEvent.Set();
                Assert.True(false);
            };

            long costTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds() - now;

            transferManager.Download(downloadTask);
            autoEvent.WaitOne();
        }
Esempio n. 2
0
        /// 下载时对单链接限速
        public async void DownloadObjectTrafficLimit()
        {
            //.cssg-snippet-body-start:[download-object-traffic-limit]
            TransferConfig transferConfig = new TransferConfig();

            // 初始化 TransferManager
            TransferManager transferManager = new TransferManager(cosXml, transferConfig);

            String bucket        = "examplebucket-1250000000";   //存储桶,格式:BucketName-APPID
            String cosPath       = "exampleobject";              //对象在存储桶中的位置标识符,即称对象键
            string localDir      = System.IO.Path.GetTempPath(); //本地文件夹
            string localFileName = "my-local-temp-file";         //指定本地保存的文件名

            GetObjectRequest request = new GetObjectRequest(bucket,
                                                            cosPath, localDir, localFileName);

            request.LimitTraffic(8 * 1000 * 1024); // 限制为1MB/s

            COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(request);
            await transferManager.DownloadAsync(downloadTask);

            //.cssg-snippet-body-end
        }