Ejemplo n.º 1
0
        /// <summary>
        /// Copy a part.
        /// </summary>
        /// <param name="request">Parameters in a request for copying a part</param>
        /// <returns> Response to a part copy request</returns>
        public CopyPartResponse CopyPart(CopyPartRequest request)
        {
            CopyPartResponse response = this.DoRequest <CopyPartRequest, CopyPartResponse>(request, delegate()
            {
                if (request.ObjectKey == null)
                {
                    throw new ObsException(Constants.InvalidObjectKeyMessage, ErrorType.Sender, Constants.InvalidObjectKey, "");
                }
                if (string.IsNullOrEmpty(request.UploadId))
                {
                    throw new ObsException(Constants.InvalidUploadIdMessage, ErrorType.Sender, Constants.InvalidUploadId, "");
                }

                if (request.PartNumber <= 0)
                {
                    throw new ObsException(Constants.InvalidPartNumberMessage, ErrorType.Sender, Constants.InvalidPartNumber, "");
                }

                if (string.IsNullOrEmpty(request.SourceBucketName))
                {
                    throw new ObsException(Constants.InvalidSourceBucketNameMessage, ErrorType.Sender, Constants.InvalidBucketName, "");
                }
                if (request.SourceObjectKey == null)
                {
                    throw new ObsException(Constants.InvalidSourceObjectKeyMessage, ErrorType.Sender, Constants.InvalidObjectKey, "");
                }
            });

            response.PartNumber = request.PartNumber;
            return(response);
        }
        static void CopyPart()
        {
            try
            {
                ByteRange range = new ByteRange(10, 20);

                CopyPartRequest request = new CopyPartRequest()
                {
                    SourceBucketName = bucketName,
                    SourceObjectKey  = objectName,
                    SourceVersionId  = versionId,
                    ObjectKey        = destobjectName,
                    PartNumber       = 1,
                    UploadId         = uploadId,
                    ByteRange        = range
                };
                CopyPartResponse response = client.CopyPart(request);

                Console.WriteLine("Copy part response: {0}", response.StatusCode);
                Console.WriteLine("ETag: {0}", response.ETag);
                etag = response.ETag;
            }
            catch (ObsException ex)
            {
                Console.WriteLine("Exception errorcode: {0}, when copy part.", ex.ErrorCode);
                Console.WriteLine("Exception errormessage: {0}", ex.ErrorMessage);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 结束对复制段的异步请求。
        /// </summary>
        /// <param name="ar">异步请求的响应结果。</param>
        /// <returns>复制段的响应结果。</returns>
        public CopyPartResponse EndCopyPart(IAsyncResult ar)
        {
            CopyPartResponse   response = this.EndDoRequest <CopyPartRequest, CopyPartResponse>(ar);
            HttpObsAsyncResult result   = ar as HttpObsAsyncResult;
            CopyPartRequest    request  = result.AdditionalState as CopyPartRequest;

            response.PartNumber = request.PartNumber;
            return(response);
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, CopyPartResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.Read())
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("ETag", targetDepth))
                    {
                        response.ETag = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("LastModified", targetDepth))
                    {
                        response.LastModified = DateTimeUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                }
                else if (context.IsEndElement && context.CurrentDepth < originalDepth)
                {
                    return;
                }
            }


            IWebResponseData responseData = context.ResponseData;

            if (responseData.IsHeaderPresent("x-amz-copy-source-version-id"))
            {
                response.CopySourceVersionId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-copy-source-version-id"));
            }
            if (responseData.IsHeaderPresent("x-amz-server-side-encryption"))
            {
                response.ServerSideEncryptionMethod = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption"));
            }
            if (responseData.IsHeaderPresent(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader))
            {
                response.ServerSideEncryptionKeyManagementServiceKeyId = S3Transforms.ToString(responseData.GetHeaderValue(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader));
            }
            if (responseData.IsHeaderPresent(S3Constants.AmzHeaderBucketKeyEnabled))
            {
                response.BucketKeyEnabled = S3Transforms.ToBool(responseData.GetHeaderValue(S3Constants.AmzHeaderBucketKeyEnabled));
            }

            return;
        }
