Google Cloud Storage C++ Client  1.24.0
A C++ Client Library for Google Cloud Storage
object_metadata.h
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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OBJECT_METADATA_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OBJECT_METADATA_H
17 
18 #include "google/cloud/storage/internal/common_metadata.h"
19 #include "google/cloud/storage/internal/complex_option.h"
22 #include "google/cloud/optional.h"
23 #include "google/cloud/status_or.h"
24 #include "absl/types/optional.h"
25 #include <map>
26 
27 namespace google {
28 namespace cloud {
29 namespace storage {
30 inline namespace STORAGE_CLIENT_NS {
31 namespace internal {
32 struct ObjectMetadataParser;
33 class GrpcClient;
34 } // namespace internal
35 
38  // The encryption algorithm name.
39  std::string encryption_algorithm;
40  // The SHA256 hash of the encryption key.
41  std::string key_sha256;
42 };
43 
46  std::string object_name;
47  absl::optional<std::int64_t> generation;
48  absl::optional<std::int64_t> if_generation_match;
49 };
50 
51 std::ostream& operator<<(std::ostream& os, ComposeSourceObject const& r);
52 
53 inline bool operator==(CustomerEncryption const& lhs,
54  CustomerEncryption const& rhs) {
55  return std::tie(lhs.encryption_algorithm, lhs.key_sha256) ==
56  std::tie(rhs.encryption_algorithm, rhs.key_sha256);
57 }
58 
59 inline bool operator<(CustomerEncryption const& lhs,
60  CustomerEncryption const& rhs) {
61  return std::tie(lhs.encryption_algorithm, lhs.key_sha256) <
62  std::tie(rhs.encryption_algorithm, rhs.key_sha256);
63 }
64 
65 inline bool operator!=(CustomerEncryption const& lhs,
66  CustomerEncryption const& rhs) {
67  return std::rel_ops::operator!=(lhs, rhs);
68 }
69 
70 inline bool operator>(CustomerEncryption const& lhs,
71  CustomerEncryption const& rhs) {
72  return std::rel_ops::operator>(lhs, rhs);
73 }
74 
75 inline bool operator<=(CustomerEncryption const& lhs,
76  CustomerEncryption const& rhs) {
77  return std::rel_ops::operator<=(lhs, rhs);
78 }
79 
80 inline bool operator>=(CustomerEncryption const& lhs,
81  CustomerEncryption const& rhs) {
82  return std::rel_ops::operator>=(lhs, rhs);
83 }
84 
88 class ObjectMetadata : private internal::CommonMetadata<ObjectMetadata> {
89  public:
90  ObjectMetadata() = default;
91 
92  // Please keep these in alphabetical order, that make it easier to verify we
93  // have actually implemented all of them.
94  std::vector<ObjectAccessControl> const& acl() const { return acl_; }
95  std::vector<ObjectAccessControl>& mutable_acl() { return acl_; }
96  ObjectMetadata& set_acl(std::vector<ObjectAccessControl> acl) {
97  acl_ = std::move(acl);
98  return *this;
99  }
100 
101  std::string const& bucket() const { return bucket_; }
102 
103  std::string const& cache_control() const { return cache_control_; }
104  ObjectMetadata& set_cache_control(std::string cache_control) {
105  cache_control_ = std::move(cache_control);
106  return *this;
107  }
108 
109  std::int32_t component_count() const { return component_count_; }
110 
111  std::string content_disposition() const { return content_disposition_; }
113  content_disposition_ = std::move(value);
114  return *this;
115  }
116 
117  std::string content_encoding() const { return content_encoding_; }
118  ObjectMetadata& set_content_encoding(std::string value) {
119  content_encoding_ = std::move(value);
120  return *this;
121  }
122 
123  std::string content_language() const { return content_language_; }
124  ObjectMetadata& set_content_language(std::string value) {
125  content_language_ = std::move(value);
126  return *this;
127  }
128 
129  std::string content_type() const { return content_type_; }
130  ObjectMetadata& set_content_type(std::string value) {
131  content_type_ = std::move(value);
132  return *this;
133  }
134 
135  std::string const& crc32c() const { return crc32c_; }
136 
137  bool has_customer_encryption() const {
138  return customer_encryption_.has_value();
139  }
141  return customer_encryption_.value();
142  }
143 
144  using CommonMetadata::etag;
145 
146  bool event_based_hold() const { return event_based_hold_; }
148  event_based_hold_ = v;
149  return *this;
150  }
151 
152  std::int64_t generation() const { return generation_; }
153 
154  using CommonMetadata::id;
155  using CommonMetadata::kind;
156 
157  std::string const& kms_key_name() const { return kms_key_name_; }
158  std::string const& md5_hash() const { return md5_hash_; }
159  std::string const& media_link() const { return media_link_; }
160 
162  bool has_metadata(std::string const& key) const {
164  return metadata_.end() != metadata_.find(key);
165  }
166  std::string const& metadata(std::string const& key) const {
167  return metadata_.at(key);
168  }
169 
171  ObjectMetadata& delete_metadata(std::string const& key) {
172  auto i = metadata_.find(key);
173  if (i == metadata_.end()) {
174  return *this;
175  }
176  metadata_.erase(i);
177  return *this;
178  }
179 
181  ObjectMetadata& upsert_metadata(std::string key, std::string value) {
182  auto i = metadata_.lower_bound(key);
183  if (i == metadata_.end() || i->first != key) {
184  metadata_.emplace_hint(i, std::move(key), std::move(value));
185  } else {
186  i->second = std::move(value);
187  }
188  return *this;
189  }
190 
191  std::map<std::string, std::string> const& metadata() const {
192  return metadata_;
193  }
194  std::map<std::string, std::string>& mutable_metadata() { return metadata_; }
196 
197  using CommonMetadata::has_owner;
198  using CommonMetadata::metageneration;
199  using CommonMetadata::name;
200  using CommonMetadata::owner;
201 
202  std::chrono::system_clock::time_point retention_expiration_time() const {
203  return retention_expiration_time_;
204  }
205 
206  using CommonMetadata::self_link;
207 
208  std::uint64_t size() const { return size_; }
209 
210  using CommonMetadata::storage_class;
212  CommonMetadata::set_storage_class(std::move(v));
213  return *this;
214  }
215 
216  bool temporary_hold() const { return temporary_hold_; }
218  temporary_hold_ = v;
219  return *this;
220  }
221 
222  using CommonMetadata::time_created;
223 
224  std::chrono::system_clock::time_point time_deleted() const {
225  return time_deleted_;
226  }
227  std::chrono::system_clock::time_point time_storage_class_updated() const {
228  return time_storage_class_updated_;
229  }
230 
231  using CommonMetadata::updated;
232 
233  bool has_custom_time() const { return custom_time_.has_value(); }
234  std::chrono::system_clock::time_point custom_time() const {
235  return custom_time_.value_or(std::chrono::system_clock::time_point{});
236  }
237  ObjectMetadata& set_custom_time(std::chrono::system_clock::time_point v) {
238  custom_time_ = v;
239  return *this;
240  }
242  custom_time_.reset();
243  return *this;
244  }
245 
246  friend bool operator==(ObjectMetadata const& lhs, ObjectMetadata const& rhs);
247  friend bool operator!=(ObjectMetadata const& lhs, ObjectMetadata const& rhs) {
248  return !(lhs == rhs);
249  }
250 
251  private:
252  friend struct internal::ObjectMetadataParser;
253  friend class internal::GrpcClient;
254 
255  friend std::ostream& operator<<(std::ostream& os, ObjectMetadata const& rhs);
256  // Keep the fields in alphabetical order.
257  std::vector<ObjectAccessControl> acl_;
258  std::string bucket_;
259  std::string cache_control_;
260  std::int32_t component_count_{0};
261  std::string content_disposition_;
262  std::string content_encoding_;
263  std::string content_language_;
264  std::string content_type_;
265  std::string crc32c_;
266  absl::optional<CustomerEncryption> customer_encryption_;
267  bool event_based_hold_{false};
268  std::int64_t generation_{0};
269  std::string kms_key_name_;
270  std::string md5_hash_;
271  std::string media_link_;
272  std::map<std::string, std::string> metadata_;
273  std::chrono::system_clock::time_point retention_expiration_time_;
274  std::uint64_t size_{0};
275  bool temporary_hold_{false};
276  std::chrono::system_clock::time_point time_deleted_;
277  std::chrono::system_clock::time_point time_storage_class_updated_;
278  absl::optional<std::chrono::system_clock::time_point> custom_time_;
279 };
280 
281 std::ostream& operator<<(std::ostream& os, ObjectMetadata const& rhs);
282 
296  public:
298 
299  std::string BuildPatch() const;
300 
301  ObjectMetadataPatchBuilder& SetAcl(std::vector<ObjectAccessControl> const& v);
302 
308  ObjectMetadataPatchBuilder& ResetAcl();
309 
310  ObjectMetadataPatchBuilder& SetCacheControl(std::string const& v);
311  ObjectMetadataPatchBuilder& ResetCacheControl();
312  ObjectMetadataPatchBuilder& SetContentDisposition(std::string const& v);
313  ObjectMetadataPatchBuilder& ResetContentDisposition();
314  ObjectMetadataPatchBuilder& SetContentEncoding(std::string const& v);
315  ObjectMetadataPatchBuilder& ResetContentEncoding();
316  ObjectMetadataPatchBuilder& SetContentLanguage(std::string const& v);
317  ObjectMetadataPatchBuilder& ResetContentLanguage();
318  ObjectMetadataPatchBuilder& SetContentType(std::string const& v);
319  ObjectMetadataPatchBuilder& ResetContentType();
320  ObjectMetadataPatchBuilder& SetEventBasedHold(bool v);
321  ObjectMetadataPatchBuilder& ResetEventBasedHold();
322 
323  ObjectMetadataPatchBuilder& SetMetadata(std::string const& key,
324  std::string const& value);
325  ObjectMetadataPatchBuilder& ResetMetadata(std::string const& key);
326  ObjectMetadataPatchBuilder& ResetMetadata();
327 
328  ObjectMetadataPatchBuilder& SetTemporaryHold(bool v);
329  ObjectMetadataPatchBuilder& ResetTemporaryHold();
330 
337  ObjectMetadataPatchBuilder& SetCustomTime(
338  std::chrono::system_clock::time_point tp);
339  ObjectMetadataPatchBuilder& ResetCustomTime();
340 
341  private:
342  internal::PatchBuilder impl_;
343  bool metadata_subpatch_dirty_{false};
344  internal::PatchBuilder metadata_subpatch_;
345 };
346 
351  : public internal::ComplexOption<WithObjectMetadata, ObjectMetadata> {
352  using ComplexOption<WithObjectMetadata, ObjectMetadata>::ComplexOption;
353  // GCC <= 7.0 does not use the inherited default constructor, redeclare it
354  // explicitly
355  WithObjectMetadata() = default;
356  static char const* name() { return "object-metadata"; }
357 };
358 
359 } // namespace STORAGE_CLIENT_NS
360 } // namespace storage
361 } // namespace cloud
362 } // namespace google
363 
364 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OBJECT_METADATA_H
google::cloud::storage::v1::ObjectMetadata
Represents the metadata for a Google Cloud Storage Object.
Definition: object_metadata.h:88
google::cloud::storage::v1::ComposeSourceObject
Defines one of the source objects for a compose operation.
Definition: object_metadata.h:45
google::cloud::storage::v1::ObjectMetadata::upsert_metadata
ObjectMetadata & upsert_metadata(std::string key, std::string value)
Insert or update the metadata entry.
Definition: object_metadata.h:181
status_or.h
google::cloud::storage::v1::ObjectMetadata::set_storage_class
ObjectMetadata & set_storage_class(std::string v)
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:211
google::cloud::storage::v1::ObjectMetadata::custom_time
std::chrono::system_clock::time_point custom_time() const
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:234
google::cloud::storage::v1::ObjectMetadata::retention_expiration_time
std::chrono::system_clock::time_point retention_expiration_time() const
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:202
google::cloud::storage::v1::operator>=
bool operator>=(CustomerEncryption const &lhs, CustomerEncryption const &rhs)
Definition: object_metadata.h:80
google::cloud::storage::v1::operator<
bool operator<(BucketBilling const &lhs, BucketBilling const &rhs)
Definition: bucket_metadata.h:57
google::cloud::storage::v1::CustomerEncryption::key_sha256
std::string key_sha256
Definition: object_metadata.h:41
google::cloud::storage::v1::operator!=
bool operator!=(CustomerEncryption const &lhs, CustomerEncryption const &rhs)
Definition: object_metadata.h:65
google::cloud::storage::v1::ObjectMetadata::bucket
std::string const & bucket() const
Definition: object_metadata.h:101
google::cloud::storage::v1::operator<<
std::ostream & operator<<(std::ostream &os, BucketAccessControl const &rhs)
Definition: bucket_access_control.cc:27
google::cloud::storage::v1::ObjectMetadata::component_count
std::int32_t component_count() const
Definition: object_metadata.h:109
optional.h
google::cloud::storage::v1::ObjectMetadata::set_temporary_hold
ObjectMetadata & set_temporary_hold(bool v)
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:217
google::cloud::storage::v1::ObjectMetadata::operator!=
friend bool operator!=(ObjectMetadata const &lhs, ObjectMetadata const &rhs)
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:247
google::cloud::storage::v1::WithObjectMetadata::WithObjectMetadata
WithObjectMetadata()=default
google::cloud::storage::v1::WithObjectMetadata
A request option to define the object metadata attributes.
Definition: object_metadata.h:351
google::cloud::storage::v1::ObjectMetadata::set_custom_time
ObjectMetadata & set_custom_time(std::chrono::system_clock::time_point v)
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:237
google::cloud::storage::v1::operator>
bool operator>(BucketBilling const &lhs, BucketBilling const &rhs)
Definition: bucket_metadata.h:65
google::cloud::storage::v1::ObjectMetadata::md5_hash
std::string const & md5_hash() const
Definition: object_metadata.h:158
google::cloud::storage::v1::ObjectMetadata::event_based_hold
bool event_based_hold() const
Definition: object_metadata.h:146
google::cloud::storage::v1::ObjectMetadata::has_customer_encryption
bool has_customer_encryption() const
Definition: object_metadata.h:137
version.h
google::cloud::storage::v1::ObjectMetadata::set_acl
ObjectMetadata & set_acl(std::vector< ObjectAccessControl > acl)
Definition: object_metadata.h:96
object_access_control.h
google::cloud::storage::v1::ObjectMetadata::content_disposition
std::string content_disposition() const
Definition: object_metadata.h:111
google::cloud::storage::v1::ObjectMetadataPatchBuilder
Prepares a patch for the Bucket resource.
Definition: object_metadata.h:295
google::cloud::storage::v1::operator!=
bool operator!=(BucketBilling const &lhs, BucketBilling const &rhs)
Definition: bucket_metadata.h:61
google::cloud::storage::v1::ObjectMetadata::content_type
std::string content_type() const
Definition: object_metadata.h:129
google::cloud::storage::v1::operator<=
bool operator<=(CustomerEncryption const &lhs, CustomerEncryption const &rhs)
Definition: object_metadata.h:75
google::cloud::storage::v1::ObjectMetadata::content_language
std::string content_language() const
Definition: object_metadata.h:123
google::cloud::storage::v1::ObjectMetadata::cache_control
std::string const & cache_control() const
Definition: object_metadata.h:103
google::cloud::storage::v1::ObjectMetadata::crc32c
std::string const & crc32c() const
Definition: object_metadata.h:135
google::cloud::storage::v1::ComposeSourceObject::generation
absl::optional< std::int64_t > generation
Definition: object_metadata.h:47
google::cloud::storage::v1::ObjectMetadata::generation
std::int64_t generation() const
Definition: object_metadata.h:152
google::cloud::storage::v1::ObjectMetadata::customer_encryption
CustomerEncryption const & customer_encryption() const
Definition: object_metadata.h:140
google::cloud::storage::v1::ObjectMetadata::mutable_metadata
std::map< std::string, std::string > & mutable_metadata()
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:194
google::cloud::storage::v1::ObjectMetadata::set_content_disposition
ObjectMetadata & set_content_disposition(std::string value)
Definition: object_metadata.h:112
google::cloud::storage::v1::operator>
bool operator>(CustomerEncryption const &lhs, CustomerEncryption const &rhs)
Definition: object_metadata.h:70
google::cloud::storage::v1::ObjectMetadata::set_cache_control
ObjectMetadata & set_cache_control(std::string cache_control)
Definition: object_metadata.h:104
google::cloud::storage::v1::ObjectMetadata::time_deleted
std::chrono::system_clock::time_point time_deleted() const
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:224
google::cloud::storage::v1::ObjectMetadata::temporary_hold
bool temporary_hold() const
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:216
google::cloud::storage::v1::ObjectMetadata::set_content_type
ObjectMetadata & set_content_type(std::string value)
Definition: object_metadata.h:130
STORAGE_CLIENT_NS
#define STORAGE_CLIENT_NS
Definition: version.h:22
google::cloud::storage::v1::ObjectMetadata::media_link
std::string const & media_link() const
Definition: object_metadata.h:159
google::cloud::storage::v1::ObjectMetadata::kms_key_name
std::string const & kms_key_name() const
Definition: object_metadata.h:157
google::cloud::storage::v1::ObjectMetadata::delete_metadata
ObjectMetadata & delete_metadata(std::string const &key)
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:171
google::cloud::storage::v1::ObjectMetadata::set_content_encoding
ObjectMetadata & set_content_encoding(std::string value)
Definition: object_metadata.h:118
google::cloud::storage::v1::WithObjectMetadata::name
static char const * name()
Definition: object_metadata.h:356
google::cloud::storage::v1::ObjectMetadata::time_storage_class_updated
std::chrono::system_clock::time_point time_storage_class_updated() const
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:227
google::cloud::storage::v1::ObjectMetadata::ObjectMetadata
ObjectMetadata()=default
google::cloud::storage::v1::ObjectMetadata::mutable_acl
std::vector< ObjectAccessControl > & mutable_acl()
Definition: object_metadata.h:95
google::cloud::storage::v1::ObjectMetadata::acl
std::vector< ObjectAccessControl > const & acl() const
Definition: object_metadata.h:94
google::cloud::storage::v1::ObjectMetadata::set_content_language
ObjectMetadata & set_content_language(std::string value)
Definition: object_metadata.h:124
google::cloud::storage::v1::operator>=
bool operator>=(BucketBilling const &lhs, BucketBilling const &rhs)
Definition: bucket_metadata.h:73
google::cloud::storage::v1::operator==
bool operator==(BucketAccessControl const &lhs, BucketAccessControl const &rhs)
Definition: bucket_access_control.cc:22
google::cloud::storage::v1::ObjectMetadata::size
std::uint64_t size() const
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:208
google::cloud::storage::v1::ObjectMetadataPatchBuilder::ObjectMetadataPatchBuilder
ObjectMetadataPatchBuilder()=default
google::cloud::storage::v1::ObjectMetadata::content_encoding
std::string content_encoding() const
Definition: object_metadata.h:117
google::cloud::storage::v1::ObjectMetadata::set_event_based_hold
ObjectMetadata & set_event_based_hold(bool v)
Definition: object_metadata.h:147
google::cloud::storage::v1::ObjectMetadata::has_custom_time
bool has_custom_time() const
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:233
google::cloud::storage::v1::ObjectMetadata::metadata
std::string const & metadata(std::string const &key) const
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:166
google::cloud::storage::v1::CustomerEncryption
A simple representation for the customerEncryption field.
Definition: object_metadata.h:37
google::cloud::storage::v1::ComposeSourceObject::object_name
std::string object_name
Definition: object_metadata.h:46
google
google::cloud::storage::v1::ObjectMetadata::reset_custom_time
ObjectMetadata & reset_custom_time()
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:241
google::cloud::storage::v1::ComposeSourceObject::if_generation_match
absl::optional< std::int64_t > if_generation_match
Definition: object_metadata.h:48
google::cloud::storage::v1::CustomerEncryption::encryption_algorithm
std::string encryption_algorithm
Definition: object_metadata.h:39
google::cloud::storage::v1::operator<=
bool operator<=(BucketBilling const &lhs, BucketBilling const &rhs)
Definition: bucket_metadata.h:69
google::cloud::storage::v1::ObjectMetadata::metadata
std::map< std::string, std::string > const & metadata() const
Delete a metadata entry. This is a no-op if the key does not exist.
Definition: object_metadata.h:191