1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 
6 #include <aws/s3/model/PutObjectRequest.h>
7 #include <aws/core/AmazonWebServiceResult.h>
8 #include <aws/core/http/URI.h>
9 #include <aws/core/utils/memory/stl/AWSStringStream.h>
10 #include <aws/core/utils/HashingUtils.h>
11 
12 #include <utility>
13 
14 using namespace Aws::S3::Model;
15 using namespace Aws::Utils::Stream;
16 using namespace Aws::Utils;
17 using namespace Aws::Http;
18 using namespace Aws;
19 
PutObjectRequest()20 PutObjectRequest::PutObjectRequest() :
21     m_aCL(ObjectCannedACL::NOT_SET),
22     m_aCLHasBeenSet(false),
23     m_bucketHasBeenSet(false),
24     m_cacheControlHasBeenSet(false),
25     m_contentDispositionHasBeenSet(false),
26     m_contentEncodingHasBeenSet(false),
27     m_contentLanguageHasBeenSet(false),
28     m_contentLength(0),
29     m_contentLengthHasBeenSet(false),
30     m_contentMD5HasBeenSet(false),
31     m_expiresHasBeenSet(false),
32     m_grantFullControlHasBeenSet(false),
33     m_grantReadHasBeenSet(false),
34     m_grantReadACPHasBeenSet(false),
35     m_grantWriteACPHasBeenSet(false),
36     m_keyHasBeenSet(false),
37     m_metadataHasBeenSet(false),
38     m_serverSideEncryption(ServerSideEncryption::NOT_SET),
39     m_serverSideEncryptionHasBeenSet(false),
40     m_storageClass(StorageClass::NOT_SET),
41     m_storageClassHasBeenSet(false),
42     m_websiteRedirectLocationHasBeenSet(false),
43     m_sSECustomerAlgorithmHasBeenSet(false),
44     m_sSECustomerKeyHasBeenSet(false),
45     m_sSECustomerKeyMD5HasBeenSet(false),
46     m_sSEKMSKeyIdHasBeenSet(false),
47     m_sSEKMSEncryptionContextHasBeenSet(false),
48     m_bucketKeyEnabled(false),
49     m_bucketKeyEnabledHasBeenSet(false),
50     m_requestPayer(RequestPayer::NOT_SET),
51     m_requestPayerHasBeenSet(false),
52     m_taggingHasBeenSet(false),
53     m_objectLockMode(ObjectLockMode::NOT_SET),
54     m_objectLockModeHasBeenSet(false),
55     m_objectLockRetainUntilDateHasBeenSet(false),
56     m_objectLockLegalHoldStatus(ObjectLockLegalHoldStatus::NOT_SET),
57     m_objectLockLegalHoldStatusHasBeenSet(false),
58     m_expectedBucketOwnerHasBeenSet(false),
59     m_customizedAccessLogTagHasBeenSet(false)
60 {
61 }
62 
63 
AddQueryStringParameters(URI & uri) const64 void PutObjectRequest::AddQueryStringParameters(URI& uri) const
65 {
66     Aws::StringStream ss;
67     if(!m_customizedAccessLogTag.empty())
68     {
69         // only accept customized LogTag which starts with "x-"
70         Aws::Map<Aws::String, Aws::String> collectedLogTags;
71         for(const auto& entry: m_customizedAccessLogTag)
72         {
73             if (!entry.first.empty() && !entry.second.empty() && entry.first.substr(0, 2) == "x-")
74             {
75                 collectedLogTags.emplace(entry.first, entry.second);
76             }
77         }
78 
79         if (!collectedLogTags.empty())
80         {
81             uri.AddQueryStringParameter(collectedLogTags);
82         }
83     }
84 }
85 
GetRequestSpecificHeaders() const86 Aws::Http::HeaderValueCollection PutObjectRequest::GetRequestSpecificHeaders() const
87 {
88   Aws::Http::HeaderValueCollection headers;
89   Aws::StringStream ss;
90   if(m_aCLHasBeenSet)
91   {
92     headers.emplace("x-amz-acl", ObjectCannedACLMapper::GetNameForObjectCannedACL(m_aCL));
93   }
94 
95   if(m_cacheControlHasBeenSet)
96   {
97     ss << m_cacheControl;
98     headers.emplace("cache-control",  ss.str());
99     ss.str("");
100   }
101 
102   if(m_contentDispositionHasBeenSet)
103   {
104     ss << m_contentDisposition;
105     headers.emplace("content-disposition",  ss.str());
106     ss.str("");
107   }
108 
109   if(m_contentEncodingHasBeenSet)
110   {
111     ss << m_contentEncoding;
112     headers.emplace("content-encoding",  ss.str());
113     ss.str("");
114   }
115 
116   if(m_contentLanguageHasBeenSet)
117   {
118     ss << m_contentLanguage;
119     headers.emplace("content-language",  ss.str());
120     ss.str("");
121   }
122 
123   if(m_contentLengthHasBeenSet)
124   {
125     ss << m_contentLength;
126     headers.emplace("content-length",  ss.str());
127     ss.str("");
128   }
129 
130   if(m_contentMD5HasBeenSet)
131   {
132     ss << m_contentMD5;
133     headers.emplace("content-md5",  ss.str());
134     ss.str("");
135   }
136 
137   if(m_expiresHasBeenSet)
138   {
139     headers.emplace("expires", m_expires.ToGmtString(DateFormat::RFC822));
140   }
141 
142   if(m_grantFullControlHasBeenSet)
143   {
144     ss << m_grantFullControl;
145     headers.emplace("x-amz-grant-full-control",  ss.str());
146     ss.str("");
147   }
148 
149   if(m_grantReadHasBeenSet)
150   {
151     ss << m_grantRead;
152     headers.emplace("x-amz-grant-read",  ss.str());
153     ss.str("");
154   }
155 
156   if(m_grantReadACPHasBeenSet)
157   {
158     ss << m_grantReadACP;
159     headers.emplace("x-amz-grant-read-acp",  ss.str());
160     ss.str("");
161   }
162 
163   if(m_grantWriteACPHasBeenSet)
164   {
165     ss << m_grantWriteACP;
166     headers.emplace("x-amz-grant-write-acp",  ss.str());
167     ss.str("");
168   }
169 
170   if(m_metadataHasBeenSet)
171   {
172     for(const auto& item : m_metadata)
173     {
174       ss << "x-amz-meta-" << item.first;
175       headers.emplace(ss.str(), item.second);
176       ss.str("");
177     }
178   }
179 
180   if(m_serverSideEncryptionHasBeenSet)
181   {
182     headers.emplace("x-amz-server-side-encryption", ServerSideEncryptionMapper::GetNameForServerSideEncryption(m_serverSideEncryption));
183   }
184 
185   if(m_storageClassHasBeenSet)
186   {
187     headers.emplace("x-amz-storage-class", StorageClassMapper::GetNameForStorageClass(m_storageClass));
188   }
189 
190   if(m_websiteRedirectLocationHasBeenSet)
191   {
192     ss << m_websiteRedirectLocation;
193     headers.emplace("x-amz-website-redirect-location",  ss.str());
194     ss.str("");
195   }
196 
197   if(m_sSECustomerAlgorithmHasBeenSet)
198   {
199     ss << m_sSECustomerAlgorithm;
200     headers.emplace("x-amz-server-side-encryption-customer-algorithm",  ss.str());
201     ss.str("");
202   }
203 
204   if(m_sSECustomerKeyHasBeenSet)
205   {
206     ss << m_sSECustomerKey;
207     headers.emplace("x-amz-server-side-encryption-customer-key",  ss.str());
208     ss.str("");
209   }
210 
211   if(m_sSECustomerKeyMD5HasBeenSet)
212   {
213     ss << m_sSECustomerKeyMD5;
214     headers.emplace("x-amz-server-side-encryption-customer-key-md5",  ss.str());
215     ss.str("");
216   }
217 
218   if(m_sSEKMSKeyIdHasBeenSet)
219   {
220     ss << m_sSEKMSKeyId;
221     headers.emplace("x-amz-server-side-encryption-aws-kms-key-id",  ss.str());
222     ss.str("");
223   }
224 
225   if(m_sSEKMSEncryptionContextHasBeenSet)
226   {
227     ss << m_sSEKMSEncryptionContext;
228     headers.emplace("x-amz-server-side-encryption-context",  ss.str());
229     ss.str("");
230   }
231 
232   if(m_bucketKeyEnabledHasBeenSet)
233   {
234     ss << std::boolalpha << m_bucketKeyEnabled;
235     headers.emplace("x-amz-server-side-encryption-bucket-key-enabled", ss.str());
236     ss.str("");
237   }
238 
239   if(m_requestPayerHasBeenSet)
240   {
241     headers.emplace("x-amz-request-payer", RequestPayerMapper::GetNameForRequestPayer(m_requestPayer));
242   }
243 
244   if(m_taggingHasBeenSet)
245   {
246     ss << m_tagging;
247     headers.emplace("x-amz-tagging",  ss.str());
248     ss.str("");
249   }
250 
251   if(m_objectLockModeHasBeenSet)
252   {
253     headers.emplace("x-amz-object-lock-mode", ObjectLockModeMapper::GetNameForObjectLockMode(m_objectLockMode));
254   }
255 
256   if(m_objectLockRetainUntilDateHasBeenSet)
257   {
258     headers.emplace("x-amz-object-lock-retain-until-date", m_objectLockRetainUntilDate.ToGmtString(DateFormat::ISO_8601));
259   }
260 
261   if(m_objectLockLegalHoldStatusHasBeenSet)
262   {
263     headers.emplace("x-amz-object-lock-legal-hold", ObjectLockLegalHoldStatusMapper::GetNameForObjectLockLegalHoldStatus(m_objectLockLegalHoldStatus));
264   }
265 
266   if(m_expectedBucketOwnerHasBeenSet)
267   {
268     ss << m_expectedBucketOwner;
269     headers.emplace("x-amz-expected-bucket-owner",  ss.str());
270     ss.str("");
271   }
272 
273   return headers;
274 
275 }
276