示例#1
0
        private static List <PartETag> UploadPartsWithCache
        (
            OssClient client,
            string bucketName,
            string objectName,
            Stream streamToUpload,
            string uploadId,
            int partSize,
            EventHandler <StreamTransferProgressArgs> streamTransferProgress,
            CancellationToken cancellationToken = default(CancellationToken)
        )
        {
            long fileSize = streamToUpload.Length;

            MultipartStreamProgressMonitor progressMonitor = null;

            if (streamTransferProgress != null)
            {
                progressMonitor = new MultipartStreamProgressMonitor();
                progressMonitor.StreamTransferProgress += streamTransferProgress;
                progressMonitor.TotalBytes              = fileSize;
#if DEBUG
                progressMonitor.ShowProgressOnConsole = OutputDebugInfo;
#endif
            }

            byte[] buffer    = new byte[partSize];
            var    partETags = new List <PartETag>();
            for (int i = 0; /* part count is unknown */; i++)
            {
                cancellationToken.ThrowIfCancellationRequested();

                using (var ms = new MemoryStream())
                {
                    int readSize = streamToUpload.Read(buffer, 0, partSize);
                    if (readSize == 0)
                    {
                        break;
                    }
                    string md5 = HashUtility.CalulateHash_MD5(buffer, 0, readSize);

                    Retry((_) =>
                    {
                        var request = new UploadPartRequest(bucketName, objectName, uploadId)
                        {
                            InputStream = new MemoryStream(buffer, 0, readSize),
                            Md5Digest   = md5,
                            PartSize    = readSize,
                            PartNumber  = i + 1
                        };
                        if (progressMonitor != null)
                        {
                            request.StreamTransferProgress += (s, e) => progressMonitor.PartStreamTransferProgress(i, e);
                        }
                        var result = client.UploadPart(request);
                        partETags.Add(result.PartETag);
                    },
                          cancellationToken: cancellationToken);

                    if (readSize < partSize)
                    {
                        break;
                    }
                }
#if DEBUG
                if (OutputDebugInfo)
                {
                    Console.WriteLine("finish {0} parts", partETags.Count);
                }
#endif
            }
            return(partETags);
        }
示例#2
0
        private static List <PartETag> UploadParts
        (
            OssClient client,
            string bucketName,
            string objectName,
            Stream streamToUpload,
            string uploadId,
            int partSize,
            EventHandler <StreamTransferProgressArgs> streamTransferProgress,
            CancellationToken cancellationToken = default(CancellationToken)
        )
        {
            long fileSize = streamToUpload.Length;

            MultipartStreamProgressMonitor progressMonitor = null;

            if (streamTransferProgress != null)
            {
                progressMonitor = new MultipartStreamProgressMonitor();
                progressMonitor.StreamTransferProgress += streamTransferProgress;
                progressMonitor.TotalBytes              = fileSize;
#if DEBUG
                progressMonitor.ShowProgressOnConsole = OutputDebugInfo;
#endif
            }

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

            var partETags = new List <PartETag>();
            for (var i = 0; i < partCount; i++)
            {
                cancellationToken.ThrowIfCancellationRequested();
                var skipBytes = (long)partSize * i;
                var size      = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);

                string md5 = HashUtility.CalulateHash_MD5(streamToUpload, skipBytes, size);

                Retry((_) =>
                {
                    streamToUpload.Seek(skipBytes, 0);
                    var request = new UploadPartRequest(bucketName, objectName, uploadId)
                    {
                        InputStream = streamToUpload,
                        Md5Digest   = md5,
                        PartSize    = size,
                        PartNumber  = i + 1
                    };
                    if (progressMonitor != null)
                    {
                        request.StreamTransferProgress += (s, e) => progressMonitor.PartStreamTransferProgress(i, e);
                    }
                    var result = client.UploadPart(request);
                    partETags.Add(result.PartETag);
                },
                      cancellationToken: cancellationToken);
#if DEBUG
                if (OutputDebugInfo)
                {
                    Console.WriteLine("finish {0}/{1}", partETags.Count, partCount);
                }
#endif
            }
            return(partETags);
        }