Ejemplo n.º 1
0
        public bool uploadFile(String filePath, String bucketName, String path, System.Windows.Forms.ToolStripProgressBar prog)
        {
            Stream fileStream = null;

            try
            {
                prog.Value = 0;
                FileInfo fileInfo  = new FileInfo(filePath);
                String   objectKey = path + fileInfo.Name;
                // 开始Multipart Upload
                InitiateMultipartUploadRequest initiateMultipartUploadRequest =
                    new InitiateMultipartUploadRequest()
                {
                    BucketName = bucketName, Key = objectKey
                };
                BosClient client = GenerateBosClient();
                InitiateMultipartUploadResponse initiateMultipartUploadResponse = client.InitiateMultipartUpload(initiateMultipartUploadRequest);

                String fileName  = fileInfo.Name;
                long   totalSize = fileInfo.Length;
                prog.Maximum = (int)totalSize;

                long partSize = 1024 * 1024 * 20L;
                if (totalSize < this.comparisonPartSize)
                {
                    partSize = 1024 * 1024 * 1L;
                }
                int partCount = (int)(totalSize / partSize);

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

                // byte[] by = new byte[partSize];
                List <PartETag> partETags = new List <PartETag>();


                for (int i = 0; i < partCount; i++)
                {
                    // 获取文件流
                    fileStream = fileInfo.OpenRead();
                    // 计算每个分片跳过字节数
                    long skipBytes = partSize * i;
                    // 计算每个分块的大小
                    long   size   = Math.Min(partSize, fileInfo.Length - skipBytes);
                    byte[] buffer = new byte[size];
                    //跳到上次读取的开头
                    fileStream.Seek(skipBytes, SeekOrigin.Begin);
                    // fileStream.ReadAsync(buffer, 0, (int)size);

                    //分片的流
                    // MemoryStream ms = new MemoryStream(buffer);

                    // 创建UploadPartRequest,上传分块
                    UploadPartRequest uploadPartRequest = new UploadPartRequest();
                    uploadPartRequest.BucketName  = bucketName;
                    uploadPartRequest.Key         = path + fileInfo.Name;
                    uploadPartRequest.UploadId    = initiateMultipartUploadResponse.UploadId;
                    uploadPartRequest.InputStream = fileStream;
                    uploadPartRequest.PartSize    = size;
                    uploadPartRequest.PartNumber  = i + 1;
                    UploadPartResponse uploadPartResponse = client.UploadPart(uploadPartRequest);
                    String             eTag = uploadPartResponse.ETag;
                    while (eTag == null)
                    {
                        eTag = uploadPartResponse.ETag;
                    }
                    //标识上传成功

                    // 将返回的PartETag保存到List中。
                    partETags.Add(new PartETag()
                    {
                        ETag       = uploadPartResponse.ETag,
                        PartNumber = uploadPartResponse.PartNumber
                    });
                    System.Windows.Forms.Application.DoEvents();
                    if (prog != null)
                    {
                        prog.Value = prog.Value + (int)size;
                    }
                    fileStream.Close();
                }
                // 获取UploadId的所有Upload Part
                ListPartsRequest listPartsRequest = new ListPartsRequest()
                {
                    BucketName = bucketName,
                    Key        = objectKey,
                    UploadId   = initiateMultipartUploadResponse.UploadId,
                };

                // 完成分块上传
                CompleteMultipartUploadRequest completeMultipartUploadRequest =
                    new CompleteMultipartUploadRequest()
                {
                    BucketName = bucketName,
                    Key        = objectKey,
                    UploadId   = initiateMultipartUploadResponse.UploadId,
                    PartETags  = partETags
                };
                CompleteMultipartUploadResponse completeMultipartUploadResponse =
                    client.CompleteMultipartUpload(completeMultipartUploadRequest);
                return(true);
            }
            catch (BceServiceException)
            {
                throw;
            }
            catch (BceClientException)
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                {
                    // 关闭文件
                    fileStream.Close();
                    fileStream = null;
                }
            }
        }
