public IRequest Marshall(PutBucketVersioningRequest putBucketVersioningRequest)
        {
            IRequest request = new DefaultRequest(putBucketVersioningRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putBucketVersioningRequest.IsSetMfaCodes())
            {
                request.Headers.Add("x-amz-mfa", putBucketVersioningRequest.MfaCodes.FormattedMfaCodes);
            }

            var uriResourcePath = string.Concat("/", S3Transforms.ToStringValue(putBucketVersioningRequest.BucketName));

            request.Parameters.Add("versioning", null);

            request.CanonicalResource = S3Transforms.GetCanonicalResource(uriResourcePath, request.Parameters);
            request.ResourcePath      = S3Transforms.FormatResourcePath(uriResourcePath, request.Parameters);

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                var versioningConfigurationVersioningConfiguration = putBucketVersioningRequest.VersioningConfig;
                if (versioningConfigurationVersioningConfiguration != null)
                {
                    xmlWriter.WriteStartElement("VersioningConfiguration", "");
                    if (versioningConfigurationVersioningConfiguration.IsSetEnableMfaDelete())
                    {
                        xmlWriter.WriteElementString("MFADelete", "", versioningConfigurationVersioningConfiguration.EnableMfaDelete ? "Enabled" : "Disabled");
                    }
                    if (versioningConfigurationVersioningConfiguration.IsSetStatus())
                    {
                        xmlWriter.WriteElementString("Status", "", S3Transforms.ToXmlStringValue(versioningConfigurationVersioningConfiguration.Status));
                    }
                    xmlWriter.WriteEndElement();
                }
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers["Content-Type"] = "application/xml";

                request.Parameters[S3QueryParameter.ContentType.ToString()] = "application/xml";
                var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                request.Headers[Amazon.Util.AWSSDKUtils.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            if (!request.UseQueryString)
            {
                var queryString = Amazon.Util.AWSSDKUtils.GetParametersAsString(request.Parameters);
                if (!string.IsNullOrEmpty(queryString))
                {
                    request.ResourcePath = string.Concat(request.ResourcePath, request.ResourcePath.Contains("?") ? "&" : "?", queryString);
                }
            }

            return(request);
        }
Exemple #2
0
        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("ChecksumCRC32", targetDepth))
                    {
                        response.ChecksumCRC32 = StringUnmarshaller.GetInstance().Unmarshall(context);
                        continue;
                    }
                    if (context.TestExpression("ChecksumCRC32C", targetDepth))
                    {
                        response.ChecksumCRC32C = StringUnmarshaller.GetInstance().Unmarshall(context);
                        continue;
                    }
                    if (context.TestExpression("ChecksumSHA1", targetDepth))
                    {
                        response.ChecksumSHA1 = StringUnmarshaller.GetInstance().Unmarshall(context);
                        continue;
                    }
                    if (context.TestExpression("ChecksumSHA256", targetDepth))
                    {
                        response.ChecksumSHA256 = StringUnmarshaller.GetInstance().Unmarshall(context);
                        continue;
                    }
                    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;
        }
        public IRequest Marshall(PutBucketEncryptionRequest putBucketEncryptionRequest)
        {
            IRequest request = new DefaultRequest(putBucketEncryptionRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putBucketEncryptionRequest.IsSetExpectedBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedBucketOwner, S3Transforms.ToStringValue(putBucketEncryptionRequest.ExpectedBucketOwner));
            }

            if (string.IsNullOrEmpty(putBucketEncryptionRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "PutBucketEncryptionRequest.BucketName");
            }

            request.MarshallerVersion = 2;
            request.ResourcePath      = string.Concat("/", S3Transforms.ToStringValue(putBucketEncryptionRequest.BucketName));

            request.AddSubResource("encryption");

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                if (putBucketEncryptionRequest.IsSetServerSideEncryptionConfiguration())
                {
                    var sseConfiguration = putBucketEncryptionRequest.ServerSideEncryptionConfiguration;
                    xmlWriter.WriteStartElement("ServerSideEncryptionConfiguration", "");
                    if (sseConfiguration != null)
                    {
                        foreach (var serverSideEncryptionRule in sseConfiguration.ServerSideEncryptionRules)
                        {
                            xmlWriter.WriteStartElement("Rule", "");
                            if (serverSideEncryptionRule != null)
                            {
                                if (serverSideEncryptionRule.IsSetServerSideEncryptionByDefault())
                                {
                                    xmlWriter.WriteStartElement("ApplyServerSideEncryptionByDefault", "");
                                    var sseDefault = serverSideEncryptionRule.ServerSideEncryptionByDefault;
                                    if (sseDefault.IsSetServerSideEncryptionAlgorithm())
                                    {
                                        xmlWriter.WriteElementString("SSEAlgorithm", "", S3Transforms.ToXmlStringValue(sseDefault.ServerSideEncryptionAlgorithm));
                                    }
                                    if (sseDefault.IsSetServerSideEncryptionKeyManagementServiceKeyId())
                                    {
                                        xmlWriter.WriteElementString("KMSMasterKeyID", "", S3Transforms.ToXmlStringValue(sseDefault.ServerSideEncryptionKeyManagementServiceKeyId));
                                    }
                                    xmlWriter.WriteEndElement();
                                }

                                if (serverSideEncryptionRule.IsSetBucketKeyEnabled())
                                {
                                    xmlWriter.WriteElementString("BucketKeyEnabled", "", S3Transforms.ToXmlStringValue(serverSideEncryptionRule.BucketKeyEnabled));
                                }
                            }
                            xmlWriter.WriteEndElement();
                        }
                    }
                    xmlWriter.WriteEndElement();
                }
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                if (putBucketEncryptionRequest.IsSetContentMD5())
                {
                    checksum = putBucketEncryptionRequest.ContentMD5;
                }

                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
        public IRequest Marshall(GetObjectMetadataRequest headObjectRequest)
        {
            if (string.IsNullOrEmpty(headObjectRequest.Key))
            {
                throw new System.ArgumentException("Key is a required property and must be set before making this call.", "GetObjectMetadataRequest.Key");
            }

            IRequest request = new DefaultRequest(headObjectRequest, "AmazonS3");

            request.HttpMethod = "HEAD";

            if (headObjectRequest.IsSetEtagToMatch())
            {
                request.Headers.Add(HeaderKeys.IfMatchHeader, S3Transforms.ToStringValue(headObjectRequest.EtagToMatch));
            }

            if (headObjectRequest.IsSetModifiedSinceDateUtc())
            {
                request.Headers.Add(HeaderKeys.IfModifiedSinceHeader, S3Transforms.ToStringValue(headObjectRequest.ModifiedSinceDateUtc));
            }

            if (headObjectRequest.IsSetEtagToNotMatch())
            {
                request.Headers.Add(HeaderKeys.IfNoneMatchHeader, S3Transforms.ToStringValue(headObjectRequest.EtagToNotMatch));
            }

            if (headObjectRequest.IsSetUnmodifiedSinceDateUtc())
            {
                request.Headers.Add(HeaderKeys.IfUnmodifiedSinceHeader, S3Transforms.ToStringValue(headObjectRequest.UnmodifiedSinceDateUtc));
            }

            if (headObjectRequest.IsSetServerSideEncryptionCustomerMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerAlgorithmHeader, headObjectRequest.ServerSideEncryptionCustomerMethod);
            }
            if (headObjectRequest.IsSetServerSideEncryptionCustomerProvidedKey())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyHeader, headObjectRequest.ServerSideEncryptionCustomerProvidedKey);
                if (headObjectRequest.IsSetServerSideEncryptionCustomerProvidedKeyMD5())
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, headObjectRequest.ServerSideEncryptionCustomerProvidedKeyMD5);
                }
                else
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, AmazonS3Util.ComputeEncodedMD5FromEncodedString(headObjectRequest.ServerSideEncryptionCustomerProvidedKey));
                }
            }
            if (headObjectRequest.IsSetRequestPayer())
            {
                request.Headers.Add(S3Constants.AmzHeaderRequestPayer, S3Transforms.ToStringValue(headObjectRequest.RequestPayer.ToString()));
            }

            if (string.IsNullOrEmpty(headObjectRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "GetObjectMetadataRequest.BucketName");
            }
            if (string.IsNullOrEmpty(headObjectRequest.Key))
            {
                throw new System.ArgumentException("Key is a required property and must be set before making this call.", "GetObjectMetadataRequest.Key");
            }

            request.MarshallerVersion = 2;
            request.ResourcePath      = string.Format(CultureInfo.InvariantCulture, "/{0}/{1}",
                                                      S3Transforms.ToStringValue(headObjectRequest.BucketName),
                                                      S3Transforms.ToStringValue(headObjectRequest.Key));

            if (headObjectRequest.IsSetVersionId())
            {
                request.AddSubResource("versionId", S3Transforms.ToStringValue(headObjectRequest.VersionId));
            }
            if (headObjectRequest.IsSetPartNumber())
            {
                request.AddSubResource("partNumber", S3Transforms.ToStringValue(headObjectRequest.PartNumber.Value));
            }

            request.UseQueryString = true;

            return(request);
        }
