Пример #1
0
        public static void Upload(string bucket, string key, string srcPath, long offset, long sendContentLength)
        {
            COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, null, key);

            uploadTask.SetSrcPath(srcPath, offset, sendContentLength);
            uploadTask.progressCallback = delegate(long completed, long total)
            {
                Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total));
            };
            uploadTask.successCallback = delegate(CosResult cosResult)
            {
                COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = cosResult as COSXML.Transfer.COSXMLUploadTask.UploadTaskResult;
                QLog.D("XIAO", result.GetResultInfo());
                Console.WriteLine(result.GetResultInfo());
                Console.WriteLine(String.Format("currentThread id = {0}", Thread.CurrentThread.ManagedThreadId));
            };
            uploadTask.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.Upload(uploadTask);
        }
Пример #2
0
        internal static async Task <String> PutObject(CosXmlServer cosXml)
        {
            string cosKey = "cosKey";
            //.cssg-snippet-body-start:[transfer-upload-file]
            // 初始化 TransferConfig
            TransferConfig transferConfig = new TransferConfig();

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

            String cosPath = cosKey;    //对象在存储桶中的位置标识符,即称对象键
            String srcPath = @"本地绝对路径"; //本地文件绝对路径

            // 上传对象
            COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);

            uploadTask.SetSrcPath(srcPath);

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

            try {
                COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = await
                                                                           transferManager.UploadAsync(uploadTask);

                Console.WriteLine(result.GetResultInfo());
                string eTag = result.eTag;
            } catch (Exception e) {
                Console.WriteLine("CosException: " + e);
            }

            return(cosKey);
        }
        /// 高级接口上传对象
        public async void TransferUploadFile()
        {
            //.cssg-snippet-body-start:[transfer-upload-file]
            // 初始化 TransferConfig
            TransferConfig transferConfig = new TransferConfig();

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

            String bucket  = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
            String cosPath = "exampleobject";            //对象在存储桶中的位置标识符,即称对象键
            String srcPath = @"temp-source-file";        //本地文件绝对路径

            // 上传对象
            COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);

            uploadTask.SetSrcPath(srcPath);

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

            try {
                COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = await
                                                                           transferManager.UploadAsync(uploadTask);

                Console.WriteLine(result.GetResultInfo());
                string eTag = result.eTag;
            } catch (Exception e) {
                Console.WriteLine("CosException: " + e);
            }

            //.cssg-snippet-body-end
        }
Пример #4
0
        /// <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);
        }
        public static void UploadWithUploadId(CosXml cosXml, string bucket, string key, string srcPath, long offset, long sendContentLength)
        {
            TransferConfig transferConfig = new TransferConfig();

            transferManager = new TransferManager(cosXml, new TransferConfig());
            COSXML.Model.Object.InitMultipartUploadRequest initMultipartUploadRequest = new COSXML.Model.Object.InitMultipartUploadRequest(bucket, key);
            COSXML.Model.Object.InitMultipartUploadResult  initMultipartUploadResult  = cosXml.InitMultipartUpload(initMultipartUploadRequest);
            string upLoadId = initMultipartUploadResult.initMultipartUpload.uploadId;

            //string content = "this is partNumber 1";
            byte[] data = new byte[transferConfig.SliceSizeForUpload];
            COSXML.Model.Object.UploadPartRequest uploadPartRequest = new COSXML.Model.Object.UploadPartRequest(bucket, key, 1, upLoadId, data);
            COSXML.Model.Object.UploadPartResult  uploadPartResult  = cosXml.UploadPart(uploadPartRequest);
            string eTag = uploadPartResult.eTag;

            COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, null, key)
            {
                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.COSXMLUploadTask.UploadTaskResult result = cosResult as COSXML.Transfer.COSXMLUploadTask.UploadTaskResult;
                    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));
                }
            };

            uploadTask.SetSrcPath(srcPath, offset, sendContentLength);
            uploadTask.SetUploadId(upLoadId);
            transferManager.Upload(uploadTask);
        }
        /// 高级接口上传对象
        public void TransferUploadFile()
        {
            //.cssg-snippet-body-start:[transfer-upload-file]
            // 初始化 TransferConfig
            TransferConfig transferConfig = new TransferConfig();

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

            String bucket  = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID
            String cosPath = "exampleobject";            //对象在存储桶中的位置标识符,即称对象键
            String srcPath = @"temp-source-file";        //本地文件绝对路径

            // 上传对象
            COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);

            uploadTask.SetSrcPath(srcPath);

            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());
                }
            };
            transferManager.Upload(uploadTask);

            //.cssg-snippet-body-end
        }
Пример #7
0
        public void testUploadTask()
        {
            string key = multiKey;

            PutObjectRequest request = new PutObjectRequest(bucket, key, bigFileSrcPath);

            request.SetRequestHeader("Content-Type", "image/png");

            COSXMLUploadTask uploadTask = new COSXMLUploadTask(request);

            uploadTask.SetSrcPath(bigFileSrcPath);

            var    autoEvent = new AutoResetEvent(false);
            string eTag      = null;

            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());
                autoEvent.Set();
                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());
                }
                autoEvent.Set();
            };
            transferManager.Upload(uploadTask);
            autoEvent.WaitOne();
            Assert.NotNull(eTag);
        }