Ejemplo n.º 2
0
        private static void Main(string[] args)
        {
            BosClient    client     = SampleUtil.GetSampleBosClient();
            const string bucketName = "sample-bucket-multi-upload1"; //示例Bucket名称

            // 初始化:创建示例Bucket
            client.CreateBucket(bucketName); //指定Bucket名称
            string objectKey = "sample";

            // 1.开始Multipart Upload
            InitiateMultipartUploadRequest initiateMultipartUploadRequest =
                new InitiateMultipartUploadRequest()
            {
                BucketName = bucketName, Key = objectKey
            };
            InitiateMultipartUploadResponse initiateMultipartUploadResponse =
                client.InitiateMultipartUpload(initiateMultipartUploadRequest);

            // 2.获取Bucket内的Multipart Upload
            ListMultipartUploadsRequest listMultipartUploadsRequest =
                new ListMultipartUploadsRequest()
            {
                BucketName = bucketName
            };
            ListMultipartUploadsResponse listMultipartUploadsResponse =
                client.ListMultipartUploads(listMultipartUploadsRequest);

            foreach (MultipartUploadSummary multipartUpload in listMultipartUploadsResponse.Uploads)
            {
                Console.WriteLine("Key: " + multipartUpload.Key + " UploadId: " + multipartUpload.UploadId);
            }

            // 3.分块上传,首先设置每块为 5Mb
            long     partSize = 1024 * 1024 * 5L;
            FileInfo partFile = new FileInfo("d:\\lzb\\sample");
            // 计算分块数目
            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++)
            {
                // 获取文件流
                Stream stream = partFile.OpenRead();
                // 跳到每个分块的开头
                long skipBytes = partSize * i;
                stream.Seek(skipBytes, SeekOrigin.Begin);
                // 计算每个分块的大小
                long size = Math.Min(partSize, partFile.Length - skipBytes);
                // 创建UploadPartRequest,上传分块
                UploadPartRequest uploadPartRequest = new UploadPartRequest();
                uploadPartRequest.BucketName  = bucketName;
                uploadPartRequest.Key         = objectKey;
                uploadPartRequest.UploadId    = initiateMultipartUploadResponse.UploadId;
                uploadPartRequest.InputStream = stream;
                uploadPartRequest.PartSize    = size;
                uploadPartRequest.PartNumber  = i + 1;
                UploadPartResponse uploadPartResponse = client.UploadPart(uploadPartRequest);
                // 将返回的PartETag保存到List中。
                partETags.Add(new PartETag()
                {
                    ETag       = uploadPartResponse.ETag,
                    PartNumber = uploadPartResponse.PartNumber
                });
                // 关闭文件
                stream.Close();
            }

            // 4. 获取UploadId的所有Upload Part
            ListPartsRequest listPartsRequest = new ListPartsRequest()
            {
                BucketName = bucketName,
                Key        = objectKey,
                UploadId   = initiateMultipartUploadResponse.UploadId,
            };
            // 获取上传的所有Part信息
            ListPartsResponse listPartsResponse = client.ListParts(listPartsRequest);

            // 遍历所有Part
            foreach (PartSummary part in listPartsResponse.Parts)
            {
                Console.WriteLine("PartNumber: " + part.PartNumber + " ETag: " + part.ETag);
            }

            // 5. 完成分块上传
            CompleteMultipartUploadRequest completeMultipartUploadRequest =
                new CompleteMultipartUploadRequest()
            {
                BucketName = bucketName,
                Key        = objectKey,
                UploadId   = initiateMultipartUploadResponse.UploadId,
                PartETags  = partETags
            };
            CompleteMultipartUploadResponse completeMultipartUploadResponse =
                client.CompleteMultipartUpload(completeMultipartUploadRequest);

            Console.WriteLine(completeMultipartUploadResponse.ETag);
            Console.ReadKey();
        }