Exemple #5
0
        /// <summary>
        /// Marshaller the request object to the HTTP request.
        /// </summary>
        /// <param name="publicRequest"></param>
        /// <returns></returns>
        public IRequest Marshall(PutObjectLockConfigurationRequest publicRequest)
        {
            var request = new DefaultRequest(publicRequest, "AmazonS3");

            request.HttpMethod = "PUT";
            string uriResourcePath = "/{Bucket}";

            request.AddSubResource("object-lock");

            if (publicRequest.IsSetContentMD5())
            {
                request.Headers.Add(HeaderKeys.ContentMD5Header, S3Transforms.ToStringValue(publicRequest.ContentMD5));
            }
            if (publicRequest.IsSetRequestPayer())
            {
                request.Headers.Add(S3Constants.AmzHeaderRequestPayer, S3Transforms.ToStringValue(publicRequest.RequestPayer.ToString()));
            }
            if (publicRequest.IsSetToken())
            {
                request.Headers.Add("x-amz-bucket-object-lock-token", publicRequest.Token);
            }
            if (!publicRequest.IsSetBucketName())
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "publicRequest.BucketName");
            }
            uriResourcePath = uriResourcePath.Replace("{Bucket}", StringUtils.FromString(publicRequest.BucketName));

            request.MarshallerVersion = 2;
            request.ResourcePath      = uriResourcePath;

            var stringWriter = new StringWriter(CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = System.Text.Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                if (publicRequest.IsSetObjectLockConfiguration())
                {
                    xmlWriter.WriteStartElement("ObjectLockConfiguration", "http://s3.amazonaws.com/doc/2006-03-01/");
                    if (publicRequest.ObjectLockConfiguration.IsSetObjectLockEnabled())
                    {
                        xmlWriter.WriteElementString("ObjectLockEnabled", "http://s3.amazonaws.com/doc/2006-03-01/", StringUtils.FromString(publicRequest.ObjectLockConfiguration.ObjectLockEnabled));
                    }


                    if (publicRequest.ObjectLockConfiguration.Rule != null)
                    {
                        xmlWriter.WriteStartElement("Rule", "http://s3.amazonaws.com/doc/2006-03-01/");


                        if (publicRequest.ObjectLockConfiguration.Rule.DefaultRetention != null)
                        {
                            xmlWriter.WriteStartElement("DefaultRetention", "http://s3.amazonaws.com/doc/2006-03-01/");

                            if (publicRequest.ObjectLockConfiguration.Rule.DefaultRetention.IsSetDays())
                            {
                                xmlWriter.WriteElementString("Days", "http://s3.amazonaws.com/doc/2006-03-01/", StringUtils.FromInt(publicRequest.ObjectLockConfiguration.Rule.DefaultRetention.Days));
                            }

                            if (publicRequest.ObjectLockConfiguration.Rule.DefaultRetention.IsSetMode())
                            {
                                xmlWriter.WriteElementString("Mode", "http://s3.amazonaws.com/doc/2006-03-01/", StringUtils.FromString(publicRequest.ObjectLockConfiguration.Rule.DefaultRetention.Mode));
                            }

                            if (publicRequest.ObjectLockConfiguration.Rule.DefaultRetention.IsSetYears())
                            {
                                xmlWriter.WriteElementString("Years", "http://s3.amazonaws.com/doc/2006-03-01/", StringUtils.FromInt(publicRequest.ObjectLockConfiguration.Rule.DefaultRetention.Years));
                            }

                            xmlWriter.WriteEndElement();
                        }
                        xmlWriter.WriteEndElement();
                    }

                    xmlWriter.WriteEndElement();
                }
            }
            try
            {
                string content = stringWriter.ToString();
                request.Content = System.Text.Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
        public IRequest Marshall(GetObjectRequest getObjectRequest)
        {
            if (string.IsNullOrEmpty(getObjectRequest.Key))
            {
                throw new System.ArgumentException("Key is a required property and must be set before making this call.", "GetObjectRequest.Key");
            }

            IRequest request = new DefaultRequest(getObjectRequest, "AmazonS3");

            request.HttpMethod = "GET";

            if (getObjectRequest.IsSetEtagToMatch())
            {
                request.Headers.Add(HeaderKeys.IfMatchHeader, S3Transforms.ToStringValue(getObjectRequest.EtagToMatch));
            }

            if (getObjectRequest.IsSetModifiedSinceDateUtc())
            {
                request.Headers.Add(HeaderKeys.IfModifiedSinceHeader, S3Transforms.ToStringValue(getObjectRequest.ModifiedSinceDateUtc));
            }

            if (getObjectRequest.IsSetEtagToNotMatch())
            {
                request.Headers.Add(HeaderKeys.IfNoneMatchHeader, S3Transforms.ToStringValue(getObjectRequest.EtagToNotMatch));
            }

            if (getObjectRequest.IsSetUnmodifiedSinceDateUtc())
            {
                request.Headers.Add(HeaderKeys.IfUnmodifiedSinceHeader, S3Transforms.ToStringValue(getObjectRequest.UnmodifiedSinceDateUtc));
            }

            if (getObjectRequest.IsSetByteRange())
            {
                request.Headers.Add(HeaderKeys.RangeHeader, getObjectRequest.ByteRange.FormattedByteRange);
            }

            if (getObjectRequest.IsSetServerSideEncryptionCustomerMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerAlgorithmHeader, getObjectRequest.ServerSideEncryptionCustomerMethod);
            }
            if (getObjectRequest.IsSetServerSideEncryptionCustomerProvidedKey())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyHeader, getObjectRequest.ServerSideEncryptionCustomerProvidedKey);
                if (getObjectRequest.IsSetServerSideEncryptionCustomerProvidedKeyMD5())
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, getObjectRequest.ServerSideEncryptionCustomerProvidedKeyMD5);
                }
                else
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, AmazonS3Util.ComputeEncodedMD5FromEncodedString(getObjectRequest.ServerSideEncryptionCustomerProvidedKey));
                }
            }
            if (getObjectRequest.IsSetRequestPayer())
            {
                request.Headers.Add(S3Constants.AmzHeaderRequestPayer, S3Transforms.ToStringValue(getObjectRequest.RequestPayer.ToString()));
            }

            if (getObjectRequest.IsSetExpectedBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedBucketOwner, S3Transforms.ToStringValue(getObjectRequest.ExpectedBucketOwner));
            }

            if (string.IsNullOrEmpty(getObjectRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "GetObjectRequest.BucketName");
            }
            if (string.IsNullOrEmpty(getObjectRequest.Key))
            {
                throw new System.ArgumentException("Key is a required property and must be set before making this call.", "GetObjectRequest.Key");
            }

            request.MarshallerVersion = 2;
            request.ResourcePath      = string.Format(CultureInfo.InvariantCulture, "/{0}/{1}",
                                                      S3Transforms.ToStringValue(getObjectRequest.BucketName),
                                                      S3Transforms.ToStringValue(getObjectRequest.Key));

            var headerOverrides = getObjectRequest.ResponseHeaderOverrides;

            if (headerOverrides.CacheControl != null)
            {
                request.Parameters.Add("response-cache-control", S3Transforms.ToStringValue(headerOverrides.CacheControl));
            }
            if (headerOverrides.ContentDisposition != null)
            {
                request.Parameters.Add("response-content-disposition", S3Transforms.ToStringValue(headerOverrides.ContentDisposition));
            }
            if (headerOverrides.ContentEncoding != null)
            {
                request.Parameters.Add("response-content-encoding", S3Transforms.ToStringValue(headerOverrides.ContentEncoding));
            }
            if (headerOverrides.ContentLanguage != null)
            {
                request.Parameters.Add("response-content-language", S3Transforms.ToStringValue(headerOverrides.ContentLanguage));
            }
            if (headerOverrides.ContentType != null)
            {
                request.Parameters.Add("response-content-type", S3Transforms.ToStringValue(headerOverrides.ContentType));
            }
            if (getObjectRequest.IsSetResponseExpiresUtc())
            {
                request.Parameters.Add("response-expires", S3Transforms.ToStringValue(getObjectRequest.ResponseExpiresUtc));
            }
            if (getObjectRequest.IsSetVersionId())
            {
                request.AddSubResource("versionId", S3Transforms.ToStringValue(getObjectRequest.VersionId));
            }
            if (getObjectRequest.IsSetPartNumber())
            {
                request.AddSubResource("partNumber", S3Transforms.ToStringValue(getObjectRequest.PartNumber.Value));
            }

            request.UseQueryString = true;

            return(request);
        }
        public IRequest Marshall(PutPublicAccessBlockRequest putPutPublicAccessBlockRequest)
        {
            IRequest request = new DefaultRequest(putPutPublicAccessBlockRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (string.IsNullOrEmpty(putPutPublicAccessBlockRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "putPutPublicAccessBlockRequest.BucketName");
            }

            request.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(putPutPublicAccessBlockRequest.BucketName));

            request.AddSubResource("publicAccessBlock");

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                var publicAccessBlockConfiguration = putPutPublicAccessBlockRequest.PublicAccessBlockConfiguration;
                if (publicAccessBlockConfiguration != null)
                {
                    xmlWriter.WriteStartElement("PublicAccessBlockConfiguration", "http://s3.amazonaws.com/doc/2006-03-01/");
                    if (publicAccessBlockConfiguration.IsSetBlockPublicAcls())
                    {
                        xmlWriter.WriteElementString("BlockPublicAcls", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(publicAccessBlockConfiguration.BlockPublicAcls));
                    }
                    if (publicAccessBlockConfiguration.IsSetIgnorePublicAcls())
                    {
                        xmlWriter.WriteElementString("IgnorePublicAcls", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(publicAccessBlockConfiguration.IgnorePublicAcls));
                    }
                    if (publicAccessBlockConfiguration.IsSetBlockPublicPolicy())
                    {
                        xmlWriter.WriteElementString("BlockPublicPolicy", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(publicAccessBlockConfiguration.BlockPublicPolicy));
                    }
                    if (publicAccessBlockConfiguration.IsSetRestrictPublicBuckets())
                    {
                        xmlWriter.WriteElementString("RestrictPublicBuckets", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(publicAccessBlockConfiguration.RestrictPublicBuckets));
                    }
                    xmlWriter.WriteEndElement();
                }
            }
            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                string checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                if (putPutPublicAccessBlockRequest.IsSetContentMD5())
                {
                    checksum = putPutPublicAccessBlockRequest.ContentMD5;
                }
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Exemple #8
0
        public IRequest Marshall(ListPartsRequest listPartsRequest)
        {
            IRequest request = new DefaultRequest(listPartsRequest, "AmazonS3");

            if (listPartsRequest.IsSetRequestPayer())
            {
                request.Headers.Add(S3Constants.AmzHeaderRequestPayer, S3Transforms.ToStringValue(listPartsRequest.RequestPayer.ToString()));
            }

            if (listPartsRequest.IsSetExpectedBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedBucketOwner, S3Transforms.ToStringValue(listPartsRequest.ExpectedBucketOwner));
            }

            request.HttpMethod = "GET";

            if (string.IsNullOrEmpty(listPartsRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "ListPartsRequest.BucketName");
            }
            if (string.IsNullOrEmpty(listPartsRequest.Key))
            {
                throw new System.ArgumentException("Key is a required property and must be set before making this call.", "ListPartsRequest.Key");
            }

            request.ResourcePath = string.Format(CultureInfo.InvariantCulture, "/{0}/{1}",
                                                 S3Transforms.ToStringValue(listPartsRequest.BucketName),
                                                 S3Transforms.ToStringValue(listPartsRequest.Key));

            if (listPartsRequest.IsSetUploadId())
            {
                request.AddSubResource("uploadId", S3Transforms.ToStringValue(listPartsRequest.UploadId));
            }

            if (listPartsRequest.IsSetMaxParts())
            {
                request.Parameters.Add("max-parts", S3Transforms.ToStringValue(listPartsRequest.MaxParts));
            }
            if (listPartsRequest.IsSetPartNumberMarker())
            {
                request.Parameters.Add("part-number-marker", S3Transforms.ToStringValue(listPartsRequest.PartNumberMarker));
            }
            if (listPartsRequest.IsSetEncoding())
            {
                request.Parameters.Add("encoding-type", S3Transforms.ToStringValue(listPartsRequest.Encoding));
            }
            if (listPartsRequest.IsSetSSECustomerAlgorithm())
            {
                request.Headers["x-amz-server-side-encryption-customer-algorithm"] = S3Transforms.ToStringValue(listPartsRequest.SSECustomerAlgorithm);
            }
            if (listPartsRequest.IsSetSSECustomerKey())
            {
                request.Headers["x-amz-server-side-encryption-customer-key"] = S3Transforms.ToStringValue(listPartsRequest.SSECustomerKey);
            }
            if (listPartsRequest.IsSetSSECustomerKeyMD5())
            {
                request.Headers["x-amz-server-side-encryption-customer-key-MD5"] = S3Transforms.ToStringValue(listPartsRequest.SSECustomerKeyMD5);
            }

            request.UseQueryString = true;

            return(request);
        }
        public IRequest Marshall(CopyPartRequest copyPartRequest)
        {
            IRequest request = new DefaultRequest(copyPartRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (copyPartRequest.IsSetSourceBucket())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceHeader, ConstructCopySourceHeaderValue(copyPartRequest.SourceBucket, copyPartRequest.SourceKey, copyPartRequest.SourceVersionId));
            }

            if (copyPartRequest.IsSetETagToMatch())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfMatchHeader, AWSSDKUtils.Join(copyPartRequest.ETagToMatch));
            }

            if (copyPartRequest.IsSetETagToNotMatch())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfNoneMatchHeader, AWSSDKUtils.Join(copyPartRequest.ETagsToNotMatch));
            }

            if (copyPartRequest.IsSetModifiedSinceDate())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfModifiedSinceHeader, copyPartRequest.ModifiedSinceDate.ToUniversalTime().ToString(AWSSDKUtils.GMTDateFormat, CultureInfo.InvariantCulture));
            }

            if (copyPartRequest.IsSetUnmodifiedSinceDate())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfUnmodifiedSinceHeader, copyPartRequest.UnmodifiedSinceDate.ToUniversalTime().ToString(AWSSDKUtils.GMTDateFormat, CultureInfo.InvariantCulture));
            }

            if (copyPartRequest.IsSetServerSideEncryptionCustomerMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerAlgorithmHeader, copyPartRequest.ServerSideEncryptionCustomerMethod);
            }
            if (copyPartRequest.IsSetServerSideEncryptionCustomerProvidedKey())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyHeader, copyPartRequest.ServerSideEncryptionCustomerProvidedKey);
                if (copyPartRequest.IsSetServerSideEncryptionCustomerProvidedKeyMD5())
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, copyPartRequest.ServerSideEncryptionCustomerProvidedKeyMD5);
                }
                else
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, AmazonS3Util.ComputeEncodedMD5FromEncodedString(copyPartRequest.ServerSideEncryptionCustomerProvidedKey));
                }
            }
            if (copyPartRequest.IsSetCopySourceServerSideEncryptionCustomerMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerAlgorithmHeader, copyPartRequest.CopySourceServerSideEncryptionCustomerMethod);
            }
            if (copyPartRequest.IsSetCopySourceServerSideEncryptionCustomerProvidedKey())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerKeyHeader, copyPartRequest.CopySourceServerSideEncryptionCustomerProvidedKey);
                if (copyPartRequest.IsSetCopySourceServerSideEncryptionCustomerProvidedKeyMD5())
                {
                    request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerKeyMD5Header, copyPartRequest.CopySourceServerSideEncryptionCustomerProvidedKeyMD5);
                }
                else
                {
                    request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerKeyMD5Header, AmazonS3Util.ComputeEncodedMD5FromEncodedString(copyPartRequest.CopySourceServerSideEncryptionCustomerProvidedKey));
                }
            }
            if (copyPartRequest.IsSetServerSideEncryptionKeyManagementServiceKeyId())
            {
                request.Headers.Add(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader, copyPartRequest.ServerSideEncryptionKeyManagementServiceKeyId);
            }

            request.Headers.Add(HeaderKeys.XAmzCopySourceRangeHeader, ConstructCopySourceRangeHeader(copyPartRequest.FirstByte, copyPartRequest.LastByte));

            request.ResourcePath = string.Format(CultureInfo.InvariantCulture, "/{0}/{1}",
                                                 S3Transforms.ToStringValue(copyPartRequest.DestinationBucket),
                                                 S3Transforms.ToStringValue(copyPartRequest.DestinationKey));

            request.AddSubResource("partNumber", S3Transforms.ToStringValue(copyPartRequest.PartNumber));
            request.AddSubResource("uploadId", S3Transforms.ToStringValue(copyPartRequest.UploadId));

            request.UseQueryString = true;

            return(request);
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, ListPartsResponse 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("Bucket", num))
                    {
                        response.BucketName = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("Key", num))
                    {
                        response.Key = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("UploadId", num))
                    {
                        response.UploadId = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("PartNumberMarker", num))
                    {
                        response.PartNumberMarker = IntUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("NextPartNumberMarker", num))
                    {
                        response.NextPartNumberMarker = IntUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("MaxParts", num))
                    {
                        response.MaxParts = IntUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("IsTruncated", num))
                    {
                        response.IsTruncated = BoolUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("Part", num))
                    {
                        response.Parts.Add(PartDetailUnmarshaller.Instance.Unmarshall(context));
                    }
                    else if (context.TestExpression("Initiator", num))
                    {
                        response.Initiator = InitiatorUnmarshaller.Instance.Unmarshall(context);
                    }
                    else if (context.TestExpression("Owner", num))
                    {
                        response.Owner = OwnerUnmarshaller.Instance.Unmarshall(context);
                    }
                    else if (context.TestExpression("StorageClass", num))
                    {
                        response.StorageClass = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                }
                else if (context.get_IsEndElement() && context.get_CurrentDepth() < currentDepth)
                {
                    return;
                }
            }
            IWebResponseData responseData = context.get_ResponseData();

            if (responseData.IsHeaderPresent("x-amz-abort-date"))
            {
                response.AbortDate = S3Transforms.ToDateTime(responseData.GetHeaderValue("x-amz-abort-date"));
            }
            if (responseData.IsHeaderPresent("x-amz-abort-rule-id"))
            {
                response.AbortRuleId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-abort-rule-id"));
            }
            if (responseData.IsHeaderPresent(S3Constants.AmzHeaderRequestCharged))
            {
                response.RequestCharged = RequestCharged.FindValue(responseData.GetHeaderValue(S3Constants.AmzHeaderRequestCharged));
            }
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, InitiateMultipartUploadResponse 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("Bucket", targetDepth))
                    {
                        response.BucketName = StringUnmarshaller.GetInstance().Unmarshall(context);

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

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

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


            IWebResponseData responseData = context.ResponseData;

            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-customer-algorithm"))
            {
                response.ServerSideEncryptionCustomerMethod = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption-customer-algorithm"));
            }
            if (responseData.IsHeaderPresent("x-amz-server-side-encryption-customer-key-MD5"))
            {
                response.ServerSideEncryptionCustomerProvidedKeyMD5 = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption-customer-key-MD5"));
            }
            if (responseData.IsHeaderPresent(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader))
            {
                response.ServerSideEncryptionKeyManagementServiceKeyId = S3Transforms.ToString(responseData.GetHeaderValue(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader));
            }
            if (responseData.IsHeaderPresent("x-amz-server-side-encryption-context"))
            {
                response.ServerSideEncryptionKeyManagementServiceEncryptionContext = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption-context"));
            }
            if (responseData.IsHeaderPresent(HeaderKeys.XAmzAbortDateHeader))
            {
                response.AbortDate = S3Transforms.ToDateTime(responseData.GetHeaderValue(HeaderKeys.XAmzAbortDateHeader));
            }
            if (responseData.IsHeaderPresent(HeaderKeys.XAmzAbortRuleIdHeader))
            {
                response.AbortRuleId = S3Transforms.ToString(responseData.GetHeaderValue(HeaderKeys.XAmzAbortRuleIdHeader));
            }
            if (responseData.IsHeaderPresent(S3Constants.AmzHeaderRequestCharged))
            {
                response.RequestCharged = RequestCharged.FindValue(responseData.GetHeaderValue(S3Constants.AmzHeaderRequestCharged));
            }

            return;
        }
