Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        /// 高级接口下载对象
        public async void TransferDownloadObject()
        {
            //.cssg-snippet-body-start:[transfer-download-object]
            // 初始化 TransferConfig
            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";         //指定本地保存的文件名

            // 下载对象
            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);
            }
            //.cssg-snippet-body-end
        }
Esempio n. 3
0
        public static void Download(string bucket, string key, string localDir, string localFileName)
        {
            COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, null, key, localDir, localFileName)
            {
                progressCallback = delegate(long completed, long total)
                {
                    Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total));
                },
                successCallback = delegate(CosResult cosResult)
                {
                    COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = cosResult as COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult;
                    QLog.D("XIAO", result.GetResultInfo());
                    Console.WriteLine(result.GetResultInfo());
                    Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId));
                },
                failCallback = 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());
                    }
                    Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId));
                }
            };

            transferManager.Download(downloadTask);
        }
Esempio n. 4
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. 5
0
        public async Task DownloadObject(string key, string localDir, string localFileName)
        {
            var downloadTask = GetDownloadTask(key, localDir, localFileName);


            // 初始化 TransferConfig
            TransferConfig transferConfig = new TransferConfig();

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


            try
            {
                COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = await transferManager.DownloadAsync(downloadTask);

                string eTag = result.eTag;
            }
            catch (Exception e)
            {
                logger.LogError(e.ToLogMessage());
            }
        }