Esempio n. 1
0
        private static void UploadPartCallback(IAsyncResult ar)
        {
            var result         = client.EndUploadPart(ar);
            var wrappedContext = (UploadPartContextWrapper)ar.AsyncState;

            wrappedContext.PartStream.Close();

            var ctx = wrappedContext.Context;

            lock (ctx.SyncLock)
            {
                var partETags = ctx.PartETags;
                partETags.Add(new PartETag(wrappedContext.PartNumber, result.ETag));
                ctx.CompletedParts++;

                Console.WriteLine("finish {0}/{1}", ctx.CompletedParts, ctx.TotalParts);
                if (ctx.CompletedParts == ctx.TotalParts)
                {
                    partETags.Sort((e1, e2) => (e1.PartNumber - e2.PartNumber));
                    var completeMultipartUploadRequest =
                        new CompleteMultipartUploadRequest(ctx.BucketName, ctx.ObjectName, ctx.UploadId);
                    foreach (var partETag in partETags)
                    {
                        completeMultipartUploadRequest.PartETags.Add(partETag);
                    }

                    var completeMultipartUploadResult = client.CompleteMultipartUpload(completeMultipartUploadRequest);
                    Console.WriteLine(@"Async upload multipart result : " + completeMultipartUploadResult.Location);

                    ctx.WaitEvent.Set();
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 上传资源
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="mimeType">文件类型</param>
        /// <param name="stream"></param>
        public void Upload(string fileName, string mimeType, Stream stream)
        {
            //根据资源大小计算Multipart 2MB为一个Part
            int num = (int)(stream.Length / 0x200000L);

            if (num > 0 && (stream.Length % 0x200000L) != 0)
            {
                num += 1;
            }

            ObjectMetadata metadata = new ObjectMetadata()
            {
                ContentType = mimeType
            };

            try
            {
                if (num > 1)
                {
                    InitiateMultipartUploadRequest initiateMultipartUploadRequest =
                        new InitiateMultipartUploadRequest(_bucketName, fileName)
                    {
                        ObjectMetadata = metadata
                    };
                    InitiateMultipartUploadResult result =
                        _ossClient.InitiateMultipartUpload(initiateMultipartUploadRequest);
                    Console.WriteLine("UploadId:" + result.UploadId);
                    List <PartETag> collection = new List <PartETag>();

                    for (int i = 0; i < num; i++)
                    {
                        int begin = 0x200000 * i;
                        stream.Seek((long)begin, SeekOrigin.Begin);
                        long end = 0x200000 < (stream.Length - begin) ? 0x200000L : stream.Length - begin;

                        UploadPartRequest uploadPartRequest = new UploadPartRequest(_bucketName, fileName,
                                                                                    result.UploadId)
                        {
                            InputStream = stream,
                            PartSize    = end,
                            PartNumber  = i + 1
                        };
                        var partResult = _ossClient.UploadPart(uploadPartRequest);
                        collection.Add(partResult.PartETag);
                    }
                    var completeMultipartUploadRequest = new CompleteMultipartUploadRequest(_bucketName, fileName,
                                                                                            result.UploadId);
                    ((List <PartETag>)completeMultipartUploadRequest.PartETags).AddRange(collection);
                    _ossClient.CompleteMultipartUpload(completeMultipartUploadRequest);
                }
                else
                {
                    _ossClient.PutObject(_bucketName, fileName, stream, metadata);
                }
            }
            catch (WebException exception)
            {
                throw new Exception("上传失败,请重试", exception);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 拷贝大文件
        /// </summary>
        /// <param name="client"></param>
        /// <param name="sourceBucket">源空间</param>
        /// <param name="sourceKey">源文件</param>
        /// <param name="optBucket">目标空间</param>
        /// <param name="optKey">目标文件</param>
        /// <returns></returns>
        private CopyFileResultDto CopyBigFile(OssClient client, string sourceBucket, string sourceKey,
                                              string optBucket, string optKey)
        {
            var initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(optBucket, optKey);
            var result = client.InitiateMultipartUpload(initiateMultipartUploadRequest);

            var partSize  = Core.Tools.GetPartSize(ChunkUnit.U4096K);
            var metadata  = client.GetObjectMetadata(sourceBucket, sourceKey);
            var fileSize  = metadata.ContentLength;
            var partCount = (int)fileSize / partSize;

            if (fileSize % partSize != 0)
            {
                partCount++;
            }

            // 开始分片拷贝。
            var partETags = new List <PartETag>();

            for (var i = 0; i < partCount; i++)
            {
                var skipBytes = (long)partSize * i;
                var size      = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
                // 创建UploadPartCopyRequest。可以通过UploadPartCopyRequest指定限定条件。
                var uploadPartCopyRequest =
                    new UploadPartCopyRequest(optBucket, optKey, sourceBucket, sourceKey,
                                              result.UploadId)
                {
                    PartSize   = size,
                    PartNumber = i + 1,
                    // BeginIndex用来定位此次上传分片开始所对应的位置。
                    BeginIndex = skipBytes
                };
                // 调用uploadPartCopy方法来拷贝每一个分片。
                var uploadPartCopyResult = client.UploadPartCopy(uploadPartCopyRequest);
                Console.WriteLine("UploadPartCopy : {0}", i);
                partETags.Add(uploadPartCopyResult.PartETag);
            }

            // 完成分片拷贝。
            var completeMultipartUploadRequest =
                new CompleteMultipartUploadRequest(optBucket, optKey, result.UploadId);

            // partETags为分片上传中保存的partETag的列表,OSS收到用户提交的此列表后,会逐一验证每个数据分片的有效性。全部验证通过后,OSS会将这些分片合成一个完整的文件。
            foreach (var partETag in partETags)
            {
                completeMultipartUploadRequest.PartETags.Add(partETag);
            }

            var ret = client.CompleteMultipartUpload(completeMultipartUploadRequest);

            if (ret.HttpStatusCode == HttpStatusCode.OK)
            {
                return(new CopyFileResultDto(true, sourceKey, "success"));
            }

            return(new CopyFileResultDto(false, sourceKey,
                                         $"RequestId:{ret.RequestId},HttpStatusCode:{ret.HttpStatusCode}"));
        }
Esempio n. 4
0
        private static CompleteMultipartUploadResult CompleteUploadPart(String bucketName, String objectName,
                                                                        String uploadId, List <PartETag> partETags)
        {
            var completeMultipartUploadRequest =
                new CompleteMultipartUploadRequest(bucketName, objectName, uploadId);

            foreach (var partETag in partETags)
            {
                completeMultipartUploadRequest.PartETags.Add(partETag);
            }

            return(OssClient.CompleteMultipartUpload(completeMultipartUploadRequest));
        }
        /// <summary>
        /// 分片上传
        /// </summary>
        /// <param name="localPath"></param>
        /// <param name="path"></param>
        /// <param name="fileSize"></param>
        /// <returns></returns>
        private bool uploadMulti(string localPath, string path, long fileSize)
        {
            string endPoint = "http://" + m_endPoint;
            var    client   = new OssClient(endPoint, m_keyID, m_keySecret);

            try
            {
                var    request  = new InitiateMultipartUploadRequest(m_bucket, path);
                var    result   = client.InitiateMultipartUpload(request);
                string uploadId = result.UploadId;

                int partSize = (int)(fileSize / 9990);
                if (partSize < m_blockSize)
                {
                    partSize = m_blockSize;
                }
                int partCount = (int)(fileSize / partSize);
                var partETags = new List <PartETag>();
                using (var fs = File.Open(localPath, FileMode.Open, FileAccess.Read))
                {
                    for (int i = 0; i < partCount; i++)
                    {
                        long skipBytes = partSize * i;
                        fs.Seek(skipBytes, 0);
                        long size = (i < partCount - 1) ? partSize : (fileSize - skipBytes);
                        var  req  = new UploadPartRequest(m_bucket, path, uploadId)
                        {
                            InputStream = fs,
                            PartSize    = size,
                            PartNumber  = i + 1
                        };
                        var res = client.UploadPart(req);
                        partETags.Add(res.PartETag);
                    }
                }

                var completeMultipartUploadRequest = new CompleteMultipartUploadRequest(m_bucket, path, uploadId);
                foreach (var partETag in partETags)
                {
                    completeMultipartUploadRequest.PartETags.Add(partETag);
                }
                client.CompleteMultipartUpload(completeMultipartUploadRequest);
            }
            catch (Exception ex)
            {
                Console.WriteLine("分片上传失败:" + ex.Message);
                return(false);
            }
            return(true);
        }
Esempio n. 6
0
        /// <summary>
        /// 上传完成
        /// </summary>
        /// <param name="task"></param>
        private void UploadComplete(OssTaskDO task)
        {
            //如果是暂停就跳出
            if (task.Progress < task.TotalProgress - 1)
            {
                //关闭内存流
                this.Close(task);
                //清除配置文件,只有删除状态删除配置
                if (task.Status == OssTaskStatus.DELETE)
                {
                    this.ClearConfig(task);
                }
                return;
            }

            //线上检查
            var ossPath          = CheckOssPath(task.UploadOssPath + @"/" + task.UploadOssName);
            var listPartsRequest = new ListPartsRequest(aliyunOSSConfig.BucketName, ossPath, task.UploadOssId);
            var listPartsResult  = client.ListParts(listPartsRequest);

            //生成Tag
            var             completeMultipartUploadRequest = new CompleteMultipartUploadRequest(aliyunOSSConfig.BucketName, ossPath, task.UploadOssId);
            List <PartETag> eTags = new List <PartETag>();

            foreach (var item in task.UploadETag)
            {
                var values = item.Split(',');
                eTags.Add(new PartETag(Convert.ToInt32(values[0]), values[1], values[2], Convert.ToInt64(values[3])));
            }
            eTags.Sort((x, y) => x.PartNumber.CompareTo(y.PartNumber));
            eTags.ForEach(it => completeMultipartUploadRequest.PartETags.Add(it));


            //合并文件上传完成
            client.CompleteMultipartUpload(completeMultipartUploadRequest);


            //保存配置
            task.Status         = OssTaskStatus.COMPLETE;
            task.ActualProgress = 100;
            SaveOssTaskConfig(taskList);

            //关闭内存流
            this.Close(task);
            //清除配置文件
            this.ClearConfig(task);

            //执行回执
            downloadCompleteCallBack?.Invoke(task);
        }
        private CompleteMultipartUploadResult CompleteUploadPart(string objectKey, string uploadId, List <PartETag> partETags)
        {
            CompleteMultipartUploadResult uploadResult = null;

            try
            {
                var completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, objectKey, uploadId);
                foreach (var partETag in partETags)
                {
                    completeMultipartUploadRequest.PartETags.Add(partETag);
                }
                uploadResult = client.CompleteMultipartUpload(completeMultipartUploadRequest);
            }
            catch (OssException ex)
            {
                lastError = ex;
            }

            return(uploadResult);
        }
        public void PutObjectMultipart(string bucketName, string key, string fileToUpload)
        {
            var partSize    = 1000 * 1000;
            var initRequest = new InitiateMultipartUploadRequest(bucketName, key);
            var uploadId    = ossClient.InitiateMultipartUpload(initRequest);
            var fi          = new FileInfo(fileToUpload);
            var fileSize    = fi.Length;
            var parCount    = fileSize / partSize;

            if (fileSize % partSize != 0)
            {
                parCount++;
            }

            var partEtags = new List <PartETag>();

            for (var i = 0; i < parCount; i++)
            {
                using (var fs = File.Open(fileToUpload, FileMode.Open))
                {
                    var skipBytes = (long)partSize * i;
                    fs.Seek(skipBytes, 0);
                    var size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
                    var uploadPartRequest = new UploadPartRequest(bucketName, key, uploadId.UploadId)
                    {
                        InputStream = fs,
                        PartSize    = size,
                        PartNumber  = i + 1
                    };
                    var upLoadPartRequestResult = ossClient.UploadPart(uploadPartRequest);
                    partEtags.Add(upLoadPartRequestResult.PartETag);
                }
            }
            var completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId.UploadId);

            foreach (var partETag in partEtags)
            {
                completeMultipartUploadRequest.PartETags.Add(partETag);
            }
            var completeResult = ossClient.CompleteMultipartUpload(completeMultipartUploadRequest);
        }
Esempio n. 9
0
        public void mutiPartUpload(OssClient ossClient_0, string string_0, string string_1, string string_2, int int_0)
        {
            InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(string_2, string_1);
            InitiateMultipartUploadResult  initiateMultipartUploadResult  = ossClient_0.InitiateMultipartUpload(initiateMultipartUploadRequest);
            int      num      = 1024 * int_0;
            FileInfo fileInfo = new FileInfo(string_0);
            int      num2     = (int)(fileInfo.Length / (long)num);

            if (fileInfo.Length % (long)num != 0L)
            {
                num2++;
            }
            List <PartETag> list = new List <PartETag>();

            for (int i = 0; i < num2; i++)
            {
                FileStream fileStream = new FileStream(fileInfo.FullName, FileMode.Open);
                long       num3       = (long)(num * i);
                fileStream.Position = num3;
                long             value            = ((long)num < fileInfo.Length - num3) ? ((long)num) : (fileInfo.Length - num3);
                UploadPartResult uploadPartResult = ossClient_0.UploadPart(new UploadPartRequest(string_2, string_1, initiateMultipartUploadResult.UploadId)
                {
                    InputStream = fileStream,
                    PartSize    = new long?(value),
                    PartNumber  = new int?(i + 1)
                });
                list.Add(uploadPartResult.PartETag);
                fileStream.Close();
            }
            CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(string_2, string_1, initiateMultipartUploadResult.UploadId);

            foreach (PartETag item in list)
            {
                completeMultipartUploadRequest.PartETags.Add(item);
            }
            ossClient_0.CompleteMultipartUpload(completeMultipartUploadRequest);
        }
Esempio n. 10
0
        public static void UploadToOSS(string fileMD5, string fileExt, Stream fs)
        {
            fs.Position = 0;
            var key = fileMD5 + "." + fileExt;

            //var uploadFile = fileLocalPath + @"\" + date + @"\" + key;
            try
            {
                bool UploadStatus = false;
                #region 从本地读取视频文件并上传

                var content = fs;
                //using (var content = File.Open(uploadFile, FileMode.Open))
                //{
                if (content.Length < 50 * 1024 * 1024) //50M
                {
                    //Common.WriteLog(string.Format("文件{0}上传开始", key));
                    var resultS = client.PutObject(bucketName, key, content);
                    UploadStatus = true;
                    //Common.WriteLog(string.Format("文件{0}上传成功,返回信息为{1}", key, resultS.ETag));
                }
                else
                {
                    //初始化分片上传
                    //Common.WriteLog(string.Format("文件{0}开始分片上传", key));
                    var request1 = new InitiateMultipartUploadRequest(bucketName, key);
                    var UploadId = client.InitiateMultipartUpload(request1).UploadId;

                    int partCount = 0;
                    var fileSize  = content.Length;
                    int partSize  = 10 * 1024 * 1024;
                    partCount = (int)(fileSize / partSize + (fileSize % partSize == 0 ? 0 : 1));


                    // 开始分片上传
                    var partETags = new List <PartETag>();
                    for (var i = 0; i < partCount; i++)
                    {
                        var skipBytes = (long)partSize * i;

                        //定位到本次上传片应该开始的位置
                        content.Seek(skipBytes, 0);

                        //计算本次上传的片大小,最后一片为剩余的数据大小,其余片都是part size大小。
                        var size    = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
                        var request = new UploadPartRequest(bucketName, key, UploadId)
                        {
                            InputStream = content,
                            PartSize    = size,
                            PartNumber  = i + 1
                        };

                        //调用UploadPart接口执行上传功能,返回结果中包含了这个数据片的ETag值
                        var result2 = client.UploadPart(request);
                        partETags.Add(result2.PartETag);
                    }
                    //完成分片上传
                    var completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, UploadId);
                    foreach (var partETag in partETags)
                    {
                        completeMultipartUploadRequest.PartETags.Add(partETag);
                    }
                    var resultEnd = client.CompleteMultipartUpload(completeMultipartUploadRequest);

                    UploadStatus = true;


                    //Common.WriteLog(string.Format("文件{0}分片上传结束", key));
                }

                //}
                #endregion
            }
            catch (Exception ex)
            {
                Common.CommonHelp.WriteLOG("阿里云上传问题:" + ex.Message);
            }
        }
Esempio n. 11
0
        public void MutiPartUpload(string fileName, string key)
        {
            Console.WriteLine("开始上传:" + key);
            InitiateMultipartUploadRequest initRequest =
                new InitiateMultipartUploadRequest(ossConfig.BucketName, key);
            InitiateMultipartUploadResult initResult = ossClient.InitiateMultipartUpload(initRequest);


            // 设置每块为 5M
            int partSize = 1024 * 1024 * 5;

            FileInfo partFile = new FileInfo(fileName);

            // 计算分块数目
            int partCount = (int)(partFile.Length / partSize);

            if (partFile.Length % partSize != 0)
            {
                partCount++;
            }
            Console.WriteLine("数据分块上传,一共:{0}块", partCount);
            // 新建一个List保存每个分块上传后的ETag和PartNumber
            List <PartETag> partETags = new List <PartETag>();

            for (int i = 0; i < partCount; i++)
            {
                // 获取文件流
                FileStream fis = new FileStream(partFile.FullName, FileMode.Open);

                // 跳到每个分块的开头
                long skipBytes = partSize * i;
                fis.Position = skipBytes;
                //fis.skip(skipBytes);

                // 计算每个分块的大小
                long size = partSize < partFile.Length - skipBytes ?
                            partSize : partFile.Length - skipBytes;

                // 创建UploadPartRequest,上传分块
                UploadPartRequest uploadPartRequest = new UploadPartRequest(ossConfig.BucketName, key, initResult.UploadId);
                uploadPartRequest.InputStream = fis;
                uploadPartRequest.PartSize    = size;
                uploadPartRequest.PartNumber  = (i + 1);
                UploadPartResult uploadPartResult = ossClient.UploadPart(uploadPartRequest);

                // 将返回的PartETag保存到List中。
                partETags.Add(uploadPartResult.PartETag);

                // 关闭文件
                fis.Close();
                Console.WriteLine("第{0}块,上传完毕", i + 1);
            }

            CompleteMultipartUploadRequest completeReq = new CompleteMultipartUploadRequest(ossConfig.BucketName, key, initResult.UploadId);

            foreach (PartETag partETag in partETags)
            {
                completeReq.PartETags.Add(partETag);
            }
            //  红色标注的是与JAVA的SDK有区别的地方

            //完成分块上传
            Console.WriteLine("合并数据块开始");
            CompleteMultipartUploadResult completeResult = ossClient.CompleteMultipartUpload(completeReq);

            Console.WriteLine("合并数据块结束");
            // 返回最终文件的MD5,用于用户进行校验

            Console.WriteLine(key + " 上传成功.");
        }
        public static void MultipartUploadProgress(string bucketName)
        {
            const string key = "MultipartUploadProgress";

            try
            {
                // 初始化分片上传任务
                var initRequest = new InitiateMultipartUploadRequest(bucketName, key);
                var initResult  = client.InitiateMultipartUpload(initRequest);

                // 设置每块为 1M
                const int partSize = 1024 * 1024 * 1;
                var       partFile = new FileInfo(Config.FileToUpload);
                // 计算分块数目
                var partCount = CalculatePartCount(partFile.Length, partSize);

                // 新建一个List保存每个分块上传后的ETag和PartNumber
                var partETags = new List <PartETag>();
                //upload the file
                using (var fs = new FileStream(partFile.FullName, FileMode.Open))
                {
                    for (var i = 0; i < partCount; i++)
                    {
                        // 跳到每个分块的开头
                        long skipBytes = partSize * i;
                        fs.Position = skipBytes;

                        // 计算每个分块的大小
                        var size = partSize < partFile.Length - skipBytes ? partSize : partFile.Length - skipBytes;

                        // 创建UploadPartRequest,上传分块
                        var uploadPartRequest = new UploadPartRequest(bucketName, key, initResult.UploadId)
                        {
                            InputStream = fs,
                            PartSize    = size,
                            PartNumber  = (i + 1)
                        };
                        var uploadPartResult = client.UploadPart(uploadPartRequest);

                        // 将返回的PartETag保存到List中。
                        partETags.Add(uploadPartResult.PartETag);
                    }
                }

                // 提交上传任务
                var metadata        = BuildCallbackMetadata(callbackUrl, callbackBody);
                var completeRequest = new CompleteMultipartUploadRequest(bucketName, key, initResult.UploadId)
                {
                    Metadata = metadata
                };
                foreach (var partETag in partETags)
                {
                    completeRequest.PartETags.Add(partETag);
                }
                var completeUploadResult = client.CompleteMultipartUpload(completeRequest);
                var responseContent      = GetCallbackResponse(completeUploadResult);

                Console.WriteLine("Multipart upload object:{0} succeeded, callback response content:{1}", key, responseContent);
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
Esempio n. 13
0
        public static void UploadBigFile(string accesskey, string secret, string endpoint, string bucketname, string filename)
        {
            string objName = Path.GetFileName(filename);
            // 创建OssClient实例。
            var client = new OssClient(endpoint, accesskey, secret);
            // 初始化分片上传。
            var uploadId = "";

            try
            {
                // 定义上传的文件及所属存储空间的名称。您可以在InitiateMultipartUploadRequest中设置ObjectMeta,但不必指定其中的ContentLength。
                var request = new InitiateMultipartUploadRequest(bucketname, objName);
                var result  = client.InitiateMultipartUpload(request);
                uploadId = result.UploadId;
                // 打印UploadId。
                Console.WriteLine("Upload Id:{0}", result.UploadId);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Init multipart upload failed:{ex.Message}");
            }
            // 计算分片总数。
            var partSize  = 100 * 1024;
            var fi        = new FileInfo(filename);
            var fileSize  = fi.Length;
            var partCount = fileSize / partSize;

            if (fileSize % partSize != 0)
            {
                partCount++;
            }
            // 开始分片上传。PartETags是保存PartETag的列表,OSS收到用户提交的分片列表后,会逐一验证每个分片数据的有效性。当所有的数据分片通过验证后,OSS会将这些分片组合成一个完整的文件。
            var partETags = new List <PartETag>();

            try
            {
                using (var fs = File.Open(filename, FileMode.Open))
                {
                    for (var i = 0; i < partCount; i++)
                    {
                        var skipBytes = (long)partSize * i;
                        fs.Seek(skipBytes, 0);
                        var size    = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
                        var request = new UploadPartRequest(bucketname, objName, uploadId)
                        {
                            InputStream = fs,
                            PartSize    = size,
                            PartNumber  = i + 1
                        };
                        request.StreamTransferProgress += streamProgressCallback;
                        var result = client.UploadPart(request);
                        partETags.Add(result.PartETag);
                        Console.WriteLine($"finish {partETags.Count}/{partCount}");
                    }
                    Console.WriteLine("MultipartUpload succeeded");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"MultipartUpload failed:{ex.Message}");
            }
            // 完成分片上传。
            try
            {
                var completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketname, objName, uploadId);
                foreach (var partETag in partETags)
                {
                    completeMultipartUploadRequest.PartETags.Add(partETag);
                }
                var result = client.CompleteMultipartUpload(completeMultipartUploadRequest);
                Console.WriteLine("complete Multipart succeeded");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"complete Multipart failed, {ex.Message}");
            }
        }
Esempio n. 14
0
        public void mutiPartUpload()
        {
            for (; ;)
            {
                getListItemName();

                if (string.IsNullOrEmpty(path))
                {
                    break;
                }

                key = Guid.NewGuid().ToString() + path.Substring(path.LastIndexOf('.'));

                OssClient ossClient = new OssClient(endPoint, accessKeyID, accessKeySecret);

                InitiateMultipartUploadRequest initRequest =
                    new InitiateMultipartUploadRequest(bucketName, key);
                InitiateMultipartUploadResult initResult = ossClient.InitiateMultipartUpload(initRequest);


                // 设置每块为 5M ,不允许小于5M
                int partSize = 1024 * 100;

                FileInfo partFile = new FileInfo(path);

                // 计算分块数目
                int partCount = (int)(partFile.Length / partSize);
                if (partFile.Length % partSize != 0)
                {
                    partCount++;
                }

                // 新建一个List保存每个分块上传后的ETag和PartNumber
                List <PartETag> partETags = new List <PartETag>();

                for (int i = 0; i < partCount; i++)
                {
                    Bar.Value = (i * 100) / partCount;
                    UploadInfo(Bar.Value.ToString());

                    // 获取文件流
                    FileStream fis = new FileStream(partFile.FullName, FileMode.Open);

                    // 跳到每个分块的开头
                    long skipBytes = partSize * i;
                    fis.Position = skipBytes;
                    //fis.skip(skipBytes);

                    // 计算每个分块的大小
                    long size = partSize < partFile.Length - skipBytes ?
                                partSize : partFile.Length - skipBytes;

                    // 创建UploadPartRequest,上传分块
                    UploadPartRequest uploadPartRequest = new UploadPartRequest(bucketName, key, initResult.UploadId);
                    uploadPartRequest.InputStream = fis;
                    uploadPartRequest.PartSize    = size;
                    uploadPartRequest.PartNumber  = (i + 1);
                    UploadPartResult uploadPartResult = ossClient.UploadPart(uploadPartRequest);

                    // 将返回的PartETag保存到List中。
                    partETags.Add(uploadPartResult.PartETag);

                    // 关闭文件
                    fis.Close();

                    manualReset.WaitOne();//如果ManualResetEvent的初始化为终止状态(true),那么该方法将一直工作,直到收到Reset信号。然后,直到收到Set信号,就继续工作。
                }

                CompleteMultipartUploadRequest completeReq = new CompleteMultipartUploadRequest(bucketName, key, initResult.UploadId);
                foreach (PartETag partETag in partETags)
                {
                    completeReq.PartETags.Add(partETag);
                }
                //  红色标注的是与JAVA的SDK有区别的地方

                //完成分块上传
                CompleteMultipartUploadResult completeResult = ossClient.CompleteMultipartUpload(completeReq);

                Bar.Value = 100;
                UploadInfo(Bar.Value.ToString());

                // 返回最终文件的MD5,用于用户进行校验
                //Console.WriteLine(completeResult.ETag);

                setListItemValue();
            }
        }
        /// <summary>
        /// 文件拷贝,默认采用分片拷贝的方式
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="objectName"></param>
        /// <param name="destBucketName"></param>
        /// <param name="destObjectName"></param>
        /// <returns></returns>
        public async Task <bool> CopyObjectAsync(string bucketName, string objectName, string destBucketName, string destObjectName = null)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            if (string.IsNullOrEmpty(objectName))
            {
                throw new ArgumentNullException(nameof(objectName));
            }
            if (string.IsNullOrEmpty(destBucketName))
            {
                destBucketName = bucketName;
            }
            if (string.IsNullOrEmpty(destObjectName))
            {
                destObjectName = objectName;
            }
            var partSize = 50 * 1024 * 1024;
            // 创建OssClient实例。
            // 初始化拷贝任务。可以通过InitiateMultipartUploadRequest指定目标文件元信息。
            var request = new InitiateMultipartUploadRequest(destBucketName, destObjectName);
            var result  = _client.InitiateMultipartUpload(request);
            // 计算分片数。
            var metadata  = _client.GetObjectMetadata(bucketName, objectName);
            var fileSize  = metadata.ContentLength;
            var partCount = (int)fileSize / partSize;

            if (fileSize % partSize != 0)
            {
                partCount++;
            }
            // 开始分片拷贝。
            var partETags = new List <PartETag>();

            for (var i = 0; i < partCount; i++)
            {
                var skipBytes = (long)partSize * i;
                var size      = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
                // 创建UploadPartCopyRequest。可以通过UploadPartCopyRequest指定限定条件。
                var uploadPartCopyRequest = new UploadPartCopyRequest(destBucketName, destObjectName, bucketName, objectName, result.UploadId)
                {
                    PartSize   = size,
                    PartNumber = i + 1,
                    // BeginIndex用来定位此次上传分片开始所对应的位置。
                    BeginIndex = skipBytes
                };
                // 调用uploadPartCopy方法来拷贝每一个分片。
                var uploadPartCopyResult = _client.UploadPartCopy(uploadPartCopyRequest);
                partETags.Add(uploadPartCopyResult.PartETag);
            }
            // 完成分片拷贝。
            var completeMultipartUploadRequest =
                new CompleteMultipartUploadRequest(destBucketName, destObjectName, result.UploadId);

            // partETags为分片上传中保存的partETag的列表,OSS收到用户提交的此列表后,会逐一验证每个数据分片的有效性。全部验证通过后,OSS会将这些分片合成一个完整的文件。
            foreach (var partETag in partETags)
            {
                completeMultipartUploadRequest.PartETags.Add(partETag);
            }
            _client.CompleteMultipartUpload(completeMultipartUploadRequest);
            return(await Task.FromResult(true));
        }