Example #1
0
        public IRequest Marshall(RestoreObjectRequest restoreObjectRequest)
        {
            IRequest request = new DefaultRequest(restoreObjectRequest, "AmazonS3");

            request.HttpMethod = "POST";

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

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

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

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

            request.AddSubResource("restore");
            if (restoreObjectRequest.IsSetVersionId())
            {
                request.AddSubResource("versionId", S3Transforms.ToStringValue(restoreObjectRequest.VersionId));
            }

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

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                restoreObjectRequest.Marshall("RestoreRequest", xmlWriter);
            }

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

                ChecksumUtils.SetRequestChecksum(request, restoreObjectRequest.ChecksumAlgorithm, fallbackToMD5: false);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Example #2
0
        [DataRow(true, CoreChecksumAlgorithm.SHA256, "x-amz-checksum-crc32", "x-amz-checksum-crc32")] // Checksum specified but another was set, still calculate it anyway.
        public void TestRequestChecksumSelection(bool fallbackToMD5, CoreChecksumAlgorithm checksumAlgorithm, string originalHeaderKey, string expectedHeaderKey)
        {
            var mock    = new Mock <IRequest>();
            var headers = new Dictionary <string, string>();
            var request = mock.Object;

            mock.SetupGet(x => x.Headers).Returns(headers);
            mock.SetupGet(x => x.Content).Returns(Encoding.ASCII.GetBytes("foo"));

            if (!string.IsNullOrEmpty(originalHeaderKey))
            {
                headers.Add(originalHeaderKey, "foo");
            }

            ChecksumUtils.SetRequestChecksum(request, checksumAlgorithm.ToString(), fallbackToMD5);

            if (!string.IsNullOrEmpty(expectedHeaderKey))
            {
                Assert.IsTrue(request.Headers.ContainsKey(expectedHeaderKey));
                Assert.IsFalse(string.IsNullOrEmpty(request.Headers[expectedHeaderKey]));
            }
            else
            {
                Assert.AreEqual(0, request.Headers.Count);
            }
        }
Example #3
0
        public IRequest Marshall(PutBucketRequestPaymentRequest putBucketRequestPaymentRequest)
        {
            IRequest request = new DefaultRequest(putBucketRequestPaymentRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putBucketRequestPaymentRequest.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(putBucketRequestPaymentRequest.ChecksumAlgorithm));
            }

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

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

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

            request.AddSubResource("requestPayment");

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

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                var requestPaymentConfigurationRequestPaymentConfiguration = putBucketRequestPaymentRequest.RequestPaymentConfiguration;
                if (requestPaymentConfigurationRequestPaymentConfiguration != null)
                {
                    xmlWriter.WriteStartElement("RequestPaymentConfiguration", "");
                    if (requestPaymentConfigurationRequestPaymentConfiguration.IsSetPayer())
                    {
                        xmlWriter.WriteElementString("Payer", "", S3Transforms.ToXmlStringValue(requestPaymentConfigurationRequestPaymentConfiguration.Payer));
                    }
                    xmlWriter.WriteEndElement();
                }
            }

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

                ChecksumUtils.SetRequestChecksum(request, putBucketRequestPaymentRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Example #4
0
        /// <summary>
        /// Marshall PutObjectTaggingRequest into an http request.
        /// </summary>
        public IRequest Marshall(PutObjectTaggingRequest putObjectTaggingRequest)
        {
            IRequest request = new DefaultRequest(putObjectTaggingRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putObjectTaggingRequest.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(putObjectTaggingRequest.ChecksumAlgorithm));
            }
            if (putObjectTaggingRequest.IsSetExpectedBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedBucketOwner, S3Transforms.ToStringValue(putObjectTaggingRequest.ExpectedBucketOwner));
            }
            if (putObjectTaggingRequest.IsSetRequestPayer())
            {
                request.Headers.Add(S3Constants.AmzHeaderRequestPayer, S3Transforms.ToStringValue(putObjectTaggingRequest.RequestPayer));
            }

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

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

            if (putObjectTaggingRequest.IsSetVersionId())
            {
                request.AddSubResource("versionId", putObjectTaggingRequest.VersionId);
            }

            try
            {
                var content = AmazonS3Util.SerializeTaggingToXml(putObjectTaggingRequest.Tagging);
                request.Content = Encoding.UTF8.GetBytes(content);
                request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";

                ChecksumUtils.SetRequestChecksum(request, putObjectTaggingRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marhsall request to XML", e);
            }

            return(request);
        }