Exemple #12
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, CopyObjectResponse 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 = StringUnmarshaller.GetInstance().Unmarshall(context);

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


            IWebResponseData responseData = context.ResponseData;

            if (responseData.IsHeaderPresent("x-amz-expiration"))
            {
                response.Expiration = new Expiration(responseData.GetHeaderValue("x-amz-expiration"));
            }
            if (responseData.IsHeaderPresent("x-amz-copy-source-version-id"))
            {
                response.SourceVersionId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-copy-source-version-id"));
            }
            if (responseData.IsHeaderPresent("x-amz-version-id"))
            {
                response.VersionId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-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-customer-algorithm"))
            {
                response.ServerSideEncryptionCustomerMethod = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption-customer-algorithm"));
            }
            if (responseData.IsHeaderPresent("x-amz-server-side-encryption-customer-key-MD5"))
            {
                response.ServerSideEncryptionCustomerProvidedKeyMD5 = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption-customer-key-MD5"));
            }
            if (responseData.IsHeaderPresent(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader))
            {
                response.ServerSideEncryptionKeyManagementServiceKeyId = S3Transforms.ToString(responseData.GetHeaderValue(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader));
            }
            if (responseData.IsHeaderPresent("x-amz-server-side-encryption-context"))
            {
                response.ServerSideEncryptionKeyManagementServiceEncryptionContext = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-server-side-encryption-context"));
            }
            if (responseData.IsHeaderPresent(S3Constants.AmzHeaderRequestCharged))
            {
                response.RequestCharged = RequestCharged.FindValue(responseData.GetHeaderValue(S3Constants.AmzHeaderRequestCharged));
            }

            return;
        }
Exemple #13
0
        public IRequest Marshall(DeleteObjectRequest deleteObjectRequest)
        {
            //IL_0006: Unknown result type (might be due to invalid IL or missing references)
            //IL_000c: Expected O, but got Unknown
            IRequest val = new DefaultRequest(deleteObjectRequest, "AmazonS3");

            val.set_HttpMethod("DELETE");
            if (deleteObjectRequest.IsSetMfaCodes())
            {
                val.get_Headers().Add("x-amz-mfa", deleteObjectRequest.MfaCodes.FormattedMfaCodes);
            }
            val.set_ResourcePath(string.Format(CultureInfo.InvariantCulture, "/{0}/{1}", S3Transforms.ToStringValue(deleteObjectRequest.BucketName), S3Transforms.ToStringValue(deleteObjectRequest.Key)));
            if (deleteObjectRequest.IsSetVersionId())
            {
                val.AddSubResource("versionId", S3Transforms.ToStringValue(deleteObjectRequest.VersionId));
            }
            if (deleteObjectRequest.IsSetRequestPayer())
            {
                val.get_Headers().Add(S3Constants.AmzHeaderRequestPayer, S3Transforms.ToStringValue(((object)deleteObjectRequest.RequestPayer).ToString()));
            }
            val.set_UseQueryString(true);
            return(val);
        }
Exemple #14
0
        public IRequest Marshall(GetObjectTaggingRequest getObjectTaggingRequest)
        {
            //IL_0006: Unknown result type (might be due to invalid IL or missing references)
            //IL_000c: Expected O, but got Unknown
            IRequest val = new DefaultRequest(getObjectTaggingRequest, "AmazonS3");

            val.set_HttpMethod("GET");
            val.set_UseQueryString(true);
            val.set_ResourcePath(string.Format(CultureInfo.InvariantCulture, "/{0}/{1}", S3Transforms.ToStringValue(getObjectTaggingRequest.BucketName), S3Transforms.ToStringValue(getObjectTaggingRequest.Key)));
            val.AddSubResource("tagging");
            return(val);
        }
