public static PutObjectResult SimpleUpload ( IOssClientBuilder clientBuilder, // Ensure OssClient is created with MD5 check option. string bucketName, string objectName, Stream streamToUpload, EventHandler <StreamTransferProgressArgs> streamTransferProgress = null, CancellationToken cancellationToken = default(CancellationToken) ) { if (clientBuilder == null) { throw new ArgumentNullException(nameof(clientBuilder)); } var client = clientBuilder.Build(o => o.EnalbeMD5Check = true); return(Retry((_) => client.PutObject(new PutObjectRequest(bucketName, objectName, streamToUpload) { StreamTransferProgress = (s, e) => { cancellationToken.ThrowIfCancellationRequested(); streamTransferProgress?.Invoke(s, e); } }) )); }
public static CompleteMultipartUploadResult UploadMultipart ( IOssClientBuilder clientBuilder, string bucketName, string objectName, string fileToUpload, int partSize = DEFAULT_PART_SIZE, EventHandler <StreamTransferProgressArgs> streamTransferProgress = null, CancellationToken cancellationToken = default(CancellationToken) ) { CompleteMultipartUploadResult retVal = null; if (clientBuilder == null) { throw new ArgumentNullException(nameof(clientBuilder)); } long fileSize = new FileInfo(fileToUpload).Length; long progressUpdateInterval = (fileSize / 100 + 4096) / 4096 * 4096; var client = clientBuilder.Build(o => { o.ProgressUpdateInterval = progressUpdateInterval; }); var uploadId = InitiateMultipartUpload(client, bucketName, objectName); using (var cleaner = new MultipartCleaner(client, bucketName, objectName, uploadId)) { var partETags = UploadParts(client, bucketName, objectName, fileToUpload, uploadId, partSize, (s, e) => { cancellationToken.ThrowIfCancellationRequested(); streamTransferProgress?.Invoke(s, e); }); retVal = CompleteUploadPart(client, bucketName, objectName, uploadId, partETags); cleaner.Complete(); } #if DEBUG if (OutputDebugInfo) { Console.WriteLine("Multipart put object:{0} succeeded", objectName); } #endif return(retVal); }
public static AppendUploadResult AppendUpload ( IOssClientBuilder clientBuilder, string bucketName, string objectName, Stream streamToUpload, Action <AppendUploadOptions> options = null, EventHandler <StreamTransferProgressArgs> streamTransferProgress = null, CancellationToken cancellationToken = default(CancellationToken) ) { if (clientBuilder == null) { throw new ArgumentNullException(nameof(clientBuilder)); } if (streamToUpload == null) { throw new ArgumentNullException(nameof(streamToUpload)); } AppendUploadOptions uploadOptions = new AppendUploadOptions(); options?.Invoke(uploadOptions); int blockSize = Math.Max(MIN_BLOCK_SIZE, uploadOptions.InitialBlockSize); var client = clientBuilder.Build(o => o.EnableCrcCheck = true); long position = 0; ObjectMetadata metadata = null; try { metadata = client.GetObjectMetadata(bucketName, objectName); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception) #pragma warning restore CA1031 // Do not catch general exception types { // No such object } if (metadata != null) { if (string.Compare(metadata.ObjectType, "Appendable", StringComparison.InvariantCultureIgnoreCase) == 0) { if (metadata.ContentLength > 0) { if (metadata.ContentLength <= streamToUpload.Length) { if (uploadOptions.AllowResume) { position = metadata.ContentLength; if (uploadOptions.VerifyBeforeResume) { using (var stream = new PartialStream(streamToUpload, 0, position)) using (var crc64 = new Crc64HashAlgorithm()) { crc64.ComputeHash(stream); string localHash = BitConverter.ToUInt64(crc64.Hash, 0).ToString(CultureInfo.InvariantCulture); if (string.Compare(metadata.Crc64, localHash, StringComparison.InvariantCultureIgnoreCase) != 0) { if (uploadOptions.AllowOverwrite) { client.DeleteObject(bucketName, objectName); position = 0; } else { throw new OssException("Hash mismatched, could not resume file"); } } } } } else if (uploadOptions.AllowOverwrite) { client.DeleteObject(bucketName, objectName); } else { throw new OssException("Could not resume or overwrite file"); } } else { if (uploadOptions.AllowOverwrite) { client.DeleteObject(bucketName, objectName); } else { throw new OssException("Could not resume, the length of remote object is longer than local object."); } } } } else if (uploadOptions.AllowOverwrite) { client.DeleteObject(bucketName, objectName); } else { throw new OssException("The object is not appendable"); } } if (streamToUpload.Length == position) { return(new AppendUploadResult { ETag = metadata.ETag, Crc64 = metadata.Crc64, ContentLength = metadata.ContentLength }); } while (true) { var result = Retry((retryCount) => { if (retryCount > 0) { blockSize = Math.Max(MIN_BLOCK_SIZE, blockSize >> 1); } var length = Math.Min(blockSize, streamToUpload.Length - position); var request = new AppendObjectRequest(bucketName, objectName) { Content = new PartialStream(streamToUpload, position, length), Position = position, StreamTransferProgress = (s, e) => { cancellationToken.ThrowIfCancellationRequested(); if (streamTransferProgress != null) { streamTransferProgress.Invoke(s, new StreamTransferProgressArgs(e.IncrementTransferred, position + e.TransferredBytes, streamToUpload.Length)); } } }; return(client.AppendObject(request)); }, cancellationToken: cancellationToken); position = result.NextAppendPosition; if (position >= streamToUpload.Length) { return(new AppendUploadResult { ETag = result.ETag, Crc64 = result.HashCrc64Ecma.ToString(CultureInfo.InvariantCulture), ContentLength = result.NextAppendPosition, AppendObjectResult = result }); } } }
public static CompleteMultipartUploadResult UploadMultipart ( IOssClientBuilder clientBuilder, string bucketName, string objectName, Stream streamToUpload, Action <MultipartUploadOptions> options = null, EventHandler <StreamTransferProgressArgs> streamTransferProgress = null, CancellationToken cancellationToken = default(CancellationToken) ) { CompleteMultipartUploadResult retVal = null; if (clientBuilder == null) { throw new ArgumentNullException(nameof(clientBuilder)); } if (streamToUpload == null) { throw new ArgumentNullException(nameof(streamToUpload)); } MultipartUploadOptions uploadOptions = new MultipartUploadOptions(); options?.Invoke(uploadOptions); int partSize = Math.Max(DEFAULT_PART_SIZE, uploadOptions.PartSize); var streamMode = uploadOptions.StreamMode; long fileSize = streamToUpload.Length; long progressUpdateInterval = (fileSize / 100 + 4096) / 4096 * 4096; if (streamMode == MultipartStreamMode.Auto || streamMode == MultipartStreamMode.PartialFileStream) { if (streamToUpload is FileStream && streamToUpload.CanSeek && streamToUpload.CanRead) { streamMode = MultipartStreamMode.PartialFileStream; } else if (streamToUpload.CanRead) { streamMode = MultipartStreamMode.SequenceInputStream; } } if (streamMode == MultipartStreamMode.PartialFileStream) { var client = clientBuilder.Build(o => { o.ProgressUpdateInterval = progressUpdateInterval; }); var uploadId = InitiateMultipartUpload(client, bucketName, objectName); using (var cleaner = new MultipartCleaner(client, bucketName, objectName, uploadId)) { var partETags = UploadParts(client, bucketName, objectName, streamToUpload, uploadId, partSize, (s, e) => { cancellationToken.ThrowIfCancellationRequested(); streamTransferProgress?.Invoke(s, e); }); retVal = CompleteUploadPart(client, bucketName, objectName, uploadId, partETags); cleaner.Complete(); } } else if (streamMode == MultipartStreamMode.SequenceInputStream) { var client = clientBuilder.Build(o => { o.ProgressUpdateInterval = progressUpdateInterval; }); var uploadId = InitiateMultipartUpload(client, bucketName, objectName); using (var cleaner = new MultipartCleaner(client, bucketName, objectName, uploadId)) { var partETags = UploadPartsWithCache(client, bucketName, objectName, streamToUpload, uploadId, partSize, (s, e) => { cancellationToken.ThrowIfCancellationRequested(); streamTransferProgress?.Invoke(s, e); }); retVal = CompleteUploadPart(client, bucketName, objectName, uploadId, partETags); cleaner.Complete(); } } else { throw new ArgumentException("Stream Type is not supported", nameof(streamToUpload)); } #if DEBUG if (OutputDebugInfo) { Console.WriteLine("Multipart put object:{0} succeeded", objectName); } #endif return(retVal); }