Ejemplo n.º 3
0
        public static bool UploadFile(this BosClient bosClient, BOSMultipartUploadRequest bosUploadRequest, IProgress <int> onProgressPercentChanged)
        {
            Debug.Assert(bosUploadRequest.RequestInfo.PartSize > 0);
            var filePath = bosUploadRequest.RequestInfo.FilePath;

            if (!File.Exists(filePath))
            {
                return(false);
            }

            var fileInfo = new FileInfo(filePath);

            long partSize = bosUploadRequest.RequestInfo.PartSize;

            int parts = (int)(fileInfo.Length / partSize);

            if (fileInfo.Length % partSize != 0)
            {
                parts += 1;
            }

            if (bosUploadRequest.RequestInfo.PartETags == null)
            {
                bosUploadRequest.RequestInfo.PartETags = new List <PartETag>();
            }

            if (string.IsNullOrEmpty(bosUploadRequest.RequestInfo.UploadId))
            {
                var initiateMultipartUploadRequest = new InitiateMultipartUploadRequest()
                {
                    BucketName = bosUploadRequest.RequestInfo.Bucket,
                    Key        = bosUploadRequest.RequestInfo.ObjectKey
                };
                var initiateMultipartUploadResponse = bosClient.InitiateMultipartUpload(initiateMultipartUploadRequest);

                bosUploadRequest.RequestInfo.UploadId = initiateMultipartUploadResponse.UploadId;
            }

            var uploadId = bosUploadRequest.RequestInfo.UploadId;

            for (int i = bosUploadRequest.RequestInfo.LastPartNumber; i < parts; ++i)
            {
                if (bosUploadRequest.PauseCancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                if (bosUploadRequest.AbortCancellationToken.IsCancellationRequested)
                {
                    var abortMultipartUploadRequest = new AbortMultipartUploadRequest()
                    {
                        BucketName = bosUploadRequest.RequestInfo.Bucket,
                        Key        = bosUploadRequest.RequestInfo.ObjectKey,
                        UploadId   = uploadId,
                    };
                    bosClient.AbortMultipartUpload(abortMultipartUploadRequest);

                    return(false);
                }

                using (var stream = fileInfo.OpenRead())
                {
                    var skipBytes = partSize * i;
                    stream.Seek(skipBytes, SeekOrigin.Begin);

                    onProgressPercentChanged?.Report((int)(((double)skipBytes / fileInfo.Length) * 100));

                    var actualPartSize = Math.Min(partSize, fileInfo.Length - skipBytes);

                    var uploadPartRequest = new UploadPartRequest();
                    uploadPartRequest.BucketName  = bosUploadRequest.RequestInfo.Bucket;
                    uploadPartRequest.Key         = bosUploadRequest.RequestInfo.ObjectKey;
                    uploadPartRequest.UploadId    = uploadId;
                    uploadPartRequest.InputStream = stream;
                    uploadPartRequest.PartSize    = actualPartSize;
                    uploadPartRequest.PartNumber  = i + 1;

                    var uploadPartResponse = bosClient.UploadPart(uploadPartRequest);

                    bosUploadRequest.RequestInfo.PartETags.Add(
                        new PartETag()
                    {
                        ETag       = uploadPartResponse.ETag,
                        PartNumber = uploadPartResponse.PartNumber
                    });

                    bosUploadRequest.RequestInfo.LastPartNumber = uploadPartResponse.PartNumber;
                }
            }

            var completeMultipartUploadRequest =
                new CompleteMultipartUploadRequest()
            {
                BucketName = bosUploadRequest.RequestInfo.Bucket,
                Key        = bosUploadRequest.RequestInfo.ObjectKey,
                UploadId   = uploadId,
                PartETags  = bosUploadRequest.RequestInfo.PartETags
            };

            var completeMultipartUploadResponse = bosClient.CompleteMultipartUpload(completeMultipartUploadRequest);

            onProgressPercentChanged?.Report(100);

            return(completeMultipartUploadResponse != null);
        }