Exemple #15
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, GetObjectMetadataResponse response)
        {
            IWebResponseData responseData = context.ResponseData;

            if (responseData.IsHeaderPresent("x-amz-delete-marker"))
            {
                response.DeleteMarker = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-delete-marker"));
            }
            if (responseData.IsHeaderPresent("accept-ranges"))
            {
                response.AcceptRanges = S3Transforms.ToString(responseData.GetHeaderValue("accept-ranges"));
            }
            if (responseData.IsHeaderPresent("x-amz-expiration"))
            {
                response.Expiration = new Expiration(responseData.GetHeaderValue("x-amz-expiration"));
            }
            if (responseData.IsHeaderPresent("x-amz-restore"))
            {
                bool     restoreInProgress;
                DateTime?restoreExpiration;
                AmazonS3Util.ParseAmzRestoreHeader(responseData.GetHeaderValue("x-amz-restore"), out restoreInProgress, out restoreExpiration);

                response.RestoreInProgress = restoreInProgress;
                response.RestoreExpiration = restoreExpiration;
            }
            if (responseData.IsHeaderPresent("Last-Modified"))
            {
                response.LastModified = S3Transforms.ToDateTime(responseData.GetHeaderValue("Last-Modified"));
            }
            if (responseData.IsHeaderPresent("ETag"))
            {
                response.ETag = S3Transforms.ToString(responseData.GetHeaderValue("ETag"));
            }
            if (responseData.IsHeaderPresent("x-amz-missing-meta"))
            {
                response.MissingMeta = S3Transforms.ToInt(responseData.GetHeaderValue("x-amz-missing-meta"));
            }
            if (responseData.IsHeaderPresent("x-amz-version-id"))
            {
                response.VersionId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-version-id"));
            }
            if (responseData.IsHeaderPresent("Cache-Control"))
            {
                response.Headers.CacheControl = S3Transforms.ToString(responseData.GetHeaderValue("Cache-Control"));
            }
            if (responseData.IsHeaderPresent("Content-Disposition"))
            {
                response.Headers.ContentDisposition = S3Transforms.ToString(responseData.GetHeaderValue("Content-Disposition"));
            }
            if (responseData.IsHeaderPresent("Content-Encoding"))
            {
                response.Headers.ContentEncoding = S3Transforms.ToString(responseData.GetHeaderValue("Content-Encoding"));
            }
            if (responseData.IsHeaderPresent("Content-Length"))
            {
                response.Headers.ContentLength = long.Parse(responseData.GetHeaderValue("Content-Length"), CultureInfo.InvariantCulture);
            }
            if (responseData.IsHeaderPresent("Content-Type"))
            {
                response.Headers.ContentType = S3Transforms.ToString(responseData.GetHeaderValue("Content-Type"));
            }
            if (responseData.IsHeaderPresent("Expires"))
            {
                response.Expires = S3Transforms.ToDateTime(responseData.GetHeaderValue("Expires"));
            }
            if (responseData.IsHeaderPresent("x-amz-website-redirect-location"))
            {
                response.WebsiteRedirectLocation = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-website-redirect-location"));
            }
            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-customer-algorithm"))
            {
                response.ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.FindValue(responseData.GetHeaderValue("x-amz-server-side-encryption-customer-algorithm"));
            }
            if (responseData.IsHeaderPresent(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader))
            {
                response.ServerSideEncryptionKeyManagementServiceKeyId = S3Transforms.ToString(responseData.GetHeaderValue(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader));
            }
            if (responseData.IsHeaderPresent("x-amz-replication-status"))
            {
                response.ReplicationStatus = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-replication-status"));
            }

            foreach (var name in responseData.GetHeaderNames())
            {
                if (name.StartsWith("x-amz-meta-", StringComparison.OrdinalIgnoreCase))
                {
                    response.Metadata[name] = responseData.GetHeaderValue(name);
                }
            }

            return;
        }
        public IRequest Marshall(PutBucketInventoryConfigurationRequest putBucketInventoryConfigurationRequest)
        {
            IRequest request = new DefaultRequest(putBucketInventoryConfigurationRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            request.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(putBucketInventoryConfigurationRequest.BucketName));

            request.AddSubResource("inventory");

            if (putBucketInventoryConfigurationRequest.IsSetInventoryId())
            {
                request.AddSubResource("id", S3Transforms.ToStringValue(putBucketInventoryConfigurationRequest.InventoryId));
            }

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                if (putBucketInventoryConfigurationRequest.IsSetInventoryConfiguration())
                {
                    var inventoryConfiguration = putBucketInventoryConfigurationRequest.InventoryConfiguration;
                    xmlWriter.WriteStartElement("InventoryConfiguration", "http://s3.amazonaws.com/doc/2006-03-01/");
                    if (inventoryConfiguration != null)
                    {
                        if (inventoryConfiguration.IsSetDestination())
                        {
                            InventoryDestination inventoryDestination = inventoryConfiguration.Destination;
                            xmlWriter.WriteStartElement("Destination", "http://s3.amazonaws.com/doc/2006-03-01/");
                            if (inventoryDestination.isSetS3BucketDestination())
                            {
                                InventoryS3BucketDestination inventoryS3BucketDestination = inventoryDestination.S3BucketDestination;
                                xmlWriter.WriteStartElement("S3BucketDestination", "http://s3.amazonaws.com/doc/2006-03-01/");
                                if (inventoryS3BucketDestination.IsSetAccountId())
                                {
                                    xmlWriter.WriteElementString("AccountId", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(inventoryS3BucketDestination.AccountId));
                                }
                                if (inventoryS3BucketDestination.IsSetBucketName())
                                {
                                    xmlWriter.WriteElementString("Bucket", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(inventoryS3BucketDestination.BucketName));
                                }
                                if (inventoryS3BucketDestination.IsSetInventoryFormat())
                                {
                                    xmlWriter.WriteElementString("Format", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(inventoryS3BucketDestination.InventoryFormat));
                                }
                                if (inventoryS3BucketDestination.IsSetPrefix())
                                {
                                    xmlWriter.WriteElementString("Prefix", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(inventoryS3BucketDestination.Prefix));
                                }
                                if (inventoryS3BucketDestination.IsSetInventoryEncryption())
                                {
                                    xmlWriter.WriteStartElement("Encryption", "http://s3.amazonaws.com/doc/2006-03-01/");
                                    var inventoryEncryption = inventoryS3BucketDestination.InventoryEncryption;
                                    if (inventoryEncryption.IsSetSSEKMS())
                                    {
                                        xmlWriter.WriteStartElement("SSE-KMS", "http://s3.amazonaws.com/doc/2006-03-01/");
                                        if (inventoryEncryption.SSEKMS.IsSetKeyId())
                                        {
                                            xmlWriter.WriteElementString("KeyId", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(inventoryEncryption.SSEKMS.KeyId));
                                        }
                                        xmlWriter.WriteEndElement();
                                    }
                                    if (inventoryEncryption.IsSetSSES3())
                                    {
                                        xmlWriter.WriteStartElement("SSE-S3", "http://s3.amazonaws.com/doc/2006-03-01/");
                                        xmlWriter.WriteEndElement();
                                    }
                                    xmlWriter.WriteEndElement();
                                }
                                xmlWriter.WriteEndElement();
                            }
                            xmlWriter.WriteEndElement();
                        }

                        xmlWriter.WriteElementString("IsEnabled", "http://s3.amazonaws.com/doc/2006-03-01/", inventoryConfiguration.IsEnabled.ToString().ToLowerInvariant());

                        if (inventoryConfiguration.IsSetInventoryFilter())
                        {
                            xmlWriter.WriteStartElement("Filter", "http://s3.amazonaws.com/doc/2006-03-01/");
                            var predicate = inventoryConfiguration.InventoryFilter.InventoryFilterPredicate;
                            predicate.Accept(new InventoryPredicateVisitor(xmlWriter));
                            xmlWriter.WriteEndElement();
                        }

                        if (inventoryConfiguration.IsSetInventoryId())
                        {
                            xmlWriter.WriteElementString("Id", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(inventoryConfiguration.InventoryId));
                        }

                        if (inventoryConfiguration.IsSetIncludedObjectVersions())
                        {
                            xmlWriter.WriteElementString("IncludedObjectVersions", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(inventoryConfiguration.IncludedObjectVersions));
                        }

                        if (inventoryConfiguration.IsSetInventoryOptionalFields())
                        {
                            xmlWriter.WriteStartElement("OptionalFields", "http://s3.amazonaws.com/doc/2006-03-01/");
                            foreach (var inventoryOptionalField in inventoryConfiguration.InventoryOptionalFields)
                            {
                                xmlWriter.WriteElementString("Field", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(inventoryOptionalField));
                            }
                            xmlWriter.WriteEndElement();
                        }

                        if (inventoryConfiguration.IsSetSchedule())
                        {
                            xmlWriter.WriteStartElement("Schedule", "http://s3.amazonaws.com/doc/2006-03-01/");
                            var inventorySchedule = inventoryConfiguration.Schedule;
                            if (inventorySchedule.IsFrequency())
                            {
                                xmlWriter.WriteElementString("Frequency", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(inventorySchedule.Frequency));
                            }
                            xmlWriter.WriteEndElement();
                        }
                    }
                    xmlWriter.WriteEndElement();
                }
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
        public IRequest Marshall(PutObjectRequest putObjectRequest)
        {
            IRequest request = new DefaultRequest(putObjectRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putObjectRequest.IsSetCannedACL())
            {
                request.Headers.Add(HeaderKeys.XAmzAclHeader, S3Transforms.ToStringValue(putObjectRequest.CannedACL));
            }

            var headers = putObjectRequest.Headers;

            foreach (var key in headers.Keys)
            {
                request.Headers[key] = headers[key];
            }

            if (putObjectRequest.IsSetMD5Digest())
            {
                request.Headers[HeaderKeys.ContentMD5Header] = putObjectRequest.MD5Digest;
            }

            HeaderACLRequestMarshaller.Marshall(request, putObjectRequest);

            if (putObjectRequest.IsSetServerSideEncryptionMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzServerSideEncryptionHeader, S3Transforms.ToStringValue(putObjectRequest.ServerSideEncryptionMethod));
            }

            if (putObjectRequest.IsSetStorageClass())
            {
                request.Headers.Add(HeaderKeys.XAmzStorageClassHeader, S3Transforms.ToStringValue(putObjectRequest.StorageClass));
            }

            if (putObjectRequest.IsSetWebsiteRedirectLocation())
            {
                request.Headers.Add(HeaderKeys.XAmzWebsiteRedirectLocationHeader, S3Transforms.ToStringValue(putObjectRequest.WebsiteRedirectLocation));
            }

            if (putObjectRequest.IsSetServerSideEncryptionCustomerMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerAlgorithmHeader, putObjectRequest.ServerSideEncryptionCustomerMethod);
            }
            if (putObjectRequest.IsSetServerSideEncryptionCustomerProvidedKey())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyHeader, putObjectRequest.ServerSideEncryptionCustomerProvidedKey);
                if (putObjectRequest.IsSetServerSideEncryptionCustomerProvidedKeyMD5())
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, putObjectRequest.ServerSideEncryptionCustomerProvidedKeyMD5);
                }
                else
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, AmazonS3Util.ComputeEncodedMD5FromEncodedString(putObjectRequest.ServerSideEncryptionCustomerProvidedKey));
                }
            }

            if (putObjectRequest.IsSetServerSideEncryptionKeyManagementServiceKeyId())
            {
                request.Headers.Add(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader, putObjectRequest.ServerSideEncryptionKeyManagementServiceKeyId);
            }

            if (putObjectRequest.IsSetServerSideEncryptionKeyManagementServiceEncryptionContext())
            {
                request.Headers.Add("x-amz-server-side-encryption-context", putObjectRequest.ServerSideEncryptionKeyManagementServiceEncryptionContext);
            }

            if (putObjectRequest.IsSetObjectLockLegalHoldStatus())
            {
                request.Headers.Add("x-amz-object-lock-legal-hold", S3Transforms.ToStringValue(putObjectRequest.ObjectLockLegalHoldStatus));
            }

            if (putObjectRequest.IsSetObjectLockMode())
            {
                request.Headers.Add("x-amz-object-lock-mode", S3Transforms.ToStringValue(putObjectRequest.ObjectLockMode));
            }

            if (putObjectRequest.IsSetObjectLockRetainUntilDate())
            {
                request.Headers.Add("x-amz-object-lock-retain-until-date", S3Transforms.ToStringValue(putObjectRequest.ObjectLockRetainUntilDate, AWSSDKUtils.ISO8601DateFormat));
            }

            if (putObjectRequest.IsSetRequestPayer())
            {
                request.Headers.Add(S3Constants.AmzHeaderRequestPayer, S3Transforms.ToStringValue(putObjectRequest.RequestPayer.ToString()));
            }

            if (putObjectRequest.IsSetTagSet())
            {
                request.Headers.Add(S3Constants.AmzHeaderTagging, AmazonS3Util.TagSetToQueryString(putObjectRequest.TagSet));
            }

            AmazonS3Util.SetMetadataHeaders(request, putObjectRequest.Metadata);

            if (string.IsNullOrEmpty(putObjectRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "PutObjectRequest.BucketName");
            }
            if (string.IsNullOrEmpty(putObjectRequest.Key))
            {
                throw new System.ArgumentException("Key is a required property and must be set before making this call.", "PutObjectRequest.Key");
            }

            request.ResourcePath = string.Format(CultureInfo.InvariantCulture, "/{0}/{1}",
                                                 S3Transforms.ToStringValue(putObjectRequest.BucketName),
                                                 S3Transforms.ToStringValue(putObjectRequest.Key));


            if (putObjectRequest.InputStream != null)
            {
                // Wrap the stream in a stream that has a length
                var streamWithLength = GetStreamWithLength(putObjectRequest.InputStream, putObjectRequest.Headers.ContentLength);
                if (streamWithLength.Length > 0)
                {
                    request.UseChunkEncoding = putObjectRequest.UseChunkEncoding;
                }
                var length = streamWithLength.Length - streamWithLength.Position;
                if (!request.Headers.ContainsKey(HeaderKeys.ContentLengthHeader))
                {
                    request.Headers.Add(HeaderKeys.ContentLengthHeader, length.ToString(CultureInfo.InvariantCulture));
                }

                // Wrap input stream in MD5Stream
                var hashStream = new MD5Stream(streamWithLength, null, length);
                putObjectRequest.InputStream = hashStream;
            }

            request.ContentStream = putObjectRequest.InputStream;
            if (!request.Headers.ContainsKey(HeaderKeys.ContentTypeHeader))
            {
                request.Headers.Add(HeaderKeys.ContentTypeHeader, "text/plain");
            }

            return(request);
        }