Ejemplo n.º 5
0
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            CopyPartResponse copyPartResponse = new CopyPartResponse();

            while (context.Read())
            {
                if (context.get_IsStartElement())
                {
                    UnmarshallResult(context, copyPartResponse);
                }
            }
            return(copyPartResponse);
        }
Ejemplo n.º 6
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, CopyPartResponse response)
        {
            int currentDepth = context.get_CurrentDepth();
            int num          = currentDepth + 1;

            if (context.get_IsStartOfDocument())
            {
                num += 2;
            }
            while (context.Read())
            {
                if (context.get_IsStartElement() || context.get_IsAttribute())
                {
                    if (context.TestExpression("ETag", num))
                    {
                        response.ETag = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("LastModified", num))
                    {
                        response.LastModified = DateTimeUnmarshaller.GetInstance().Unmarshall(context);
                    }
                }
                else if (context.get_IsEndElement() && context.get_CurrentDepth() < currentDepth)
                {
                    return;
                }
            }
            IWebResponseData responseData = context.get_ResponseData();

            if (responseData.IsHeaderPresent("x-amz-copy-source-version-id"))
            {
                response.CopySourceVersionId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-copy-source-version-id"));
            }
            if (responseData.IsHeaderPresent("x-amz-server-side-encryption"))
            {
                response.ServerSideEncryptionMethod = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption"));
            }
            if (responseData.IsHeaderPresent("x-amz-server-side-encryption-aws-kms-key-id"))
            {
                response.ServerSideEncryptionKeyManagementServiceKeyId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption-aws-kms-key-id"));
            }
        }
