public Stream GetNonWrapperBaseStream() { Stream stream = this; do { PartialWrapperStream partialWrapperStream = stream as PartialWrapperStream; if (partialWrapperStream != null) { return(partialWrapperStream); } stream = (stream as WrapperStream).BaseStream; }while (stream is WrapperStream); return(stream); }
static List<byte[]> computePartHashs(Stream stream) { long initialPosition = stream.Position; List<byte[]> hashes = new List<byte[]>(); while (stream.Position < stream.Length) { Stream wrapper = new PartialWrapperStream(stream, MB); byte[] currentHash = CryptoUtilFactory.CryptoInstance.ComputeSHA256Hash(wrapper); hashes.Add(currentHash); } stream.Seek(initialPosition, SeekOrigin.Begin); return hashes; }
internal override void Execute() { FileInfo fileInfo = new FileInfo(filePath); FileStream fileStream = File.OpenRead(filePath); string uploadId = null; try { this.currentUploadProgressArgs = new StreamTransferProgressArgs(0, 0, fileInfo.Length); long partSize = CalculatePartSize(fileInfo.Length); InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest() { AccountId = this.options.AccountId, ArchiveDescription = archiveDescription, VaultName = vaultName, PartSize = partSize }; ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)initiateRequest).AddBeforeRequestHandler(new ArchiveTransferManager.UserAgentPostFix("MultiUpload").UserAgentRequestEventHandlerSync); InitiateMultipartUploadResponse initiateResponse = this.manager.GlacierClient.InitiateMultipartUpload(initiateRequest); uploadId = initiateResponse.UploadId; List<string> partTreeHashs = new List<string>(); long currentPosition = 0; while (currentPosition < fileInfo.Length) { long length = partSize; if (currentPosition + partSize > fileInfo.Length) { length = fileInfo.Length - currentPosition; } Stream partStream = new PartialWrapperStream(fileStream, length); string checksum = TreeHashGenerator.CalculateTreeHash(partStream); partTreeHashs.Add(checksum); UploadMultipartPartRequest uploadRequest = new UploadMultipartPartRequest() { AccountId = this.options.AccountId, Checksum = checksum, Range = "bytes " + currentPosition + "-" + (currentPosition + length - 1) + "/*", UploadId = uploadId, VaultName = vaultName, Body = partStream }; uploadRequest.StreamTransferProgress += this.ProgressCallback; ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)uploadRequest).AddBeforeRequestHandler(new ArchiveTransferManager.UserAgentPostFix("MultiUpload").UserAgentRequestEventHandlerSync); this.manager.GlacierClient.UploadMultipartPart(uploadRequest); currentPosition += partSize; } string totalFileChecksum = TreeHashGenerator.CalculateTreeHash(partTreeHashs); string archiveSize = fileInfo.Length.ToString(CultureInfo.InvariantCulture); CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest() { AccountId = this.options.AccountId, ArchiveSize = archiveSize, VaultName = vaultName, Checksum = totalFileChecksum, UploadId = uploadId }; ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)compRequest).AddBeforeRequestHandler(new ArchiveTransferManager.UserAgentPostFix("MultiUpload").UserAgentRequestEventHandlerSync); CompleteMultipartUploadResponse completeMultipartUploadResponse = this.manager.GlacierClient.CompleteMultipartUpload(compRequest); string archiveId = completeMultipartUploadResponse.ArchiveId; this.UploadResult = new UploadResult(archiveId, totalFileChecksum); } catch (Exception) { // If we got an unrecoverable then abort the upload. if (!string.IsNullOrEmpty(uploadId)) { AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest() { AccountId = this.options.AccountId, VaultName = this.vaultName, UploadId = uploadId }; ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)abortRequest).AddBeforeRequestHandler(new ArchiveTransferManager.UserAgentPostFix("MultiUpload").UserAgentRequestEventHandlerSync); this.manager.GlacierClient.AbortMultipartUpload(abortRequest); } throw; } finally { try { fileStream.Close(); } catch (Exception) { } } }