Exemple #18
0
        public IRequest Marshall(PutCORSConfigurationRequest putCORSConfigurationRequest)
        {
            IRequest request = new DefaultRequest(putCORSConfigurationRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            request.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(putCORSConfigurationRequest.BucketName));

            request.AddSubResource("cors");

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                var configuration = putCORSConfigurationRequest.Configuration;
                if (configuration != null)
                {
                    xmlWriter.WriteStartElement("CORSConfiguration", "");

                    if (configuration != null)
                    {
                        var cORSConfigurationCORSConfigurationcORSRulesList = configuration.Rules;
                        if (cORSConfigurationCORSConfigurationcORSRulesList != null && cORSConfigurationCORSConfigurationcORSRulesList.Count > 0)
                        {
                            foreach (var cORSConfigurationCORSConfigurationcORSRulesListValue in cORSConfigurationCORSConfigurationcORSRulesList)
                            {
                                xmlWriter.WriteStartElement("CORSRule", "");

                                if (cORSConfigurationCORSConfigurationcORSRulesListValue != null)
                                {
                                    var cORSRuleMemberallowedMethodsList = cORSConfigurationCORSConfigurationcORSRulesListValue.AllowedMethods;
                                    if (cORSRuleMemberallowedMethodsList != null && cORSRuleMemberallowedMethodsList.Count > 0)
                                    {
                                        foreach (string cORSRuleMemberallowedMethodsListValue in cORSRuleMemberallowedMethodsList)
                                        {
                                            xmlWriter.WriteStartElement("AllowedMethod", "");
                                            xmlWriter.WriteValue(cORSRuleMemberallowedMethodsListValue);
                                            xmlWriter.WriteEndElement();
                                        }
                                    }
                                }

                                if (cORSConfigurationCORSConfigurationcORSRulesListValue != null)
                                {
                                    var cORSRuleMemberallowedOriginsList = cORSConfigurationCORSConfigurationcORSRulesListValue.AllowedOrigins;
                                    if (cORSRuleMemberallowedOriginsList != null && cORSRuleMemberallowedOriginsList.Count > 0)
                                    {
                                        foreach (string cORSRuleMemberallowedOriginsListValue in cORSRuleMemberallowedOriginsList)
                                        {
                                            xmlWriter.WriteStartElement("AllowedOrigin", "");
                                            xmlWriter.WriteValue(cORSRuleMemberallowedOriginsListValue);
                                            xmlWriter.WriteEndElement();
                                        }
                                    }
                                }

                                if (cORSConfigurationCORSConfigurationcORSRulesListValue != null)
                                {
                                    var cORSRuleMemberexposeHeadersList = cORSConfigurationCORSConfigurationcORSRulesListValue.ExposeHeaders;
                                    if (cORSRuleMemberexposeHeadersList != null && cORSRuleMemberexposeHeadersList.Count > 0)
                                    {
                                        foreach (string cORSRuleMemberexposeHeadersListValue in cORSRuleMemberexposeHeadersList)
                                        {
                                            xmlWriter.WriteStartElement("ExposeHeader", "");
                                            xmlWriter.WriteValue(cORSRuleMemberexposeHeadersListValue);
                                            xmlWriter.WriteEndElement();
                                        }
                                    }
                                }

                                if (cORSConfigurationCORSConfigurationcORSRulesListValue != null)
                                {
                                    var cORSRuleMemberallowedHeadersList = cORSConfigurationCORSConfigurationcORSRulesListValue.AllowedHeaders;
                                    if (cORSRuleMemberallowedHeadersList != null && cORSRuleMemberallowedHeadersList.Count > 0)
                                    {
                                        foreach (string cORSRuleMemberallowedHeadersListValue in cORSRuleMemberallowedHeadersList)
                                        {
                                            xmlWriter.WriteStartElement("AllowedHeader", "");
                                            xmlWriter.WriteValue(cORSRuleMemberallowedHeadersListValue);
                                            xmlWriter.WriteEndElement();
                                        }
                                    }
                                }

                                if (cORSConfigurationCORSConfigurationcORSRulesListValue.IsSetMaxAgeSeconds())
                                {
                                    xmlWriter.WriteElementString("MaxAgeSeconds", "", S3Transforms.ToXmlStringValue(cORSConfigurationCORSConfigurationcORSRulesListValue.MaxAgeSeconds));
                                }

                                if (cORSConfigurationCORSConfigurationcORSRulesListValue.IsSetId())
                                {
                                    xmlWriter.WriteElementString("ID", "", S3Transforms.ToXmlStringValue(cORSConfigurationCORSConfigurationcORSRulesListValue.Id));
                                }

                                xmlWriter.WriteEndElement();
                            }
                        }
                    }
                    xmlWriter.WriteEndElement();
                }
            }


            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
        public IRequest Marshall(PutBucketVersioningRequest putBucketVersioningRequest)
        {
            IRequest request = new DefaultRequest(putBucketVersioningRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putBucketVersioningRequest.IsSetMfaCodes())
            {
                request.Headers.Add(HeaderKeys.XAmzMfaHeader, putBucketVersioningRequest.MfaCodes.FormattedMfaCodes);
            }

            if (putBucketVersioningRequest.IsSetExpectedBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedBucketOwner, S3Transforms.ToStringValue(putBucketVersioningRequest.ExpectedBucketOwner));
            }

            if (string.IsNullOrEmpty(putBucketVersioningRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "PutBucketVersioningRequest.BucketName");
            }

            request.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(putBucketVersioningRequest.BucketName));

            request.AddSubResource("versioning");

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                var versioningConfigurationVersioningConfiguration = putBucketVersioningRequest.VersioningConfig;
                if (versioningConfigurationVersioningConfiguration != null)
                {
                    xmlWriter.WriteStartElement("VersioningConfiguration", "");
                    if (versioningConfigurationVersioningConfiguration.IsSetEnableMfaDelete())
                    {
                        xmlWriter.WriteElementString("MfaDelete", "", versioningConfigurationVersioningConfiguration.EnableMfaDelete ? "Enabled" : "Disabled");
                    }
                    if (versioningConfigurationVersioningConfiguration.IsSetStatus())
                    {
                        xmlWriter.WriteElementString("Status", "", S3Transforms.ToXmlStringValue(versioningConfigurationVersioningConfiguration.Status));
                    }
                    xmlWriter.WriteEndElement();
                }
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AWSSDKUtils.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Exemple #20
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, GetObjectResponse response)
        {
            response.ResponseStream = context.Stream;

            IWebResponseData responseData = context.ResponseData;

            if (responseData.IsHeaderPresent("x-amz-delete-marker"))
            {
                response.DeleteMarker = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-delete-marker"));
            }
            if (responseData.IsHeaderPresent("accept-ranges"))
            {
                response.AcceptRanges = S3Transforms.ToString(responseData.GetHeaderValue("accept-ranges"));
            }
            if (context.ResponseData.IsHeaderPresent("content-range"))
            {
                response.ContentRange = S3Transforms.ToString(responseData.GetHeaderValue("content-range"));
            }
            if (responseData.IsHeaderPresent("x-amz-expiration"))
            {
                response.Expiration = new Expiration(responseData.GetHeaderValue("x-amz-expiration"));
            }
            if (responseData.IsHeaderPresent("x-amz-restore"))
            {
                bool     restoreInProgress;
                DateTime?restoreExpiration;
                AmazonS3Util.ParseAmzRestoreHeader(responseData.GetHeaderValue("x-amz-restore"), out restoreInProgress, out restoreExpiration);

                response.RestoreInProgress = restoreInProgress;
                response.RestoreExpiration = restoreExpiration;
            }
            if (responseData.IsHeaderPresent("Last-Modified"))
            {
                response.LastModified = S3Transforms.ToDateTime(responseData.GetHeaderValue("Last-Modified"));
            }
            if (responseData.IsHeaderPresent("ETag"))
            {
                response.ETag = S3Transforms.ToString(responseData.GetHeaderValue("ETag"));
            }
            if (responseData.IsHeaderPresent("x-amz-missing-meta"))
            {
                response.MissingMeta = S3Transforms.ToInt(responseData.GetHeaderValue("x-amz-missing-meta"));
            }
            if (responseData.IsHeaderPresent("x-amz-version-id"))
            {
                response.VersionId = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-version-id"));
            }
            if (responseData.IsHeaderPresent("Cache-Control"))
            {
                response.Headers.CacheControl = S3Transforms.ToString(responseData.GetHeaderValue("Cache-Control"));
            }
            if (responseData.IsHeaderPresent("Content-Disposition"))
            {
                response.Headers.ContentDisposition = S3Transforms.ToString(responseData.GetHeaderValue("Content-Disposition"));
            }
            if (responseData.IsHeaderPresent("Content-Encoding"))
            {
                response.Headers.ContentEncoding = S3Transforms.ToString(responseData.GetHeaderValue("Content-Encoding"));
            }
            if (responseData.IsHeaderPresent("Content-Length"))
            {
                response.Headers.ContentLength = long.Parse(responseData.GetHeaderValue("Content-Length"), CultureInfo.InvariantCulture);
            }
            if (responseData.IsHeaderPresent("x-amz-object-lock-legal-hold"))
            {
                response.ObjectLockLegalHoldStatus = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-object-lock-legal-hold"));
            }
            if (responseData.IsHeaderPresent("x-amz-object-lock-mode"))
            {
                response.ObjectLockMode = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-object-lock-mode"));
            }
            if (responseData.IsHeaderPresent("x-amz-object-lock-retain-until-date"))
            {
                response.ObjectLockRetainUntilDate = S3Transforms.ToDateTime(responseData.GetHeaderValue("x-amz-object-lock-retain-until-date"));
            }
            if (responseData.IsHeaderPresent("Content-Type"))
            {
                response.Headers.ContentType = S3Transforms.ToString(responseData.GetHeaderValue("Content-Type"));
            }
            if (responseData.IsHeaderPresent("Expires"))
            {
                response.RawExpires = S3Transforms.ToString(responseData.GetHeaderValue("Expires"));
            }
            if (responseData.IsHeaderPresent("x-amz-website-redirect-location"))
            {
                response.WebsiteRedirectLocation = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-website-redirect-location"));
            }
            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-customer-algorithm"))
            {
                response.ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.FindValue(responseData.GetHeaderValue("x-amz-server-side-encryption-customer-algorithm"));
            }
            if (responseData.IsHeaderPresent(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader))
            {
                response.ServerSideEncryptionKeyManagementServiceKeyId = S3Transforms.ToString(responseData.GetHeaderValue(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader));
            }
            if (responseData.IsHeaderPresent("x-amz-replication-status"))
            {
                response.ReplicationStatus = S3Transforms.ToString(responseData.GetHeaderValue("x-amz-replication-status"));
            }
            if (responseData.IsHeaderPresent(S3Constants.AmzHeaderMultipartPartsCount))
            {
                response.PartsCount = S3Transforms.ToInt(responseData.GetHeaderValue(S3Constants.AmzHeaderMultipartPartsCount));
            }
            if (responseData.IsHeaderPresent(HeaderKeys.XAmzStorageClassHeader))
            {
                response.StorageClass = S3Transforms.ToString(responseData.GetHeaderValue(HeaderKeys.XAmzStorageClassHeader));
            }
            if (responseData.IsHeaderPresent(S3Constants.AmzHeaderRequestCharged))
            {
                response.RequestCharged = RequestCharged.FindValue(responseData.GetHeaderValue(S3Constants.AmzHeaderRequestCharged));
            }
            if (responseData.IsHeaderPresent(S3Constants.AmzHeaderTaggingCount))
            {
                response.TagCount = S3Transforms.ToInt(responseData.GetHeaderValue(S3Constants.AmzHeaderTaggingCount));
            }

            foreach (var name in responseData.GetHeaderNames())
            {
                if (name.StartsWith("x-amz-meta-", StringComparison.OrdinalIgnoreCase))
                {
                    response.Metadata[name] = AWSConfigsS3.EnableUnicodeEncodingForObjectMetadata
                        ? Uri.UnescapeDataString(responseData.GetHeaderValue(name)) : responseData.GetHeaderValue(name);
                }
            }

            return;
        }
        /// <summary>
        /// Marshaller the request object to the HTTP request.
        /// </summary>
        /// <param name="publicRequest"></param>
        /// <returns></returns>
        public IRequest Marshall(PutObjectRetentionRequest publicRequest)
        {
            var request = new DefaultRequest(publicRequest, "AmazonS3");

            request.HttpMethod = "PUT";
            string uriResourcePath = "/{Bucket}/{Key+}";

            request.AddSubResource("retention");

            if (publicRequest.IsSetBypassGovernanceRetention())
            {
                request.Headers.Add("x-amz-bypass-governance-retention", S3Transforms.ToStringValue(publicRequest.BypassGovernanceRetention));
            }
            if (publicRequest.IsSetContentMD5())
            {
                request.Headers.Add(HeaderKeys.ContentMD5Header, S3Transforms.ToStringValue(publicRequest.ContentMD5));
            }
            if (publicRequest.IsSetRequestPayer())
            {
                request.Headers.Add(S3Constants.AmzHeaderRequestPayer, S3Transforms.ToStringValue(publicRequest.RequestPayer.ToString()));
            }
            if (publicRequest.IsSetExpectedBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedBucketOwner, S3Transforms.ToStringValue(publicRequest.ExpectedBucketOwner));
            }
            if (!publicRequest.IsSetBucketName())
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "publicRequest.BucketName");
            }
            uriResourcePath = uriResourcePath.Replace("{Bucket}", StringUtils.FromString(publicRequest.BucketName));
            if (!publicRequest.IsSetKey())
            {
                throw new System.ArgumentException("Key is a required property and must be set before making this call.", "publicRequest.Key");
            }
            uriResourcePath = uriResourcePath.Replace("{Key+}", StringUtils.FromString(publicRequest.Key));

            if (publicRequest.IsSetVersionId())
            {
                request.Parameters.Add("versionId", StringUtils.FromString(publicRequest.VersionId));
            }
            request.MarshallerVersion = 2;
            request.ResourcePath      = uriResourcePath;

            var stringWriter = new StringWriter(CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = System.Text.Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                if (publicRequest.IsSetRetention())
                {
                    xmlWriter.WriteStartElement("Retention", "http://s3.amazonaws.com/doc/2006-03-01/");
                    if (publicRequest.Retention.IsSetMode())
                    {
                        xmlWriter.WriteElementString("Mode", "http://s3.amazonaws.com/doc/2006-03-01/", StringUtils.FromString(publicRequest.Retention.Mode));
                    }

                    if (publicRequest.Retention.IsSetRetainUntilDate())
                    {
                        xmlWriter.WriteElementString("RetainUntilDate", "http://s3.amazonaws.com/doc/2006-03-01/", StringUtils.FromDateTimeToISO8601(publicRequest.Retention.RetainUntilDate));
                    }


                    xmlWriter.WriteEndElement();
                }
            }
            try
            {
                string content = stringWriter.ToString();
                request.Content = System.Text.Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            request.UseQueryString = true;
            return(request);
        }