Ejemplo n.º 7
0
        private void TestCopyPart(string bucketName, string key, string keyId)
        {
            string   dstKey = "dstObject";
            string   srcKey = key;
            string   srcVersionID;
            string   srcETag;
            DateTime srcTimeStamp;
            string   uploadID = null;

            try
            {
                //Get the srcObjectTimestamp
                GetObjectMetadataResponse gomr = Client.GetObjectMetadata(new GetObjectMetadataRequest
                {
                    BucketName = bucketName,
                    Key        = srcKey
                });
                srcTimeStamp = gomr.LastModified;
                srcVersionID = gomr.VersionId;
                srcETag      = gomr.ETag;

                //Start the multipart upload
                InitiateMultipartUploadResponse imur = Client.InitiateMultipartUpload(new InitiateMultipartUploadRequest
                {
                    BucketName = bucketName,
                    Key        = dstKey,
                    ServerSideEncryptionMethod = ServerSideEncryptionMethod.AWSKMS,
                    ServerSideEncryptionKeyManagementServiceKeyId = keyId
                });
                Assert.AreEqual(ServerSideEncryptionMethod.AWSKMS, imur.ServerSideEncryptionMethod);
                var usedKeyId = imur.ServerSideEncryptionKeyManagementServiceKeyId;
                VerifyKeyId(keyId, usedKeyId);
                uploadID = imur.UploadId;


                CopyPartRequest request = new CopyPartRequest
                {
                    DestinationBucket = bucketName,
                    DestinationKey    = dstKey,
                    SourceBucket      = bucketName,
                    SourceKey         = srcKey,
                    UploadId          = uploadID,
                    PartNumber        = 1,
                };
                CopyPartResponse response = Client.CopyPart(request);
                Assert.AreEqual(ServerSideEncryptionMethod.AWSKMS, response.ServerSideEncryptionMethod);
                usedKeyId = response.ServerSideEncryptionKeyManagementServiceKeyId;
                VerifyKeyId(keyId, usedKeyId);

                //ETag
                Assert.IsNotNull(response.ETag);
                Assert.IsTrue((response.ETag != null) && (response.ETag.Length > 0));

                //LastModified
                Assert.IsNotNull(response.LastModified);
                Assert.AreNotEqual(DateTime.MinValue, response.LastModified);

                //PartNumber
                Assert.IsTrue(response.PartNumber == 1);

                var completeResponse = Client.CompleteMultipartUpload(new CompleteMultipartUploadRequest
                {
                    BucketName = bucketName,
                    Key        = dstKey,
                    UploadId   = uploadID,
                    PartETags  = new List <PartETag>()
                                                     {
                                         new PartETag  {
                             ETag  =  response.ETag,  PartNumber  =  response.PartNumber 
                        }
                                    
                    }
                });
                Assert.AreEqual(ServerSideEncryptionMethod.AWSKMS, completeResponse.ServerSideEncryptionMethod);
                usedKeyId = completeResponse.ServerSideEncryptionKeyManagementServiceKeyId;
                VerifyKeyId(keyId, usedKeyId);
            }
            finally
            {
                //abort the multipart upload
                if (uploadID != null)
                {
                    Client.AbortMultipartUpload(new AbortMultipartUploadRequest
                    {
                        BucketName = bucketName,
                        Key        = dstKey,
                        UploadId   = uploadID
                    });
                }
            }
        }
        private static void ProcessResponseHandlers(IExecutionContext executionContext)
        {
            //IL_00ac: Unknown result type (might be due to invalid IL or missing references)
            //IL_00b3: Expected O, but got Unknown
            AmazonWebServiceResponse response   = executionContext.get_ResponseContext().get_Response();
            IRequest          request           = executionContext.get_RequestContext().get_Request();
            bool              flag              = HasSSEHeaders(executionContext.get_ResponseContext().get_HttpResponse());
            GetObjectResponse getObjectResponse = response as GetObjectResponse;

            if (getObjectResponse != null)
            {
                GetObjectRequest getObjectRequest = request.get_OriginalRequest() as GetObjectRequest;
                getObjectResponse.BucketName = getObjectRequest.BucketName;
                getObjectResponse.Key        = getObjectRequest.Key;
                if (!string.IsNullOrEmpty(getObjectResponse.ETag) && !getObjectResponse.ETag.Contains("-") && !flag && getObjectRequest.ByteRange == null)
                {
                    byte[]     array = AWSSDKUtils.HexStringToBytes(getObjectResponse.ETag.Trim(etagTrimChars));
                    HashStream val   = getObjectResponse.ResponseStream = (Stream) new MD5Stream(getObjectResponse.ResponseStream, array, getObjectResponse.get_ContentLength());
                }
            }
            DeleteObjectsResponse deleteObjectsResponse = response as DeleteObjectsResponse;

            if (deleteObjectsResponse != null && deleteObjectsResponse.DeleteErrors != null && deleteObjectsResponse.DeleteErrors.Count > 0)
            {
                throw new DeleteObjectsException(deleteObjectsResponse);
            }
            PutObjectResponse putObjectResponse = response as PutObjectResponse;
            PutObjectRequest  putObjectRequest  = request.get_OriginalRequest() as PutObjectRequest;

            if (putObjectRequest != null)
            {
                HashStream val2 = putObjectRequest.InputStream as HashStream;
                if (val2 != null)
                {
                    if (putObjectResponse != null && !flag)
                    {
                        val2.CalculateHash();
                        CompareHashes(putObjectResponse.ETag, val2.get_CalculatedHash());
                    }
                    putObjectRequest.InputStream = val2.GetNonWrapperBaseStream();
                }
            }
            ListObjectsResponse listObjectsResponse = response as ListObjectsResponse;

            if (listObjectsResponse != null && listObjectsResponse.IsTruncated && string.IsNullOrEmpty(listObjectsResponse.NextMarker) && listObjectsResponse.S3Objects.Count > 0)
            {
                listObjectsResponse.NextMarker = listObjectsResponse.S3Objects.Last().Key;
            }
            UploadPartRequest  uploadPartRequest  = request.get_OriginalRequest() as UploadPartRequest;
            UploadPartResponse uploadPartResponse = response as UploadPartResponse;

            if (uploadPartRequest != null)
            {
                if (uploadPartResponse != null)
                {
                    uploadPartResponse.PartNumber = uploadPartRequest.PartNumber;
                }
                HashStream val3 = uploadPartRequest.InputStream as HashStream;
                if (val3 != null)
                {
                    if (uploadPartResponse != null && !flag)
                    {
                        val3.CalculateHash();
                        CompareHashes(uploadPartResponse.ETag, val3.get_CalculatedHash());
                    }
                    uploadPartRequest.InputStream = val3.GetNonWrapperBaseStream();
                }
            }
            CopyPartResponse copyPartResponse = response as CopyPartResponse;

            if (copyPartResponse != null)
            {
                copyPartResponse.PartNumber = ((CopyPartRequest)request.get_OriginalRequest()).PartNumber;
            }
            AmazonS3Client.CleanupRequest(request);
        }