Example #5
0
        public IRequest Marshall(PutBucketPolicyRequest putBucketPolicyRequest)
        {
            IRequest request = new DefaultRequest(putBucketPolicyRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putBucketPolicyRequest.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(putBucketPolicyRequest.ChecksumAlgorithm));
            }
            if (putBucketPolicyRequest.IsSetContentMD5())
            {
                request.Headers.Add(HeaderKeys.ContentMD5Header, S3Transforms.ToStringValue(putBucketPolicyRequest.ContentMD5));
            }
            if (!request.Headers.ContainsKey(HeaderKeys.ContentTypeHeader))
            {
                request.Headers.Add(HeaderKeys.ContentTypeHeader, "text/plain");
            }
            if (putBucketPolicyRequest.IsSetConfirmRemoveSelfBucketAccess())
            {
                request.Headers.Add(HeaderKeys.ConfirmSelfBucketAccess, S3Transforms.ToStringValue(putBucketPolicyRequest.ConfirmRemoveSelfBucketAccess));
            }
            if (putBucketPolicyRequest.IsSetExpectedBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedBucketOwner, S3Transforms.ToStringValue(putBucketPolicyRequest.ExpectedBucketOwner));
            }

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

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

            request.AddSubResource("policy");

            request.ContentStream = new MemoryStream(Encoding.UTF8.GetBytes(putBucketPolicyRequest.Policy));

            ChecksumUtils.SetRequestChecksum(request, putBucketPolicyRequest.ChecksumAlgorithm);

            return(request);
        }
