Пример #1
0
        private ServiceRequest BuildRequest()
        {
            var conf    = OssUtils.GetClientConfiguration(Client);
            var request = new ServiceRequest
            {
                Method       = Method,
                Endpoint     = OssUtils.MakeBucketEndpoint(Endpoint, Bucket, conf),
                ResourcePath = OssUtils.MakeResourcePath(Key)
            };

            foreach (var p in Parameters)
            {
                request.Parameters.Add(p.Key, p.Value);
            }

            var adjustedTime = DateTime.UtcNow.AddSeconds(conf.TickOffset);

            request.Headers[HttpHeaders.Date] = DateUtils.FormatRfc822Date(adjustedTime);
            if (!Headers.ContainsKey(HttpHeaders.ContentType))
            {
                request.Headers[HttpHeaders.ContentType] = string.Empty;
            }
            foreach (var h in Headers)
            {
                request.Headers.Add(h.Key, h.Value);
            }
            if (Context.Credentials.UseToken)
            {
                request.Headers[HttpHeaders.SecurityToken] = Context.Credentials.SecurityToken;
            }

            request.Content = Content;

            return(request);
        }
        public static CompleteMultipartUploadCommand Create(IServiceClient client, Uri endpoint, ExecutionContext context,
                                                            CompleteMultipartUploadRequest completeMultipartUploadRequest)
        {
            OssUtils.CheckBucketName(completeMultipartUploadRequest.BucketName);
            OssUtils.CheckObjectKey(completeMultipartUploadRequest.Key);

            if (string.IsNullOrEmpty(completeMultipartUploadRequest.UploadId))
            {
                throw new ArgumentException("The parameter is empty or null.", "uploadId");
            }

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

            var conf = OssUtils.GetClientConfiguration(client);

            if (conf.EnableCrcCheck)
            {
                context.ResponseHandlers.Add(new CompleteMultipartUploadCrc64Handler(completeMultipartUploadRequest));
            }

            return(new CompleteMultipartUploadCommand(client, endpoint, context,
                                                      DeserializerFactory.GetFactory().CreateCompleteUploadResultDeserializer(completeMultipartUploadRequest),
                                                      completeMultipartUploadRequest));
        }
        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));
        }
Пример #4
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));
        }
Пример #7
0
        /// <inheritdoc/>
        public Uri GeneratePresignedUri(GeneratePresignedUriRequest generatePresignedUriRequest)
        {
            ThrowIfNullRequest(generatePresignedUriRequest);

            var creds         = _credsProvider.GetCredentials();
            var accessId      = creds.AccessId;
            var accessKey     = creds.AccessKey;
            var securityToken = creds.SecurityToken;
            var useToken      = creds.UseToken;
            var bucketName    = generatePresignedUriRequest.BucketName;
            var key           = generatePresignedUriRequest.Key;

            const long ticksOf1970 = 621355968000000000;
            var        expires     = ((generatePresignedUriRequest.Expiration.ToUniversalTime().Ticks - ticksOf1970) / 10000000L)
                                     .ToString(CultureInfo.InvariantCulture);
            var resourcePath = OssUtils.MakeResourcePath(key);

            var request = new ServiceRequest();
            var conf    = OssUtils.GetClientConfiguration(_serviceClient);

            request.Endpoint     = OssUtils.MakeBucketEndpoint(_endpoint, bucketName, conf);
            request.ResourcePath = resourcePath;

            switch (generatePresignedUriRequest.Method)
            {
            case SignHttpMethod.Get:
                request.Method = HttpMethod.Get;
                break;

            case SignHttpMethod.Put:
                request.Method = HttpMethod.Put;
                break;

            default:
                throw new ArgumentException("Unsupported http method.");
            }

            request.Headers.Add(HttpHeaders.Date, expires);
            if (!string.IsNullOrEmpty(generatePresignedUriRequest.ContentType))
            {
                request.Headers.Add(HttpHeaders.ContentType, generatePresignedUriRequest.ContentType);
            }
            if (!string.IsNullOrEmpty(generatePresignedUriRequest.ContentMd5))
            {
                request.Headers.Add(HttpHeaders.ContentMd5, generatePresignedUriRequest.ContentMd5);
            }

            foreach (var pair in generatePresignedUriRequest.UserMetadata)
            {
                request.Headers.Add(OssHeaders.OssUserMetaPrefix + pair.Key, pair.Value);
            }

            if (generatePresignedUriRequest.ResponseHeaders != null)
            {
                generatePresignedUriRequest.ResponseHeaders.Populate(request.Parameters);
            }

            foreach (var param in generatePresignedUriRequest.QueryParams)
            {
                request.Parameters.Add(param.Key, param.Value);
            }

            if (useToken)
            {
                request.Parameters.Add(RequestParameters.SECURITY_TOKEN, securityToken);
            }

            var canonicalResource = "/" + (bucketName ?? "") + ((key != null ? "/" + key : ""));
            var httpMethod        = generatePresignedUriRequest.Method.ToString().ToUpperInvariant();

            var canonicalString =
                SignUtils.BuildCanonicalString(httpMethod, canonicalResource, request /*, expires*/);
            var signature = ServiceSignature.Create().ComputeSignature(accessKey, canonicalString);

            IDictionary <string, string> queryParams = new Dictionary <string, string>();

            queryParams.Add(RequestParameters.EXPIRES, expires);
            queryParams.Add(RequestParameters.OSS_ACCESS_KEY_ID, accessId);
            queryParams.Add(RequestParameters.SIGNATURE, signature);
            foreach (var param in request.Parameters)
            {
                queryParams.Add(param.Key, param.Value);
            }

            var queryString = HttpUtils.ConbineQueryString(queryParams);
            var uriString   = request.Endpoint.ToString();

            if (!uriString.EndsWith("/"))
            {
                uriString += "/";
            }
            uriString += resourcePath + "?" + queryString;

            return(new Uri(uriString));
        }