Ejemplo n.º 1
0
        public static string CalulateHash_MD5(Stream stream, long skipBytes, long size)
        {
            string md5 = null;

            if (stream is FileStream)
            {
                using (MD5 md5Hash = MD5.Create())
                    using (var md5stream = new PartialFileStream(((FileStream)stream).Name, FileMode.Open, FileAccess.Read, FileShare.Read, skipBytes, size))
                    {
                        md5 = Convert.ToBase64String(md5Hash.ComputeHash(md5stream));
                    }
            }
            else
            {
                using (MD5 md5Hash = MD5.Create())
                    using (var md5stream = new PartialStream(stream, skipBytes, size))
                    {
                        md5 = Convert.ToBase64String(md5Hash.ComputeHash(md5stream));
                    }
            }
            return(md5);
        }
Ejemplo n.º 2
0
        private static List <PartETag> UploadParts
        (
            OssClient client,
            string bucketName,
            string objectName,
            string fileToUpload,
            string uploadId,
            int partSize,
            EventHandler <StreamTransferProgressArgs> streamTransferProgress,
            CancellationToken cancellationToken = default(CancellationToken)
        )
        {
            var  fileSize = new FileInfo(fileToUpload).Length;
            long progressUpdateInterval = (fileSize / 100 + 4096) / 4096 * 4096;

            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>();
            using (var fs = File.Open(fileToUpload, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                for (var i = 0; i < partCount; i++)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    var skipBytes = (long)partSize * i;
                    var size      = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);

                    string md5 = null;
                    using (MD5 md5Hash = MD5.Create())
                        using (var md5stream = new PartialFileStream(fileToUpload, FileMode.Open, FileAccess.Read, FileShare.Read, skipBytes, size))
                        {
                            md5 = Convert.ToBase64String(md5Hash.ComputeHash(md5stream));
                        }

                    Retry((_) =>
                    {
                        fs.Seek(skipBytes, 0);
                        var request = new UploadPartRequest(bucketName, objectName, uploadId)
                        {
                            InputStream = fs,
                            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);
        }