Example #6
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.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(publicRequest.ChecksumAlgorithm));
            }
            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.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));

            request.ResourcePath = uriResourcePath;

            var stringWriter = new XMLEncodedStringWriter(CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = System.Text.Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                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";

                ChecksumUtils.SetRequestChecksum(request, publicRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Example #7
0
        /// <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.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(publicRequest.ChecksumAlgorithm));
            }
            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.ResourcePath = uriResourcePath;

            var stringWriter = new XMLEncodedStringWriter(CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = System.Text.Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                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";

                ChecksumUtils.SetRequestChecksum(request, publicRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            request.UseQueryString = true;
            return(request);
        }
Example #8
0
        public IRequest Marshall(PutBucketWebsiteRequest putBucketWebsiteRequest)
        {
            IRequest request = new DefaultRequest(putBucketWebsiteRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putBucketWebsiteRequest.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(putBucketWebsiteRequest.ChecksumAlgorithm));
            }

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

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

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

            request.AddSubResource("website");

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

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                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[HeaderKeys.ContentTypeHeader] = "application/xml";

                ChecksumUtils.SetRequestChecksum(request, putBucketWebsiteRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Example #9
0
        public IRequest Marshall(PutACLRequest putObjectAclRequest)
        {
            IRequest request = new DefaultRequest(putObjectAclRequest, "AmazonS3");

            request.HttpMethod = "PUT";

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

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

            if (putObjectAclRequest.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(putObjectAclRequest.ChecksumAlgorithm));
            }

            if (string.IsNullOrEmpty(putObjectAclRequest.BucketName))
            {
                throw new System.ArgumentException("BucketName is a required property and must be set before making this call.", "PutACLRequest.BucketName");
            }
            //Not checking if Key is null or empty because PutAcl allows to put an ACL for both a Bucket or an Object. TODO: deprecate PutAcl and create two separate operations

            // if we are putting the acl onto the bucket, the keyname component will collapse to empty string
            request.ResourcePath = string.Format(CultureInfo.InvariantCulture, "/{0}/{1}",
                                                 S3Transforms.ToStringValue(putObjectAclRequest.BucketName),
                                                 S3Transforms.ToStringValue(putObjectAclRequest.Key));

            request.AddSubResource("acl");
            if (putObjectAclRequest.IsSetVersionId())
            {
                request.AddSubResource("versionId", S3Transforms.ToStringValue(putObjectAclRequest.VersionId));
            }

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

            using (
                var xmlWriter = XmlWriter.Create(stringWriter,
                                                 new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8,
                OmitXmlDeclaration = true,
                NewLineHandling = NewLineHandling.Entitize
            }))
            {
                var accessControlPolicyAccessControlPolicy = putObjectAclRequest.AccessControlList;
                if (accessControlPolicyAccessControlPolicy != null)
                {
                    xmlWriter.WriteStartElement("AccessControlPolicy", "");
                    var accessControlPolicyAccessControlPolicygrantsList = accessControlPolicyAccessControlPolicy.Grants;
                    if (accessControlPolicyAccessControlPolicygrantsList != null &&
                        accessControlPolicyAccessControlPolicygrantsList.Count > 0)
                    {
                        accessControlPolicyAccessControlPolicy.Marshall("AccessControlList", xmlWriter);

                        var ownerOwner = accessControlPolicyAccessControlPolicy.Owner;
                        if (ownerOwner != null)
                        {
                            xmlWriter.WriteStartElement("Owner", "");
                            if (ownerOwner.IsSetDisplayName())
                            {
                                xmlWriter.WriteElementString("DisplayName", "",
                                                             S3Transforms.ToXmlStringValue(ownerOwner.DisplayName));
                            }
                            if (ownerOwner.IsSetId())
                            {
                                xmlWriter.WriteElementString("ID", "", S3Transforms.ToXmlStringValue(ownerOwner.Id));
                            }
                            xmlWriter.WriteEndElement();
                        }
                    }

                    xmlWriter.WriteEndElement();
                }
            }

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

                ChecksumUtils.SetRequestChecksum(request, putObjectAclRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

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

            request.HttpMethod = "PUT";

            if (putCORSConfigurationRequest.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(putCORSConfigurationRequest.ChecksumAlgorithm));
            }

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

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

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

            request.AddSubResource("cors");

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

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                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";

                ChecksumUtils.SetRequestChecksum(request, putCORSConfigurationRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

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

            request.HttpMethod = "PUT";

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

            if (putLifecycleConfigurationRequest.IsSetChecksumAlgorithm())
            {
                request.Headers[S3Constants.AmzHeaderSdkChecksumAlgorithm] = S3Transforms.ToStringValue(putLifecycleConfigurationRequest.ChecksumAlgorithm);
            }

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

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

            request.AddSubResource("lifecycle");

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

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                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.IsSetDateUtc())
                                        {
                                            xmlWriter.WriteElementString("Date", "", S3Transforms.ToXmlStringValue(expiration.DateUtc));
                                        }
                                        if (expiration.IsSetDays())
                                        {
                                            xmlWriter.WriteElementString("Days", "", S3Transforms.ToXmlStringValue(expiration.Days));
                                        }
                                        if (expiration.IsSetExpiredObjectDeleteMarker())
                                        {
                                            xmlWriter.WriteElementString("ExpiredObjectDeleteMarker", "", S3Transforms.ToXmlStringValue(expiration.ExpiredObjectDeleteMarker));
                                        }
                                        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.IsSetDateUtc())
                                                {
                                                    xmlWriter.WriteElementString("Date", "", S3Transforms.ToXmlStringValue(transition.DateUtc));
                                                }
                                                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();
                                            }
                                        }
                                    }

                                    var abortIncompleteMultipartUpload = lifecycleConfigurationLifecycleConfigurationrulesListValue.AbortIncompleteMultipartUpload;
                                    if (abortIncompleteMultipartUpload != null)
                                    {
                                        xmlWriter.WriteStartElement("AbortIncompleteMultipartUpload", "");
                                        if (abortIncompleteMultipartUpload.IsSetDaysAfterInitiation())
                                        {
                                            xmlWriter.WriteElementString("DaysAfterInitiation", "", S3Transforms.ToXmlStringValue(abortIncompleteMultipartUpload.DaysAfterInitiation));
                                        }
                                        xmlWriter.WriteEndElement();
                                    }
                                }
                                if (lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetId())
                                {
                                    xmlWriter.WriteElementString("ID", "", S3Transforms.ToXmlStringValue(lifecycleConfigurationLifecycleConfigurationrulesListValue.Id));
                                }

                                if (lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetPrefix() &&
                                    lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetFilter())
                                {
                                    throw new AmazonClientException("LifecycleRule.Prefix is deprecated.  Please only use LifecycleRule.Filter.");
                                }

                                if (lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetPrefix())
                                {
                                    xmlWriter.WriteElementString("Prefix", "", S3Transforms.ToXmlStringValue(lifecycleConfigurationLifecycleConfigurationrulesListValue.Prefix));
                                }

                                if (lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetFilter())
                                {
                                    xmlWriter.WriteStartElement("Filter", "");
                                    if (lifecycleConfigurationLifecycleConfigurationrulesListValue.Filter.IsSetLifecycleFilterPredicate())
                                    {
                                        lifecycleConfigurationLifecycleConfigurationrulesListValue.Filter.LifecycleFilterPredicate.Accept(new LifecycleFilterPredicateMarshallVisitor(xmlWriter));
                                    }
                                    xmlWriter.WriteEndElement();
                                }

                                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";

                ChecksumUtils.SetRequestChecksum(request, putLifecycleConfigurationRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Example #12
0
        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));
            }

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

            if (putObjectRequest.IsSetBucketKeyEnabled())
            {
                request.Headers.Add(S3Constants.AmzHeaderBucketKeyEnabled, S3Transforms.ToStringValue(putObjectRequest.BucketKeyEnabled));
            }

            if (putObjectRequest.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(putObjectRequest.ChecksumAlgorithm));
            }

            if (putObjectRequest.IsSetChecksumCRC32())
            {
                request.Headers.Add("x-amz-checksum-crc32", S3Transforms.ToStringValue(putObjectRequest.ChecksumCRC32));
            }

            if (putObjectRequest.IsSetChecksumCRC32C())
            {
                request.Headers.Add("x-amz-checksum-crc32c", S3Transforms.ToStringValue(putObjectRequest.ChecksumCRC32C));
            }

            if (putObjectRequest.IsSetChecksumSHA1())
            {
                request.Headers.Add("x-amz-checksum-sha1", S3Transforms.ToStringValue(putObjectRequest.ChecksumSHA1));
            }

            if (putObjectRequest.IsSetChecksumSHA256())
            {
                request.Headers.Add("x-amz-checksum-sha256", S3Transforms.ToStringValue(putObjectRequest.ChecksumSHA256));
            }

            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 && !(putObjectRequest.DisablePayloadSigning ?? false))
                {
                    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));
                }

                request.DisablePayloadSigning = putObjectRequest.DisablePayloadSigning;

                // Calculate Content-MD5 if not already set
                if (!putObjectRequest.IsSetMD5Digest() && putObjectRequest.CalculateContentMD5Header)
                {
                    string md5 = AmazonS3Util.GenerateMD5ChecksumForStream(putObjectRequest.InputStream);

                    if (!string.IsNullOrEmpty(md5))
                    {
                        request.Headers[HeaderKeys.ContentMD5Header] = md5;
                    }
                }

                if (!(putObjectRequest.DisableMD5Stream ?? AWSConfigsS3.DisableMD5Stream))
                {
                    // Wrap input stream in MD5Stream
                    var hashStream = new MD5Stream(streamWithLength, null, length);
                    putObjectRequest.InputStream = hashStream;
                }
                else
                {
                    putObjectRequest.InputStream = streamWithLength;
                }
            }

            request.ContentStream = putObjectRequest.InputStream;
            ChecksumUtils.SetRequestChecksum(request, putObjectRequest.ChecksumAlgorithm, fallbackToMD5: false);
            if (!request.Headers.ContainsKey(HeaderKeys.ContentTypeHeader))
            {
                request.Headers.Add(HeaderKeys.ContentTypeHeader, "text/plain");
            }

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

            request.HttpMethod = "POST";

            if (deleteObjectsRequest.IsSetBypassGovernanceRetention())
            {
                request.Headers.Add("x-amz-bypass-governance-retention", S3Transforms.ToStringValue(deleteObjectsRequest.BypassGovernanceRetention));
            }
            if (deleteObjectsRequest.IsSetMfaCodes())
            {
                request.Headers.Add(HeaderKeys.XAmzMfaHeader, deleteObjectsRequest.MfaCodes.FormattedMfaCodes);
            }
            if (deleteObjectsRequest.IsSetRequestPayer())
            {
                request.Headers.Add(S3Constants.AmzHeaderRequestPayer, S3Transforms.ToStringValue(deleteObjectsRequest.RequestPayer.ToString()));
            }
            if (deleteObjectsRequest.IsSetExpectedBucketOwner())
            {
                request.Headers.Add(S3Constants.AmzHeaderExpectedBucketOwner, S3Transforms.ToStringValue(deleteObjectsRequest.ExpectedBucketOwner));
            }
            if (deleteObjectsRequest.IsSetChecksumAlgorithm())
            {
                request.Headers[S3Constants.AmzHeaderSdkChecksumAlgorithm] = S3Transforms.ToStringValue(deleteObjectsRequest.ChecksumAlgorithm);
            }

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

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

            request.AddSubResource("delete");

            var stringWriter = new XMLEncodedStringWriter(CultureInfo.InvariantCulture);

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                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", "", S3Transforms.ToXmlStringValue(deleteObjectsRequest.Quiet));
                }
                xmlWriter.WriteEndElement();
            }

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

                ChecksumUtils.SetRequestChecksum(request, deleteObjectsRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

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

            request.HttpMethod = "PUT";

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

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

            request.AddSubResource("replication");

            if (putBucketreplicationRequest.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(putBucketreplicationRequest.ChecksumAlgorithm));
            }

            if (putBucketreplicationRequest.IsSetToken())
            {
                request.Headers.Add("x-amz-bucket-object-lock-token", putBucketreplicationRequest.Token);
            }

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

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

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                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.IsSetPriority())
                            {
                                xmlWriter.WriteElementString("Priority", "", S3Transforms.ToXmlStringValue(rule.Priority));
                            }
                            if (rule.IsSetPrefix())
                            {
                                xmlWriter.WriteElementString("Prefix", "", S3Transforms.ToXmlStringValue(rule.Prefix));
                            }

                            if (rule.IsSetFilter())
                            {
                                xmlWriter.WriteStartElement("Filter", "");
                                if (rule.Filter.IsSetPrefix())
                                {
                                    xmlWriter.WriteElementString("Prefix", "", S3Transforms.ToXmlStringValue(rule.Filter.Prefix));
                                }
                                if (rule.Filter.IsSetTag())
                                {
                                    rule.Filter.Tag.Marshall("Tag", xmlWriter);
                                }
                                if (rule.Filter.IsSetAnd())
                                {
                                    xmlWriter.WriteStartElement("And");
                                    if (rule.Filter.And.IsSetPrefix())
                                    {
                                        xmlWriter.WriteElementString("Prefix", "", S3Transforms.ToXmlStringValue(rule.Filter.And.Prefix));
                                    }
                                    if (rule.Filter.And.IsSetTags())
                                    {
                                        foreach (var tag in rule.Filter.And.Tags)
                                        {
                                            tag.Marshall("Tag", xmlWriter);
                                        }
                                    }
                                    xmlWriter.WriteEndElement();
                                }
                                xmlWriter.WriteEndElement();
                            }

                            if (rule.IsSetStatus())
                            {
                                xmlWriter.WriteElementString("Status", "", S3Transforms.ToXmlStringValue(rule.Status.ToString()));
                            }
                            if (rule.IsSetSourceSelectionCriteria())
                            {
                                xmlWriter.WriteStartElement("SourceSelectionCriteria");
                                if (rule.SourceSelectionCriteria.IsSetSseKmsEncryptedObjects())
                                {
                                    xmlWriter.WriteStartElement("SseKmsEncryptedObjects");
                                    if (rule.SourceSelectionCriteria.SseKmsEncryptedObjects.IsSetSseKmsEncryptedObjectsStatus())
                                    {
                                        xmlWriter.WriteElementString("Status", "", rule.SourceSelectionCriteria.SseKmsEncryptedObjects.SseKmsEncryptedObjectsStatus);
                                    }
                                    xmlWriter.WriteEndElement();
                                }
                                if (rule.SourceSelectionCriteria.IsSetReplicaModifications())
                                {
                                    xmlWriter.WriteStartElement("ReplicaModifications");
                                    if (rule.SourceSelectionCriteria.ReplicaModifications.IsSetStatus())
                                    {
                                        xmlWriter.WriteElementString("Status", "", rule.SourceSelectionCriteria.ReplicaModifications.Status);
                                    }
                                    xmlWriter.WriteEndElement();
                                }
                                xmlWriter.WriteEndElement();
                            }
                            if (rule.IsSetExistingObjectReplication())
                            {
                                xmlWriter.WriteStartElement("ExistingObjectReplication");
                                if (rule.ExistingObjectReplication.IsSetExistingObjectReplicationStatus())
                                {
                                    xmlWriter.WriteElementString("Status", "", rule.ExistingObjectReplication.Status);
                                }
                                xmlWriter.WriteEndElement();
                            }
                            if (rule.IsSetDeleteMarkerReplication())
                            {
                                xmlWriter.WriteStartElement("DeleteMarkerReplication");
                                if (rule.DeleteMarkerReplication.IsSetStatus())
                                {
                                    xmlWriter.WriteElementString("Status", "", rule.DeleteMarkerReplication.Status);
                                }
                                xmlWriter.WriteEndElement();
                            }
                            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);
                                }
                                if (rule.Destination.IsSetAccountId())
                                {
                                    xmlWriter.WriteElementString("Account", "", S3Transforms.ToXmlStringValue(rule.Destination.AccountId));
                                }
                                if (rule.Destination.IsSetEncryptionConfiguration())
                                {
                                    xmlWriter.WriteStartElement("EncryptionConfiguration");
                                    if (rule.Destination.EncryptionConfiguration.isSetReplicaKmsKeyID())
                                    {
                                        xmlWriter.WriteElementString("ReplicaKmsKeyID", "", S3Transforms.ToXmlStringValue(rule.Destination.EncryptionConfiguration.ReplicaKmsKeyID));
                                    }
                                    xmlWriter.WriteEndElement();
                                }
                                if (rule.Destination.IsSetMetrics())
                                {
                                    xmlWriter.WriteStartElement("Metrics");
                                    if (rule.Destination.Metrics.IsSetStatus())
                                    {
                                        xmlWriter.WriteElementString("Status", "", S3Transforms.ToXmlStringValue(rule.Destination.Metrics.Status));
                                    }
                                    if (rule.Destination.Metrics.IsSetEventThreshold())
                                    {
                                        xmlWriter.WriteStartElement("EventThreshold");
                                        if (rule.Destination.Metrics.EventThreshold.IsSetMinutes())
                                        {
                                            xmlWriter.WriteElementString("Minutes", "", S3Transforms.ToXmlStringValue(rule.Destination.Metrics.EventThreshold.Minutes));
                                        }
                                        xmlWriter.WriteEndElement();
                                    }
                                    xmlWriter.WriteEndElement();
                                }
                                if (rule.Destination.IsSetReplicationTime())
                                {
                                    xmlWriter.WriteStartElement("ReplicationTime");
                                    if (rule.Destination.ReplicationTime.IsSetStatus())
                                    {
                                        xmlWriter.WriteElementString("Status", "", S3Transforms.ToXmlStringValue(rule.Destination.ReplicationTime.Status));
                                    }
                                    if (rule.Destination.ReplicationTime.IsSetTime())
                                    {
                                        xmlWriter.WriteStartElement("Time");
                                        if (rule.Destination.ReplicationTime.Time.IsSetMinutes())
                                        {
                                            xmlWriter.WriteElementString("Minutes", "", S3Transforms.ToXmlStringValue(rule.Destination.ReplicationTime.Time.Minutes));
                                        }
                                        xmlWriter.WriteEndElement();
                                    }
                                    xmlWriter.WriteEndElement();
                                }
                                if (rule.Destination.IsSetAccessControlTranslation())
                                {
                                    xmlWriter.WriteStartElement("AccessControlTranslation");
                                    if (rule.Destination.AccessControlTranslation.IsSetOwner())
                                    {
                                        xmlWriter.WriteElementString("Owner", "", S3Transforms.ToXmlStringValue(rule.Destination.AccessControlTranslation.Owner));
                                    }
                                    xmlWriter.WriteEndElement();
                                }
                                xmlWriter.WriteEndElement();
                            }
                            xmlWriter.WriteEndElement();
                        }
                    }

                    xmlWriter.WriteEndElement();
                }
            }

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

                ChecksumUtils.SetRequestChecksum(request, putBucketreplicationRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Example #15
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 (putBucketNotificationRequest.IsSetSkipDestinationValidation())
            {
                request.Headers[S3Constants.AmzHeaderSkipDestinationValidation] = StringUtils.FromBool(putBucketNotificationRequest.SkipDestinationValidation);
            }

            if (putBucketNotificationRequest.IsSetChecksumAlgorithm())
            {
                request.Headers[S3Constants.AmzHeaderSdkChecksumAlgorithm] = S3Transforms.ToStringValue(putBucketNotificationRequest.ChecksumAlgorithm);
            }

            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.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(putBucketNotificationRequest.BucketName));

            request.AddSubResource("notification");

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

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = System.Text.Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                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();
                        }
                    }
                }

                if (putBucketNotificationRequest.IsSetEventBridgeConfiguration())
                {
                    xmlWriter.WriteStartElement("EventBridgeConfiguration", "http://s3.amazonaws.com/doc/2006-03-01/");
                    xmlWriter.WriteEndElement();
                }

                xmlWriter.WriteEndElement();
            }

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

                ChecksumUtils.SetRequestChecksum(request, putBucketNotificationRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Example #16
0
        public IRequest Marshall(PutBucketLoggingRequest putBucketLoggingRequest)
        {
            IRequest request = new DefaultRequest(putBucketLoggingRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (putBucketLoggingRequest.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(putBucketLoggingRequest.ChecksumAlgorithm));
            }

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

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

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

            request.AddSubResource("logging");

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

            using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Encoding = System.Text.Encoding.UTF8, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.Entitize
            }))
            {
                xmlWriter.WriteStartElement("BucketLoggingStatus", "");
                var bucketLoggingStatusBucketLoggingStatus = putBucketLoggingRequest.LoggingConfig;
                if (bucketLoggingStatusBucketLoggingStatus != null)
                {
                    if (bucketLoggingStatusBucketLoggingStatus != null)
                    {
                        var loggingEnabledLoggingEnabled = bucketLoggingStatusBucketLoggingStatus;
                        if (loggingEnabledLoggingEnabled != null && loggingEnabledLoggingEnabled.IsSetTargetBucket())
                        {
                            xmlWriter.WriteStartElement("LoggingEnabled", "");
                            xmlWriter.WriteElementString("TargetBucket", "", S3Transforms.ToXmlStringValue(loggingEnabledLoggingEnabled.TargetBucketName));

                            var loggingEnabledLoggingEnabledtargetGrantsList = loggingEnabledLoggingEnabled.Grants;
                            if (loggingEnabledLoggingEnabledtargetGrantsList != null && loggingEnabledLoggingEnabledtargetGrantsList.Count > 0)
                            {
                                xmlWriter.WriteStartElement("TargetGrants", "");
                                foreach (var loggingEnabledLoggingEnabledtargetGrantsListValue in loggingEnabledLoggingEnabledtargetGrantsList)
                                {
                                    xmlWriter.WriteStartElement("Grant", "");
                                    if (loggingEnabledLoggingEnabledtargetGrantsListValue != null)
                                    {
                                        var granteeGrantee = loggingEnabledLoggingEnabledtargetGrantsListValue.Grantee;
                                        if (granteeGrantee != null)
                                        {
                                            xmlWriter.WriteStartElement("Grantee", "");
                                            if (granteeGrantee.IsSetType())
                                            {
                                                xmlWriter.WriteAttributeString("xsi", "type", "http://www.w3.org/2001/XMLSchema-instance", granteeGrantee.Type.ToString());
                                            }
                                            if (granteeGrantee.IsSetDisplayName())
                                            {
                                                xmlWriter.WriteElementString("DisplayName", "", S3Transforms.ToXmlStringValue(granteeGrantee.DisplayName));
                                            }
                                            if (granteeGrantee.IsSetEmailAddress())
                                            {
                                                xmlWriter.WriteElementString("EmailAddress", "", S3Transforms.ToXmlStringValue(granteeGrantee.EmailAddress));
                                            }
                                            if (granteeGrantee.IsSetCanonicalUser())
                                            {
                                                xmlWriter.WriteElementString("ID", "", S3Transforms.ToXmlStringValue(granteeGrantee.CanonicalUser));
                                            }
                                            if (granteeGrantee.IsSetURI())
                                            {
                                                xmlWriter.WriteElementString("URI", "", S3Transforms.ToXmlStringValue(granteeGrantee.URI));
                                            }
                                            xmlWriter.WriteEndElement();
                                        }

                                        if (loggingEnabledLoggingEnabledtargetGrantsListValue.IsSetPermission())
                                        {
                                            xmlWriter.WriteElementString("Permission", "", S3Transforms.ToXmlStringValue(loggingEnabledLoggingEnabledtargetGrantsListValue.Permission));
                                        }
                                    }

                                    xmlWriter.WriteEndElement();
                                }
                                xmlWriter.WriteEndElement();
                            }
                            if (loggingEnabledLoggingEnabled.IsSetTargetPrefix())
                            {
                                xmlWriter.WriteElementString("TargetPrefix", "", S3Transforms.ToXmlStringValue(loggingEnabledLoggingEnabled.TargetPrefix));
                            }
                            else
                            {
                                xmlWriter.WriteStartElement("TargetPrefix");
                                xmlWriter.WriteEndElement();
                            }
                            xmlWriter.WriteEndElement();
                        }
                    }
                }

                xmlWriter.WriteEndElement();
            }

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

                ChecksumUtils.SetRequestChecksum(request, putBucketLoggingRequest.ChecksumAlgorithm);
            }
            catch (EncoderFallbackException e)
            {
                throw new AmazonServiceException("Unable to marshall request to XML", e);
            }

            return(request);
        }