Exemple #22
0
        public IRequest Marshall(PutBucketRequest putBucketRequest)
        {
            IRequest request = new DefaultRequest(putBucketRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putBucketRequest.IsSetCannedACL())
            {
                request.Headers.Add(HeaderKeys.XAmzAclHeader, putBucketRequest.CannedACL.Value);
            }
            else if (putBucketRequest.Grants != null && putBucketRequest.Grants.Count > 0)
            {
                ConvertPutWithACLRequest(putBucketRequest, request);
            }

            if (putBucketRequest.IsSetObjectLockEnabledForBucket())
            {
                request.Headers.Add("x-amz-bucket-object-lock-enabled", S3Transforms.ToStringValue(putBucketRequest.ObjectLockEnabledForBucket));
            }

            if (string.IsNullOrEmpty(putBucketRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "PutBucketRequest.BucketName");
            }

            request.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(putBucketRequest.BucketName));

            var stringWriter = new StringWriter(CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                string regionCode = null;
                var    region     = putBucketRequest.BucketRegion;
                if (region != null && !string.IsNullOrEmpty(region.Value))
                {
                    regionCode = region.Value;
                }
                else if (!string.IsNullOrEmpty(putBucketRequest.BucketRegionName))
                {
                    if (putBucketRequest.BucketRegionName == "eu-west-1")
                    {
                        regionCode = "EU";
                    }
                    else if (putBucketRequest.BucketRegionName != "us-east-1")
                    {
                        regionCode = putBucketRequest.BucketRegionName;
                    }
                }

                if (regionCode != null)
                {
                    xmlWriter.WriteStartElement("CreateBucketConfiguration", "");
                    xmlWriter.WriteElementString("LocationConstraint", "", regionCode);
                    xmlWriter.WriteEndElement();
                }
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AWSSDKUtils.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
        public IRequest Marshall(PutBucketIntelligentTieringConfigurationRequest PutBucketIntelligentTieringConfigurationRequest)
        {
            IRequest request = new DefaultRequest(PutBucketIntelligentTieringConfigurationRequest, "AmazonS3");
            var      intelligentTieringConfiguration = PutBucketIntelligentTieringConfigurationRequest.IntelligentTieringConfiguration;

            request.HttpMethod = "PUT";

            if (string.IsNullOrEmpty(PutBucketIntelligentTieringConfigurationRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "PutBucketIntelligentTieringConfigurationRequest.BucketName");
            }

            if (intelligentTieringConfiguration == null)
            {
                throw new System.ArgumentException("IntelligentTieringConfiguration is a required property and must be set before making this call.", "PutBucketIntelligentTieringConfigurationRequest.IntelligentTieringConfiguration");
            }

            if (string.IsNullOrEmpty(intelligentTieringConfiguration.IntelligentTieringId))
            {
                throw new System.ArgumentException("IntelligentTieringId is a required property and must be set before making this call.", "IntelligentTieringConfiguration.IntelligentTieringId");
            }

            if (string.IsNullOrEmpty(PutBucketIntelligentTieringConfigurationRequest.IntelligentTieringId))
            {
                throw new System.ArgumentException("IntelligentTieringId is a required property and must be set before making this call.", "PutBucketIntelligentTieringConfigurationRequest.IntelligentTieringId");
            }

            if (!intelligentTieringConfiguration.IsSetStatus())
            {
                throw new System.ArgumentException("Status is a required property and must be set before making this call.", "IntelligentTieringConfiguration.Status");
            }

            if (!intelligentTieringConfiguration.IsSetTieringList())
            {
                throw new System.ArgumentException("TieringList is a required property and must be set before making this call.", "IntelligentTieringConfiguration.TieringList");
            }

            request.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(PutBucketIntelligentTieringConfigurationRequest.BucketName));

            request.AddSubResource("intelligent-tiering");

            request.AddSubResource("id", PutBucketIntelligentTieringConfigurationRequest.IntelligentTieringId);

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                if (intelligentTieringConfiguration != null)
                {
                    xmlWriter.WriteStartElement("IntelligentTieringConfiguration", "http://s3.amazonaws.com/doc/2006-03-01/");
                    if (intelligentTieringConfiguration != null)
                    {
                        if (intelligentTieringConfiguration.IsSetIntelligentTieringId())
                        {
                            xmlWriter.WriteElementString("Id", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(intelligentTieringConfiguration.IntelligentTieringId));
                        }

                        if (intelligentTieringConfiguration.IsSetIntelligentTieringFilter())
                        {
                            xmlWriter.WriteStartElement("Filter", "http://s3.amazonaws.com/doc/2006-03-01/");
                            var filterPredicate = intelligentTieringConfiguration.IntelligentTieringFilter.IntelligentTieringFilterPredicate;
                            filterPredicate.Accept(new IntelligentTieringPredicateVisitor(xmlWriter));

                            xmlWriter.WriteEndElement();
                        }

                        if (intelligentTieringConfiguration.IsSetStatus())
                        {
                            xmlWriter.WriteElementString("Status", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(intelligentTieringConfiguration.Status));
                        }

                        if (intelligentTieringConfiguration.IsSetTieringList())
                        {
                            foreach (var tiering in intelligentTieringConfiguration.Tierings)
                            {
                                if (tiering != null)
                                {
                                    xmlWriter.WriteStartElement("Tiering", "http://s3.amazonaws.com/doc/2006-03-01/");
                                    xmlWriter.WriteElementString("Days", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(tiering.Days));
                                    xmlWriter.WriteElementString("AccessTier", "http://s3.amazonaws.com/doc/2006-03-01/", S3Transforms.ToXmlStringValue(tiering.AccessTier));
                                    xmlWriter.WriteEndElement();
                                }
                            }
                        }
                    }
                    xmlWriter.WriteEndElement();
                }
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AWSSDKUtils.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
        public IRequest Marshall(DeleteObjectsRequest deleteObjectsRequest)
        {
            IRequest request = new DefaultRequest(deleteObjectsRequest, "AmazonS3");

            request.HttpMethod = "POST";

            if (deleteObjectsRequest.IsSetMfaCodes())
            {
                request.Headers.Add(HeaderKeys.XAmzMfaHeader, deleteObjectsRequest.MfaCodes.FormattedMfaCodes);
            }

            request.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(deleteObjectsRequest.BucketName));

            request.AddSubResource("delete");

            var stringWriter = new StringWriter(CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                xmlWriter.WriteStartElement("Delete", "");

                var deleteDeleteobjectsList = deleteObjectsRequest.Objects;
                if (deleteDeleteobjectsList != null && deleteDeleteobjectsList.Count > 0)
                {
                    foreach (var deleteDeleteobjectsListValue in deleteDeleteobjectsList)
                    {
                        xmlWriter.WriteStartElement("Object", "");
                        if (deleteDeleteobjectsListValue.IsSetKey())
                        {
                            xmlWriter.WriteElementString("Key", "", S3Transforms.ToXmlStringValue(deleteDeleteobjectsListValue.Key));
                        }
                        if (deleteDeleteobjectsListValue.IsSetVersionId())
                        {
                            xmlWriter.WriteElementString("VersionId", "", S3Transforms.ToXmlStringValue(deleteDeleteobjectsListValue.VersionId));
                        }
                        xmlWriter.WriteEndElement();
                    }
                }
                if (deleteObjectsRequest.IsSetQuiet())
                {
                    xmlWriter.WriteElementString("Quiet", "", deleteObjectsRequest.Quiet.ToString().ToLowerInvariant());
                }
                xmlWriter.WriteEndElement();
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Exemple #25
0
        public IRequest Marshall(PutBucketWebsiteRequest putBucketWebsiteRequest)
        {
            IRequest request = new DefaultRequest(putBucketWebsiteRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            var uriResourcePath = string.Concat("/", S3Transforms.ToStringValue(putBucketWebsiteRequest.BucketName));

            request.Parameters.Add("website", null);

            request.CanonicalResource = S3Transforms.GetCanonicalResource(uriResourcePath, request.Parameters);
            request.ResourcePath      = S3Transforms.FormatResourcePath(uriResourcePath, request.Parameters);

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                var websiteConfigurationWebsiteConfiguration = putBucketWebsiteRequest.WebsiteConfiguration;
                if (websiteConfigurationWebsiteConfiguration != null)
                {
                    xmlWriter.WriteStartElement("WebsiteConfiguration", "");
                    if (websiteConfigurationWebsiteConfiguration != null)
                    {
                        string errorDocumentErrorDocument = websiteConfigurationWebsiteConfiguration.ErrorDocument;
                        if (errorDocumentErrorDocument != null)
                        {
                            xmlWriter.WriteStartElement("ErrorDocument", "");
                            xmlWriter.WriteElementString("Key", "", S3Transforms.ToXmlStringValue(errorDocumentErrorDocument));
                            xmlWriter.WriteEndElement();
                        }
                    }
                    if (websiteConfigurationWebsiteConfiguration != null)
                    {
                        string indexDocumentIndexDocument = websiteConfigurationWebsiteConfiguration.IndexDocumentSuffix;
                        if (indexDocumentIndexDocument != null)
                        {
                            xmlWriter.WriteStartElement("IndexDocument", "");
                            xmlWriter.WriteElementString("Suffix", "", S3Transforms.ToXmlStringValue(indexDocumentIndexDocument));
                            xmlWriter.WriteEndElement();
                        }
                    }
                    if (websiteConfigurationWebsiteConfiguration != null)
                    {
                        var redirectAllRequestsToRedirectAllRequestsTo = websiteConfigurationWebsiteConfiguration.RedirectAllRequestsTo;
                        if (redirectAllRequestsToRedirectAllRequestsTo != null)
                        {
                            xmlWriter.WriteStartElement("RedirectAllRequestsTo", "");
                            if (redirectAllRequestsToRedirectAllRequestsTo.IsSetHostName())
                            {
                                xmlWriter.WriteElementString("HostName", "", S3Transforms.ToXmlStringValue(redirectAllRequestsToRedirectAllRequestsTo.HostName));
                            }
                            if (redirectAllRequestsToRedirectAllRequestsTo.IsSetHttpRedirectCode())
                            {
                                xmlWriter.WriteElementString("HttpRedirectCode", "", S3Transforms.ToXmlStringValue(redirectAllRequestsToRedirectAllRequestsTo.HttpRedirectCode));
                            }
                            if (redirectAllRequestsToRedirectAllRequestsTo.IsSetProtocol())
                            {
                                xmlWriter.WriteElementString("Protocol", "", S3Transforms.ToXmlStringValue(redirectAllRequestsToRedirectAllRequestsTo.Protocol));
                            }
                            if (redirectAllRequestsToRedirectAllRequestsTo.IsSetReplaceKeyPrefixWith())
                            {
                                xmlWriter.WriteElementString("ReplaceKeyPrefixWith", "", S3Transforms.ToXmlStringValue(redirectAllRequestsToRedirectAllRequestsTo.ReplaceKeyPrefixWith));
                            }
                            if (redirectAllRequestsToRedirectAllRequestsTo.IsSetReplaceKeyWith())
                            {
                                xmlWriter.WriteElementString("ReplaceKeyWith", "", S3Transforms.ToXmlStringValue(redirectAllRequestsToRedirectAllRequestsTo.ReplaceKeyWith));
                            }
                            xmlWriter.WriteEndElement();
                        }
                    }

                    if (websiteConfigurationWebsiteConfiguration != null)
                    {
                        var websiteConfigurationWebsiteConfigurationroutingRulesList = websiteConfigurationWebsiteConfiguration.RoutingRules;
                        if (websiteConfigurationWebsiteConfigurationroutingRulesList != null && websiteConfigurationWebsiteConfigurationroutingRulesList.Count > 0)
                        {
                            xmlWriter.WriteStartElement("RoutingRules", "");
                            foreach (var websiteConfigurationWebsiteConfigurationroutingRulesListValue in websiteConfigurationWebsiteConfigurationroutingRulesList)
                            {
                                xmlWriter.WriteStartElement("RoutingRule", "");
                                if (websiteConfigurationWebsiteConfigurationroutingRulesListValue != null)
                                {
                                    var conditionCondition = websiteConfigurationWebsiteConfigurationroutingRulesListValue.Condition;
                                    if (conditionCondition != null)
                                    {
                                        xmlWriter.WriteStartElement("Condition", "");
                                        if (conditionCondition.IsSetHttpErrorCodeReturnedEquals())
                                        {
                                            xmlWriter.WriteElementString("HttpErrorCodeReturnedEquals", "", S3Transforms.ToXmlStringValue(conditionCondition.HttpErrorCodeReturnedEquals));
                                        }
                                        if (conditionCondition.IsSetKeyPrefixEquals())
                                        {
                                            xmlWriter.WriteElementString("KeyPrefixEquals", "", S3Transforms.ToXmlStringValue(conditionCondition.KeyPrefixEquals));
                                        }
                                        xmlWriter.WriteEndElement();
                                    }
                                }
                                if (websiteConfigurationWebsiteConfigurationroutingRulesListValue != null)
                                {
                                    var redirectRedirect = websiteConfigurationWebsiteConfigurationroutingRulesListValue.Redirect;
                                    if (redirectRedirect != null)
                                    {
                                        xmlWriter.WriteStartElement("Redirect", "");
                                        if (redirectRedirect.IsSetHostName())
                                        {
                                            xmlWriter.WriteElementString("HostName", "", S3Transforms.ToXmlStringValue(redirectRedirect.HostName));
                                        }
                                        if (redirectRedirect.IsSetHttpRedirectCode())
                                        {
                                            xmlWriter.WriteElementString("HttpRedirectCode", "", S3Transforms.ToXmlStringValue(redirectRedirect.HttpRedirectCode));
                                        }
                                        if (redirectRedirect.IsSetProtocol())
                                        {
                                            xmlWriter.WriteElementString("Protocol", "", S3Transforms.ToXmlStringValue(redirectRedirect.Protocol));
                                        }
                                        if (redirectRedirect.IsSetReplaceKeyPrefixWith())
                                        {
                                            xmlWriter.WriteElementString("ReplaceKeyPrefixWith", "", S3Transforms.ToXmlStringValue(redirectRedirect.ReplaceKeyPrefixWith));
                                        }
                                        if (redirectRedirect.IsSetReplaceKeyWith())
                                        {
                                            xmlWriter.WriteElementString("ReplaceKeyWith", "", S3Transforms.ToXmlStringValue(redirectRedirect.ReplaceKeyWith));
                                        }
                                        xmlWriter.WriteEndElement();
                                    }
                                }
                                xmlWriter.WriteEndElement();
                            }
                            xmlWriter.WriteEndElement();
                        }
                    }
                    xmlWriter.WriteEndElement();
                }
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers["Content-Type"] = "application/xml";

                request.Parameters[S3QueryParameter.ContentType.ToString()] = "application/xml";
                var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                request.Headers[Amazon.Util.AWSSDKUtils.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            if (!request.UseQueryString)
            {
                var queryString = Amazon.Util.AWSSDKUtils.GetParametersAsString(request.Parameters);
                if (!string.IsNullOrEmpty(queryString))
                {
                    request.ResourcePath = string.Concat(request.ResourcePath, request.ResourcePath.Contains("?") ? "&" : "?", queryString);
                }
            }

            return(request);
        }
Exemple #26
0
        public IRequest Marshall(CopyObjectRequest copyObjectRequest)
        {
            IRequest request = new DefaultRequest(copyObjectRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (copyObjectRequest.IsSetCannedACL())
            {
                request.Headers.Add(HeaderKeys.XAmzAclHeader, S3Transforms.ToStringValue(copyObjectRequest.CannedACL));
            }

            var headers = copyObjectRequest.Headers;

            foreach (var key in headers.Keys)
            {
                request.Headers[key] = headers[key];
            }

            HeaderACLRequestMarshaller.Marshall(request, copyObjectRequest);

            if (copyObjectRequest.IsSetSourceBucket())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceHeader, ConstructCopySourceHeaderValue(copyObjectRequest.SourceBucket, copyObjectRequest.SourceKey, copyObjectRequest.SourceVersionId));
            }

            if (copyObjectRequest.IsSetETagToMatch())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfMatchHeader, S3Transforms.ToStringValue(copyObjectRequest.ETagToMatch));
            }

            if (copyObjectRequest.IsSetModifiedSinceDate())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfModifiedSinceHeader, S3Transforms.ToStringValue(copyObjectRequest.ModifiedSinceDate));
            }

            if (copyObjectRequest.IsSetETagToNotMatch())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfNoneMatchHeader, S3Transforms.ToStringValue(copyObjectRequest.ETagToNotMatch));
            }

            if (copyObjectRequest.IsSetUnmodifiedSinceDate())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfUnmodifiedSinceHeader, S3Transforms.ToStringValue(copyObjectRequest.UnmodifiedSinceDate));
            }

            if (copyObjectRequest.IsSetTagSet())
            {
                request.Headers.Add(S3Constants.AmzHeaderTagging, AmazonS3Util.TagSetToQueryString(copyObjectRequest.TagSet));
                request.Headers.Add(S3Constants.AmzHeaderTaggingDirective, TaggingDirective.REPLACE.Value);
            }
            else
            {
                request.Headers.Add(S3Constants.AmzHeaderTaggingDirective, TaggingDirective.COPY.Value);
            }

            request.Headers.Add(HeaderKeys.XAmzMetadataDirectiveHeader, S3Transforms.ToStringValue(copyObjectRequest.MetadataDirective.ToString()));

            if (copyObjectRequest.IsSetServerSideEncryptionMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzServerSideEncryptionHeader, S3Transforms.ToStringValue(copyObjectRequest.ServerSideEncryptionMethod));
            }
            if (copyObjectRequest.IsSetServerSideEncryptionCustomerMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerAlgorithmHeader, copyObjectRequest.ServerSideEncryptionCustomerMethod);
            }
            if (copyObjectRequest.IsSetServerSideEncryptionCustomerProvidedKey())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyHeader, copyObjectRequest.ServerSideEncryptionCustomerProvidedKey);
                if (copyObjectRequest.IsSetServerSideEncryptionCustomerProvidedKeyMD5())
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, copyObjectRequest.ServerSideEncryptionCustomerProvidedKeyMD5);
                }
                else
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, AmazonS3Util.ComputeEncodedMD5FromEncodedString(copyObjectRequest.ServerSideEncryptionCustomerProvidedKey));
                }
            }
            if (copyObjectRequest.IsSetCopySourceServerSideEncryptionCustomerMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerAlgorithmHeader, copyObjectRequest.CopySourceServerSideEncryptionCustomerMethod);
            }
            if (copyObjectRequest.IsSetCopySourceServerSideEncryptionCustomerProvidedKey())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerKeyHeader, copyObjectRequest.CopySourceServerSideEncryptionCustomerProvidedKey);
                if (copyObjectRequest.IsSetCopySourceServerSideEncryptionCustomerProvidedKeyMD5())
                {
                    request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerKeyMD5Header, copyObjectRequest.CopySourceServerSideEncryptionCustomerProvidedKeyMD5);
                }
                else
                {
                    request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerKeyMD5Header, AmazonS3Util.ComputeEncodedMD5FromEncodedString(copyObjectRequest.CopySourceServerSideEncryptionCustomerProvidedKey));
                }
            }
            if (copyObjectRequest.IsSetServerSideEncryptionKeyManagementServiceKeyId())
            {
                request.Headers.Add(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader, copyObjectRequest.ServerSideEncryptionKeyManagementServiceKeyId);
            }

            if (copyObjectRequest.IsSetStorageClass())
            {
                request.Headers.Add(HeaderKeys.XAmzStorageClassHeader, S3Transforms.ToStringValue(copyObjectRequest.StorageClass));
            }

            if (copyObjectRequest.IsSetWebsiteRedirectLocation())
            {
                request.Headers.Add(HeaderKeys.XAmzWebsiteRedirectLocationHeader, S3Transforms.ToStringValue(copyObjectRequest.WebsiteRedirectLocation));
            }

            if (copyObjectRequest.IsSetRequestPayer())
            {
                request.Headers.Add(S3Constants.AmzHeaderRequestPayer, S3Transforms.ToStringValue(copyObjectRequest.RequestPayer.ToString()));
            }

            AmazonS3Util.SetMetadataHeaders(request, copyObjectRequest.Metadata);

            if (string.IsNullOrEmpty(copyObjectRequest.DestinationBucket))
            {
                throw new System.ArgumentException("DestinationBucket is a required property and must be set before making this call.", "CopyObjectRequest.DestinationBucket");
            }
            if (string.IsNullOrEmpty(copyObjectRequest.DestinationKey))
            {
                throw new System.ArgumentException("DestinationKey is a required property and must be set before making this call.", "CopyObjectRequest.DestinationKey");
            }

            var destinationKey = copyObjectRequest.DestinationKey.StartsWith("/", StringComparison.Ordinal)
                                    ? copyObjectRequest.DestinationKey.Substring(1)
                                    : copyObjectRequest.DestinationKey;

            request.ResourcePath = string.Format(CultureInfo.InvariantCulture, "/{0}/{1}",
                                                 S3Transforms.ToStringValue(copyObjectRequest.DestinationBucket),
                                                 S3Transforms.ToStringValue(destinationKey));


            request.UseQueryString = true;

            return(request);
        }
