Google Cloud Storage C++ Client  1.32.1
A C++ Client Library for Google Cloud Storage
object_metadata.cc
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/storage/object_metadata.h"
16 #include "google/cloud/storage/internal/metadata_parser.h"
17 #include "google/cloud/storage/internal/object_acl_requests.h"
18 #include "google/cloud/internal/absl_str_join_quiet.h"
19 #include "google/cloud/internal/format_time_point.h"
20 #include <nlohmann/json.hpp>
21 
22 namespace google {
23 namespace cloud {
24 namespace storage {
25 inline namespace STORAGE_CLIENT_NS {
26 
27 using ::google::cloud::internal::FormatRfc3339;
28 
29 bool operator==(ObjectMetadata const& lhs, ObjectMetadata const& rhs) {
30  return static_cast<internal::CommonMetadata<ObjectMetadata> const&>(lhs) ==
31  rhs &&
32  lhs.acl_ == rhs.acl_ && lhs.bucket_ == rhs.bucket_ &&
33  lhs.cache_control_ == rhs.cache_control_ &&
34  lhs.component_count_ == rhs.component_count_ &&
35  lhs.content_disposition_ == rhs.content_disposition_ &&
36  lhs.content_encoding_ == rhs.content_encoding_ &&
37  lhs.content_language_ == rhs.content_language_ &&
38  lhs.content_type_ == rhs.content_type_ && lhs.crc32c_ == rhs.crc32c_ &&
39  lhs.customer_encryption_ == rhs.customer_encryption_ &&
40  lhs.event_based_hold_ == rhs.event_based_hold_ &&
41  lhs.generation_ == rhs.generation_ &&
42  lhs.kms_key_name_ == rhs.kms_key_name_ &&
43  lhs.md5_hash_ == rhs.md5_hash_ && lhs.media_link_ == rhs.media_link_ &&
44  lhs.metadata_ == rhs.metadata_ &&
45  lhs.retention_expiration_time_ == rhs.retention_expiration_time_ &&
46  lhs.temporary_hold_ == rhs.temporary_hold_ &&
47  lhs.time_deleted_ == rhs.time_deleted_ &&
48  lhs.time_storage_class_updated_ == rhs.time_storage_class_updated_ &&
49  lhs.size_ == rhs.size_ && lhs.custom_time_ == rhs.custom_time_;
50 }
51 
52 std::ostream& operator<<(std::ostream& os, ObjectMetadata const& rhs) {
53  os << "ObjectMetadata={name=" << rhs.name() << ", acl=[";
54  os << absl::StrJoin(rhs.acl(), ", ", absl::StreamFormatter());
55  os << "], bucket=" << rhs.bucket()
56  << ", cache_control=" << rhs.cache_control()
57  << ", component_count=" << rhs.component_count()
58  << ", content_disposition=" << rhs.content_disposition()
59  << ", content_encoding=" << rhs.content_encoding()
60  << ", content_language=" << rhs.content_language()
61  << ", content_type=" << rhs.content_type() << ", crc32c=" << rhs.crc32c();
62 
64  os << ", customer_encryption.encryption_algorithm="
66  << ", customer_encryption.key_sha256="
68  }
69 
70  os << ", etag=" << rhs.etag() << ", event_based_hold=" << std::boolalpha
71  << rhs.event_based_hold() << ", generation=" << rhs.generation()
72  << ", id=" << rhs.id() << ", kind=" << rhs.kind()
73  << ", kms_key_name=" << rhs.kms_key_name()
74  << ", md5_hash=" << rhs.md5_hash() << ", media_link=" << rhs.media_link();
75  if (!rhs.metadata_.empty()) {
76  os << "metadata."
77  << absl::StrJoin(rhs.metadata_, ", metadata.", absl::PairFormatter("="));
78  }
79  os << ", metageneration=" << rhs.metageneration() << ", name=" << rhs.name();
80 
81  if (rhs.has_owner()) {
82  os << ", owner.entity=" << rhs.owner().entity
83  << ", owner.entity_id=" << rhs.owner().entity_id;
84  }
85 
86  os << ", retention_expiration_time="
87  << FormatRfc3339(rhs.retention_expiration_time())
88  << ", self_link=" << rhs.self_link() << ", size=" << rhs.size()
89  << ", storage_class=" << rhs.storage_class()
90  << ", temporary_hold=" << std::boolalpha << rhs.temporary_hold()
91  << ", time_created=" << rhs.time_created().time_since_epoch().count()
92  << ", time_deleted=" << rhs.time_deleted().time_since_epoch().count()
93  << ", time_storage_class_updated="
94  << rhs.time_storage_class_updated().time_since_epoch().count()
95  << ", updated=" << rhs.updated().time_since_epoch().count();
96  if (rhs.has_custom_time()) {
97  os << ", custom_time=" << FormatRfc3339(rhs.custom_time());
98  }
99  return os << "}";
100 }
101 
103  internal::PatchBuilder tmp = impl_;
104  if (metadata_subpatch_dirty_) {
105  if (metadata_subpatch_.empty()) {
106  tmp.RemoveField("metadata");
107  } else {
108  tmp.AddSubPatch("metadata", metadata_subpatch_);
109  }
110  }
111  return tmp.ToString();
112 }
113 
115  std::vector<ObjectAccessControl> const& v) {
116  if (v.empty()) {
117  return ResetAcl();
118  }
119  auto array = nlohmann::json::array();
120  for (auto const& a : v) {
121  array.emplace_back(nlohmann::json{
122  {"entity", a.entity()},
123  {"role", a.role()},
124  });
125  }
126  impl_.SetArrayField("acl", array.dump());
127  return *this;
128 }
129 
131  impl_.RemoveField("acl");
132  return *this;
133 }
134 
136  std::string const& v) {
137  if (v.empty()) {
138  return ResetCacheControl();
139  }
140  impl_.SetStringField("cacheControl", v);
141  return *this;
142 }
143 
145  impl_.RemoveField("cacheControl");
146  return *this;
147 }
148 
150  std::string const& v) {
151  if (v.empty()) {
153  }
154  impl_.SetStringField("contentDisposition", v);
155  return *this;
156 }
157 
160  impl_.RemoveField("contentDisposition");
161  return *this;
162 }
163 
165  std::string const& v) {
166  if (v.empty()) {
167  return ResetContentEncoding();
168  }
169  impl_.SetStringField("contentEncoding", v);
170  return *this;
171 }
172 
174  impl_.RemoveField("contentEncoding");
175  return *this;
176 }
177 
179  std::string const& v) {
180  if (v.empty()) {
181  return ResetContentLanguage();
182  }
183  impl_.SetStringField("contentLanguage", v);
184  return *this;
185 }
186 
188  impl_.RemoveField("contentLanguage");
189  return *this;
190 }
191 
193  std::string const& v) {
194  if (v.empty()) {
195  return ResetContentType();
196  }
197  impl_.SetStringField("contentType", v);
198  return *this;
199 }
200 
202  impl_.RemoveField("contentType");
203  return *this;
204 }
205 
207  bool v) {
208  impl_.SetBoolField("eventBasedHold", v);
209  return *this;
210 }
211 
213  impl_.RemoveField("eventBasedHold");
214  return *this;
215 }
216 
218  std::string const& key, std::string const& value) {
219  metadata_subpatch_.SetStringField(key.c_str(), value);
220  metadata_subpatch_dirty_ = true;
221  return *this;
222 }
223 
225  std::string const& key) {
226  metadata_subpatch_.RemoveField(key.c_str());
227  metadata_subpatch_dirty_ = true;
228  return *this;
229 }
230 
232  metadata_subpatch_.clear();
233  metadata_subpatch_dirty_ = true;
234  return *this;
235 }
236 
238  bool v) {
239  impl_.SetBoolField("temporaryHold", v);
240  return *this;
241 }
242 
244  impl_.RemoveField("temporaryHold");
245  return *this;
246 }
247 
249  std::chrono::system_clock::time_point tp) {
250  impl_.SetStringField("customTime",
251  google::cloud::internal::FormatRfc3339(tp));
252  return *this;
253 }
254 
256  impl_.RemoveField("customTime");
257  return *this;
258 }
259 
260 } // namespace STORAGE_CLIENT_NS
261 } // namespace storage
262 } // namespace cloud
263 } // namespace google