Example #17
0
        public IRequest Marshall(UploadPartRequest uploadPartRequest)
        {
            IRequest request = new DefaultRequest(uploadPartRequest, "AmazonS3");

            request.HttpMethod = "PUT";

            if (uploadPartRequest.IsSetChecksumAlgorithm())
            {
                request.Headers.Add(S3Constants.AmzHeaderSdkChecksumAlgorithm, S3Transforms.ToStringValue(uploadPartRequest.ChecksumAlgorithm));
            }

            if (uploadPartRequest.IsSetChecksumCRC32())
            {
                request.Headers["x-amz-checksum-crc32"] = S3Transforms.ToStringValue(uploadPartRequest.ChecksumCRC32);
            }

            if (uploadPartRequest.IsSetChecksumCRC32C())
            {
                request.Headers["x-amz-checksum-crc32c"] = S3Transforms.ToStringValue(uploadPartRequest.ChecksumCRC32C);
            }

            if (uploadPartRequest.IsSetChecksumSHA1())
            {
                request.Headers["x-amz-checksum-sha1"] = S3Transforms.ToStringValue(uploadPartRequest.ChecksumSHA1);
            }

            if (uploadPartRequest.IsSetChecksumSHA256())
            {
                request.Headers["x-amz-checksum-sha256"] = S3Transforms.ToStringValue(uploadPartRequest.ChecksumSHA256);
            }

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

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

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

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

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

            if (uploadPartRequest.IsSetPartNumber())
            {
                request.AddSubResource("partNumber", S3Transforms.ToStringValue(uploadPartRequest.PartNumber));
            }
            if (uploadPartRequest.IsSetUploadId())
            {
                request.AddSubResource("uploadId", S3Transforms.ToStringValue(uploadPartRequest.UploadId));
            }

            if (uploadPartRequest.InputStream != null)
            {
                // Wrap input stream in partial wrapper (to upload only part of the stream)
                var partialStream = new PartialWrapperStream(uploadPartRequest.InputStream, uploadPartRequest.PartSize);
                if (partialStream.Length > 0 && !(uploadPartRequest.DisablePayloadSigning ?? false))
                {
                    request.UseChunkEncoding = uploadPartRequest.UseChunkEncoding;
                }
                if (!request.Headers.ContainsKey(HeaderKeys.ContentLengthHeader))
                {
                    request.Headers.Add(HeaderKeys.ContentLengthHeader, partialStream.Length.ToString(CultureInfo.InvariantCulture));
                }

                request.DisablePayloadSigning = uploadPartRequest.DisablePayloadSigning;

                // Calculate Content-MD5 if not already set
                if (!uploadPartRequest.IsSetMD5Digest() && uploadPartRequest.CalculateContentMD5Header)
                {
                    string md5 = AmazonS3Util.GenerateMD5ChecksumForStream(partialStream);
                    if (!string.IsNullOrEmpty(md5))
                    {
                        request.Headers[HeaderKeys.ContentMD5Header] = md5;
                    }
                }

                if (!(uploadPartRequest.DisableMD5Stream ?? AWSConfigsS3.DisableMD5Stream))
                {
                    // Wrap input stream in MD5Stream; after this we can no longer seek or position the stream
                    var hashStream = new MD5Stream(partialStream, null, partialStream.Length);
                    uploadPartRequest.InputStream = hashStream;
                }
                else
                {
                    uploadPartRequest.InputStream = partialStream;
                }
            }

            request.ContentStream = uploadPartRequest.InputStream;
            ChecksumUtils.SetRequestChecksum(request, uploadPartRequest.ChecksumAlgorithm, fallbackToMD5: false);

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

            return(request);
        }