Exemple #27
0
        public IRequest Marshall(PutBucketNotificationRequest putBucketNotificationRequest)
        {
            IRequest request = new DefaultRequest(putBucketNotificationRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putBucketNotificationRequest.IsSetExpectedBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedBucketOwner, S3Transforms.ToStringValue(putBucketNotificationRequest.ExpectedBucketOwner));
            }

            if (string.IsNullOrEmpty(putBucketNotificationRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "PutBucketNotificationRequest.BucketName");
            }

            request.MarshallerVersion = 2;
            request.ResourcePath      = string.Concat("/", S3Transforms.ToStringValue(putBucketNotificationRequest.BucketName));

            request.AddSubResource("notification");

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = System.Text.Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                xmlWriter.WriteStartElement("NotificationConfiguration", "");

                if (putBucketNotificationRequest.IsSetTopicConfigurations())
                {
                    foreach (var topicConfiguartion in putBucketNotificationRequest.TopicConfigurations)
                    {
                        if (topicConfiguartion != null)
                        {
                            xmlWriter.WriteStartElement("TopicConfiguration", "");
                            if (topicConfiguartion.IsSetId())
                            {
                                xmlWriter.WriteElementString("Id", "", S3Transforms.ToXmlStringValue(topicConfiguartion.Id));
                            }
                            if (topicConfiguartion.IsSetTopic())
                            {
                                xmlWriter.WriteElementString("Topic", "", S3Transforms.ToXmlStringValue(topicConfiguartion.Topic));
                            }

                            WriteConfigurationCommon(xmlWriter, topicConfiguartion);

                            xmlWriter.WriteEndElement();
                        }
                    }
                }

                if (putBucketNotificationRequest.IsSetQueueConfigurations())
                {
                    foreach (var queueConfiguartion in putBucketNotificationRequest.QueueConfigurations)
                    {
                        if (queueConfiguartion != null)
                        {
                            xmlWriter.WriteStartElement("QueueConfiguration", "");
                            if (queueConfiguartion.IsSetId())
                            {
                                xmlWriter.WriteElementString("Id", "", S3Transforms.ToXmlStringValue(queueConfiguartion.Id));
                            }
                            if (queueConfiguartion.IsSetQueue())
                            {
                                xmlWriter.WriteElementString("Queue", "", S3Transforms.ToXmlStringValue(queueConfiguartion.Queue));
                            }

                            WriteConfigurationCommon(xmlWriter, queueConfiguartion);

                            xmlWriter.WriteEndElement();
                        }
                    }
                }


                if (putBucketNotificationRequest.IsSetLambdaFunctionConfigurations())
                {
                    foreach (var lambdaFunctionConfiguartion in putBucketNotificationRequest.LambdaFunctionConfigurations)
                    {
                        if (lambdaFunctionConfiguartion != null)
                        {
                            xmlWriter.WriteStartElement("CloudFunctionConfiguration", "");
                            if (lambdaFunctionConfiguartion.IsSetId())
                            {
                                xmlWriter.WriteElementString("Id", "", S3Transforms.ToXmlStringValue(lambdaFunctionConfiguartion.Id));
                            }
                            if (lambdaFunctionConfiguartion.IsSetFunctionArn())
                            {
                                xmlWriter.WriteElementString("CloudFunction", "", S3Transforms.ToXmlStringValue(lambdaFunctionConfiguartion.FunctionArn));
                            }

                            WriteConfigurationCommon(xmlWriter, lambdaFunctionConfiguartion);

                            xmlWriter.WriteEndElement();
                        }
                    }
                }

                xmlWriter.WriteEndElement();
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = System.Text.Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AWSSDKUtils.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
        public IRequest Marshall(PutLifecycleConfigurationRequest putLifecycleConfigurationRequest)
        {
            IRequest request = new DefaultRequest(putLifecycleConfigurationRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            request.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(putLifecycleConfigurationRequest.BucketName));

            request.AddSubResource("lifecycle");

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                var lifecycleConfigurationLifecycleConfiguration = putLifecycleConfigurationRequest.Configuration;
                if (lifecycleConfigurationLifecycleConfiguration != null)
                {
                    xmlWriter.WriteStartElement("LifecycleConfiguration", "");

                    if (lifecycleConfigurationLifecycleConfiguration != null)
                    {
                        var lifecycleConfigurationLifecycleConfigurationrulesList = lifecycleConfigurationLifecycleConfiguration.Rules;
                        if (lifecycleConfigurationLifecycleConfigurationrulesList != null && lifecycleConfigurationLifecycleConfigurationrulesList.Count > 0)
                        {
                            foreach (var lifecycleConfigurationLifecycleConfigurationrulesListValue in lifecycleConfigurationLifecycleConfigurationrulesList)
                            {
                                xmlWriter.WriteStartElement("Rule", "");
                                if (lifecycleConfigurationLifecycleConfigurationrulesListValue != null)
                                {
                                    var expiration = lifecycleConfigurationLifecycleConfigurationrulesListValue.Expiration;
                                    if (expiration != null)
                                    {
                                        xmlWriter.WriteStartElement("Expiration", "");
                                        if (expiration.IsSetDate())
                                        {
                                            xmlWriter.WriteElementString("Date", "", S3Transforms.ToXmlStringValue(expiration.Date));
                                        }
                                        if (expiration.IsSetDays())
                                        {
                                            xmlWriter.WriteElementString("Days", "", S3Transforms.ToXmlStringValue(expiration.Days));
                                        }
                                        xmlWriter.WriteEndElement();
                                    }

                                    var transitions = lifecycleConfigurationLifecycleConfigurationrulesListValue.Transitions;
                                    if (transitions != null && transitions.Count > 0)
                                    {
                                        foreach (var transition in transitions)
                                        {
                                            if (transition != null)
                                            {
                                                xmlWriter.WriteStartElement("Transition", "");
                                                if (transition.IsSetDate())
                                                {
                                                    xmlWriter.WriteElementString("Date", "", S3Transforms.ToXmlStringValue(transition.Date));
                                                }
                                                if (transition.IsSetDays())
                                                {
                                                    xmlWriter.WriteElementString("Days", "", S3Transforms.ToXmlStringValue(transition.Days));
                                                }
                                                if (transition.IsSetStorageClass())
                                                {
                                                    xmlWriter.WriteElementString("StorageClass", "", S3Transforms.ToXmlStringValue(transition.StorageClass));
                                                }
                                                xmlWriter.WriteEndElement();
                                            }
                                        }
                                    }

                                    var noncurrentVersionExpiration = lifecycleConfigurationLifecycleConfigurationrulesListValue.NoncurrentVersionExpiration;
                                    if (noncurrentVersionExpiration != null)
                                    {
                                        xmlWriter.WriteStartElement("NoncurrentVersionExpiration", "");
                                        if (noncurrentVersionExpiration.IsSetNoncurrentDays())
                                        {
                                            xmlWriter.WriteElementString("NoncurrentDays", "", S3Transforms.ToXmlStringValue(noncurrentVersionExpiration.NoncurrentDays));
                                        }
                                        xmlWriter.WriteEndElement();
                                    }

                                    var noncurrentVersionTransitions = lifecycleConfigurationLifecycleConfigurationrulesListValue.NoncurrentVersionTransitions;
                                    if (noncurrentVersionTransitions != null && noncurrentVersionTransitions.Count > 0)
                                    {
                                        foreach (var noncurrentVersionTransition in noncurrentVersionTransitions)
                                        {
                                            if (noncurrentVersionTransition != null)
                                            {
                                                xmlWriter.WriteStartElement("NoncurrentVersionTransition", "");
                                                if (noncurrentVersionTransition.IsSetNoncurrentDays())
                                                {
                                                    xmlWriter.WriteElementString("NoncurrentDays", "", S3Transforms.ToXmlStringValue(noncurrentVersionTransition.NoncurrentDays));
                                                }
                                                if (noncurrentVersionTransition.IsSetStorageClass())
                                                {
                                                    xmlWriter.WriteElementString("StorageClass", "", S3Transforms.ToXmlStringValue(noncurrentVersionTransition.StorageClass));
                                                }
                                                xmlWriter.WriteEndElement();
                                            }
                                        }
                                    }
                                }
                                if (lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetId())
                                {
                                    xmlWriter.WriteElementString("ID", "", S3Transforms.ToXmlStringValue(lifecycleConfigurationLifecycleConfigurationrulesListValue.Id));
                                }
                                if (lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetPrefix())
                                {
                                    xmlWriter.WriteElementString("Prefix", "", S3Transforms.ToXmlStringValue(lifecycleConfigurationLifecycleConfigurationrulesListValue.Prefix));
                                }
                                if (lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetStatus())
                                {
                                    xmlWriter.WriteElementString("Status", "", S3Transforms.ToXmlStringValue(lifecycleConfigurationLifecycleConfigurationrulesListValue.Status));
                                }
                                else
                                {
                                    xmlWriter.WriteElementString("Status", "", "Disabled");
                                }
                                xmlWriter.WriteEndElement();
                            }
                        }
                    }
                    xmlWriter.WriteEndElement();
                }
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
        public IRequest Marshall(CopyPartRequest copyPartRequest)
        {
            IRequest request        = new DefaultRequest(copyPartRequest, "AmazonS3");
            var      sourceKey      = AmazonS3Util.RemoveLeadingSlash(copyPartRequest.SourceKey);
            var      destinationKey = AmazonS3Util.RemoveLeadingSlash(copyPartRequest.DestinationKey);

            request.HttpMethod = "PUT";

            if (copyPartRequest.IsSetSourceBucket())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceHeader, ConstructCopySourceHeaderValue(copyPartRequest.SourceBucket, sourceKey, copyPartRequest.SourceVersionId));
            }

            if (copyPartRequest.IsSetETagToMatch())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfMatchHeader, AWSSDKUtils.Join(copyPartRequest.ETagToMatch));
            }

            if (copyPartRequest.IsSetETagToNotMatch())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfNoneMatchHeader, AWSSDKUtils.Join(copyPartRequest.ETagsToNotMatch));
            }

            if (copyPartRequest.IsSetModifiedSinceDate())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfModifiedSinceHeader, copyPartRequest.ModifiedSinceDate.ToUniversalTime().ToString(AWSSDKUtils.GMTDateFormat, CultureInfo.InvariantCulture));
            }

            if (copyPartRequest.IsSetUnmodifiedSinceDate())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceIfUnmodifiedSinceHeader, copyPartRequest.UnmodifiedSinceDate.ToUniversalTime().ToString(AWSSDKUtils.GMTDateFormat, CultureInfo.InvariantCulture));
            }

            if (copyPartRequest.IsSetServerSideEncryptionCustomerMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerAlgorithmHeader, copyPartRequest.ServerSideEncryptionCustomerMethod);
            }

            if (copyPartRequest.IsSetServerSideEncryptionCustomerProvidedKey())
            {
                request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyHeader, copyPartRequest.ServerSideEncryptionCustomerProvidedKey);
                if (copyPartRequest.IsSetServerSideEncryptionCustomerProvidedKeyMD5())
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, copyPartRequest.ServerSideEncryptionCustomerProvidedKeyMD5);
                }
                else
                {
                    request.Headers.Add(HeaderKeys.XAmzSSECustomerKeyMD5Header, AmazonS3Util.ComputeEncodedMD5FromEncodedString(copyPartRequest.ServerSideEncryptionCustomerProvidedKey));
                }
            }

            if (copyPartRequest.IsSetCopySourceServerSideEncryptionCustomerMethod())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerAlgorithmHeader, copyPartRequest.CopySourceServerSideEncryptionCustomerMethod);
            }

            if (copyPartRequest.IsSetCopySourceServerSideEncryptionCustomerProvidedKey())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerKeyHeader, copyPartRequest.CopySourceServerSideEncryptionCustomerProvidedKey);
                if (copyPartRequest.IsSetCopySourceServerSideEncryptionCustomerProvidedKeyMD5())
                {
                    request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerKeyMD5Header, copyPartRequest.CopySourceServerSideEncryptionCustomerProvidedKeyMD5);
                }
                else
                {
                    request.Headers.Add(HeaderKeys.XAmzCopySourceSSECustomerKeyMD5Header, AmazonS3Util.ComputeEncodedMD5FromEncodedString(copyPartRequest.CopySourceServerSideEncryptionCustomerProvidedKey));
                }
            }

            if (copyPartRequest.IsSetFirstByte() && copyPartRequest.IsSetLastByte())
            {
                request.Headers.Add(HeaderKeys.XAmzCopySourceRangeHeader, ConstructCopySourceRangeHeader(copyPartRequest.FirstByte, copyPartRequest.LastByte));
            }

            if (copyPartRequest.IsSetExpectedBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedBucketOwner, S3Transforms.ToStringValue(copyPartRequest.ExpectedBucketOwner));
            }

            if (copyPartRequest.IsSetExpectedSourceBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedSourceBucketOwner, S3Transforms.ToStringValue(copyPartRequest.ExpectedSourceBucketOwner));
            }

            if (string.IsNullOrEmpty(copyPartRequest.DestinationBucket))
            {
                throw new System.ArgumentException("DestinationBucket is a required property and must be set before making this call.", "CopyPartRequest.DestinationBucket");
            }

            if (string.IsNullOrEmpty(destinationKey))
            {
                throw new System.ArgumentException("DestinationKey is a required property and must be set before making this call.", "CopyPartRequest.DestinationKey");
            }

            request.ResourcePath = string.Format(CultureInfo.InvariantCulture, "/{0}/{1}",
                                                 S3Transforms.ToStringValue(copyPartRequest.DestinationBucket),
                                                 S3Transforms.ToStringValue(destinationKey));

            request.AddSubResource("partNumber", S3Transforms.ToStringValue(copyPartRequest.PartNumber));
            request.AddSubResource("uploadId", S3Transforms.ToStringValue(copyPartRequest.UploadId));

            request.UseQueryString = true;

            return(request);
        }
        public IRequest Marshall(PutBucketReplicationRequest putBucketreplicationRequest)
        {
            IRequest request = new DefaultRequest(putBucketreplicationRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            request.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(putBucketreplicationRequest.BucketName));

            request.AddSubResource("replication");

            var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true
            }))
            {
                var replicationConfiguration = putBucketreplicationRequest.Configuration;

                if (replicationConfiguration != null)
                {
                    xmlWriter.WriteStartElement("ReplicationConfiguration", "");
                    if (replicationConfiguration.Role != null)
                    {
                        xmlWriter.WriteElementString("Role", "", S3Transforms.ToXmlStringValue(replicationConfiguration.Role));
                    }
                    if (replicationConfiguration.Rules != null)
                    {
                        foreach (var rule in replicationConfiguration.Rules)
                        {
                            xmlWriter.WriteStartElement("Rule");
                            if (rule.IsSetId())
                            {
                                xmlWriter.WriteElementString("ID", "", S3Transforms.ToXmlStringValue(rule.Id));
                            }
                            if (rule.IsSetPrefix())
                            {
                                xmlWriter.WriteElementString("Prefix", "", S3Transforms.ToXmlStringValue(rule.Prefix));
                            }
                            else // Write an empty Prefix tag
                            {
                                xmlWriter.WriteElementString("Prefix", "", S3Transforms.ToXmlStringValue(""));
                            }

                            if (rule.IsSetStatus())
                            {
                                xmlWriter.WriteElementString("Status", "", S3Transforms.ToXmlStringValue(rule.Status.ToString()));
                            }
                            if (rule.IsSetDestination())
                            {
                                xmlWriter.WriteStartElement("Destination", "");
                                if (rule.Destination.IsSetBucketArn())
                                {
                                    xmlWriter.WriteElementString("Bucket", "", rule.Destination.BucketArn);
                                }
                                if (rule.Destination.IsSetStorageClass())
                                {
                                    xmlWriter.WriteElementString("StorageClass", "", rule.Destination.StorageClass);
                                }
                                xmlWriter.WriteEndElement();
                            }
                            xmlWriter.WriteEndElement();
                        }
                    }

                    xmlWriter.WriteEndElement();
                }
            }

            try
            {
                var content = stringWriter.ToString();
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
                request.Headers[HeaderKeys.ContentMD5Header] = checksum;
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }