public static UploadPartCommand Create(IServiceClient client, Uri endpoint, ExecutionContext context,
                                               UploadPartRequest uploadPartRequest)
        {
            OssUtils.CheckBucketName(uploadPartRequest.BucketName);
            OssUtils.CheckObjectKey(uploadPartRequest.Key);

            if (string.IsNullOrEmpty(uploadPartRequest.UploadId))
            {
                throw new ArgumentException("uploadId should be specified");
            }
            if (!uploadPartRequest.PartNumber.HasValue)
            {
                throw new ArgumentException("partNumber should be specified");
            }
            if (!uploadPartRequest.PartSize.HasValue)
            {
                throw new ArgumentException("partSize should be specified");
            }
            if (uploadPartRequest.InputStream == null)
            {
                throw new ArgumentException("inputStream should be specified");
            }

            if (uploadPartRequest.PartSize < 0 || uploadPartRequest.PartSize > OssUtils.MaxFileSize)
            {
                throw new ArgumentException("partSize not live in valid range");
            }
            if (!OssUtils.IsPartNumberInRange(uploadPartRequest.PartNumber))
            {
                throw new ArgumentException("partNumber not live in valid range");
            }

            var conf           = OssUtils.GetClientConfiguration(client);
            var originalStream = uploadPartRequest.InputStream;
            var streamLength   = uploadPartRequest.PartSize.Value;

            // wrap input stream in PartialWrapperStream
            originalStream = new PartialWrapperStream(originalStream, streamLength);

            // setup progress
            var callback = uploadPartRequest.StreamTransferProgress;

            if (callback != null)
            {
                originalStream = OssUtils.SetupProgressListeners(originalStream, conf.ProgressUpdateInterval, client, callback);
                uploadPartRequest.InputStream = originalStream;
            }

            // wrap input stream in MD5Stream
            if (conf.EnalbeMD5Check)
            {
                var hashStream = new MD5Stream(originalStream, null, streamLength);
                uploadPartRequest.InputStream = hashStream;
                context.ResponseHandlers.Add(new MD5DigestCheckHandler(hashStream));
            }

            return(new UploadPartCommand(client, endpoint, context,
                                         DeserializerFactory.GetFactory().CreateUploadPartResultDeserializer(uploadPartRequest.PartNumber.Value),
                                         uploadPartRequest));
        }
示例#2
0
        public override OssObject Deserialize(ServiceResponse xmlStream)
        {
            OssObject ossObject = new OssObject(_getObjectRequest.Key)
            {
                BucketName     = _getObjectRequest.BucketName,
                ResponseStream = xmlStream.Content,
                Metadata       = DeserializerFactory.GetFactory()
                                 .CreateGetObjectMetadataResultDeserializer().Deserialize(xmlStream)
            };

            DeserializeGeneric(xmlStream, ossObject);

            var conf           = OssUtils.GetClientConfiguration(_serviceClient);
            var originalStream = ossObject.ResponseStream;
            var streamLength   = ossObject.ContentLength;

            // setup progress
            var callback = _getObjectRequest.StreamTransferProgress;

            if (callback != null)
            {
                originalStream           = OssUtils.SetupProgressListeners(originalStream, streamLength, conf.ProgressUpdateInterval, _serviceClient, callback);
                ossObject.ResponseStream = originalStream;
            }

            // wrap response stream in MD5Stream
            if (conf.EnalbeMD5Check)
            {
                byte[] expectedHashDigest = null;
                if (xmlStream.Headers.ContainsKey(HttpHeaders.ContentMd5))
                {
                    var md5OfResponse = xmlStream.Headers[HttpHeaders.ContentMd5];
                    expectedHashDigest = Convert.FromBase64String(md5OfResponse);
                }
                var hashStream = new MD5Stream(originalStream, expectedHashDigest, streamLength);
                ossObject.ResponseStream = hashStream;
            }
            else if (conf.EnableCrcCheck && _getObjectRequest.Range == null && string.IsNullOrEmpty(_getObjectRequest.Process))
            {
                byte[] expectedHashDigest = null;
                if (xmlStream.Headers.ContainsKey(HttpHeaders.HashCrc64Ecma))
                {
                    var   crcString = xmlStream.Headers[HttpHeaders.HashCrc64Ecma];
                    ulong crcVal;
                    if (ulong.TryParse(crcString, out crcVal))
                    {
                        expectedHashDigest = BitConverter.GetBytes(crcVal);
                        var hashStream = new Crc64Stream(originalStream, expectedHashDigest, streamLength);
                        ossObject.ResponseStream = hashStream;
                    }
                }
            }

            return(ossObject);
        }
        public static PutObjectCommand Create(IServiceClient client, Uri endpoint,
                                              ExecutionContext context,
                                              PutObjectRequest putObjectRequest)
        {
            OssUtils.CheckBucketName(putObjectRequest.BucketName);
            OssUtils.CheckObjectKey(putObjectRequest.Key);

            if (putObjectRequest.Content == null)
            {
                throw new ArgumentNullException("content");
            }

            // handle upload callback error 203
            if (putObjectRequest.IsCallbackRequest())
            {
                context.ResponseHandlers.Add(new CallbackResponseHandler());
            }

            var conf           = OssUtils.GetClientConfiguration(client);
            var originalStream = putObjectRequest.Content;

            // setup progress
            var callback = putObjectRequest.StreamTransferProgress;

            if (callback != null)
            {
                originalStream           = OssUtils.SetupProgressListeners(originalStream, conf.ProgressUpdateInterval, client, callback);
                putObjectRequest.Content = originalStream;
            }

            // wrap input stream in MD5Stream
            if (conf.EnalbeMD5Check)
            {
                var streamLength = originalStream.CanSeek ? originalStream.Length : -1;
                var hashStream   = new MD5Stream(originalStream, null, streamLength);
                putObjectRequest.Content = hashStream;
                context.ResponseHandlers.Add(new MD5DigestCheckHandler(hashStream));
            }
            else if (conf.EnableCrcCheck)
            {
                var streamLength = originalStream.CanSeek ? originalStream.Length : -1;
                var hashStream   = new Crc64Stream(originalStream, null, streamLength);
                putObjectRequest.Content = hashStream;
                context.ResponseHandlers.Add(new Crc64CheckHandler(hashStream));
            }

            return(new PutObjectCommand(client, endpoint, context,
                                        DeserializerFactory.GetFactory().CreatePutObjectReusltDeserializer(putObjectRequest),
                                        putObjectRequest));
        }
        public static AppendObjectCommand Create(IServiceClient client, Uri endpoint, ExecutionContext context,
                                                 AppendObjectRequest request)
        {
            OssUtils.CheckBucketName(request.BucketName);
            OssUtils.CheckObjectKey(request.Key);

            if (request.Content == null)
            {
                throw new ArgumentNullException("request.Content");
            }

            request.ObjectMetadata = request.ObjectMetadata ?? new ObjectMetadata();
            if (request.ObjectMetadata.ContentType == null)
            {
                request.ObjectMetadata.ContentType = HttpUtils.GetContentType(request.Key, null);
            }

            var conf           = OssUtils.GetClientConfiguration(client);
            var originalStream = request.Content;
            var streamLength   = request.Content.Length;

            // setup progress
            var callback = request.StreamTransferProgress;

            if (callback != null)
            {
                originalStream  = OssUtils.SetupProgressListeners(originalStream, conf.ProgressUpdateInterval, client, callback);
                request.Content = originalStream;
            }

            // wrap input stream in MD5Stream
            if (conf.EnalbeMD5Check)
            {
                var hashStream = new MD5Stream(originalStream, null, streamLength);
                request.Content = hashStream;
                context.ResponseHandlers.Add(new MD5DigestCheckHandler(hashStream));
            }
            else if (conf.EnableCrcCheck && request.InitCrc != null)
            {
                var hashStream = new Crc64Stream(originalStream, null, streamLength, request.InitCrc.Value);
                request.Content = hashStream;
                context.ResponseHandlers.Add(new Crc64CheckHandler(hashStream));
            }

            return(new AppendObjectCommand(client, endpoint, context,
                                           DeserializerFactory.GetFactory().CreateAppendObjectReusltDeserializer(),
                                           request));
        }