Google Cloud Storage C++ Client 2.13.0
A C++ Client Library for Google Cloud Storage
Loading...
Searching...
No Matches
client.h
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// https://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_CLIENT_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_CLIENT_H
17
18#include "google/cloud/storage/hmac_key_metadata.h"
19#include "google/cloud/storage/internal/logging_client.h"
20#include "google/cloud/storage/internal/parameter_pack_validation.h"
21#include "google/cloud/storage/internal/policy_document_request.h"
22#include "google/cloud/storage/internal/request_project_id.h"
23#include "google/cloud/storage/internal/retry_client.h"
24#include "google/cloud/storage/internal/signed_url_requests.h"
25#include "google/cloud/storage/internal/tuple_filter.h"
26#include "google/cloud/storage/list_buckets_reader.h"
27#include "google/cloud/storage/list_hmac_keys_reader.h"
28#include "google/cloud/storage/list_objects_and_prefixes_reader.h"
29#include "google/cloud/storage/list_objects_reader.h"
30#include "google/cloud/storage/notification_event_type.h"
31#include "google/cloud/storage/notification_payload_format.h"
32#include "google/cloud/storage/oauth2/google_credentials.h"
33#include "google/cloud/storage/object_rewriter.h"
34#include "google/cloud/storage/object_stream.h"
35#include "google/cloud/storage/retry_policy.h"
36#include "google/cloud/storage/upload_options.h"
37#include "google/cloud/storage/version.h"
38#include "google/cloud/internal/group_options.h"
39#include "google/cloud/internal/throw_delegate.h"
40#include "google/cloud/options.h"
41#include "google/cloud/status.h"
42#include "google/cloud/status_or.h"
43#include "absl/meta/type_traits.h"
44#include "absl/strings/string_view.h"
45#include <string>
46#include <type_traits>
47#include <vector>
48
49namespace google {
50namespace cloud {
51namespace storage {
52GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
53namespace internal {
54class NonResumableParallelUploadState;
55class ResumableParallelUploadState;
56struct ClientImplDetails;
57} // namespace internal
58/**
59 * The Google Cloud Storage (GCS) Client.
60 *
61 * This is the main class to interact with GCS. It provides member functions to
62 * invoke all the APIs in the service.
63 *
64 * @par Performance
65 * Creating an object of this type is a relatively low-cost operation.
66 * Connections to the service are created on demand. Copy-assignment and
67 * copy-construction are also relatively low-cost operations, they should be
68 * comparable to copying a few shared pointers. The first request (or any
69 * request that requires a new connection) incurs the cost of creating the
70 * connection and authenticating with the service. Note that the library may
71 * need to perform other bookkeeping operations that may impact performance.
72 * For example, access tokens need to be refreshed from time to time, and this
73 * may impact the performance of some operations.
74 *
75 * @par Connection Pool
76 * By default this class uses HTTPS to communicate with the service. Creating a
77 * new HTTPS session is relatively expensive, as it must go through the TCP/IP
78 * and SSL handshakes. To minimize this overhead the class maintains a
79 * connection pool to the service. After each request completes the connection
80 * is returned to the pool, and reused in future requests. Note that for
81 * downloads (implemented by the ReadObject() member function) the connection
82 * remains in use until the download completes. Therefore, having multiple
83 * downloads open at the same time requires multiple connections.
84 *
85 * The application can limit the maximum size of this connection pool using
86 * `storage::ConnectionPoolSizeOption`. If returning a connection to the pool
87 * would make the pool larger than this limit then the oldest connection in the
88 * pool is closed (recall that all connections in the pool are inactive). Note
89 * that this is the maximum size of the pool, the client library does not create
90 * connections until needed.
91 *
92 * Note that the application may (at times) use more connections than the
93 * maximum size of the pool. For example if N downloads are in progress the
94 * library may need N connections, even if the pool size is smaller.
95 *
96 * Two clients that compare equal share the same connection pool. Two clients
97 * created with the default constructor or with the constructor from a
98 * `google::cloud::Options` are never equal and do not share connection pools.
99 * Clients created via copy (or move) construction compare equal and share the
100 * connection pool.
101 *
102 * @par Thread-safety
103 * Instances of this class created via copy-construction or copy-assignment
104 * share the underlying pool of connections. Access to these copies via multiple
105 * threads is guaranteed to work. Two threads operating on the same instance of
106 * this class is not guaranteed to work.
107 *
108 * @par Credentials
109 * The default approach for creating a Client uses Google Application Default
110 * Credentials (ADCs). Note that a default-constructed client uses the ADCs:
111 *
112 * @snippet storage_client_initialization_samples.cc default-client
113 *
114 * Finding or loading the ADCs can fail. This will result in run-time errors
115 * when making requests.
116 *
117 * If you prefer to explicitly load the ADCs use:
118 *
119 * @snippet storage_client_initialization_samples.cc explicit-adcs
120 *
121 * To load a service account credentials key file use:
122 *
123 * @snippet storage_client_initialization_samples.cc service-account-keyfile
124 *
125 * Other credential types are available, including:
126 *
127 * - `google::cloud::MakeInsecureCredentials()` for anonymous access to public
128 * GCS buckets or objects.
129 * - `google::cloud::MakeAccessTokenCredentials()` to use an access token
130 * obtained through any out-of-band mechanism.
131 * - `google::cloud::MakeImpersonateServiceAccountCredentials()` to use the IAM
132 * credentials service and [impersonate a service account].
133 * - `google::cloud::MakeServiceAccountCredentials()` to use a service account
134 * key file.
135 *
136 * [impersonate service account]:
137 * https://cloud.google.com/iam/docs/impersonating-service-accounts
138 *
139 * @par Error Handling
140 * This class uses `StatusOr<T>` to report errors. When an operation fails to
141 * perform its work the returned `StatusOr<T>` contains the error details. If
142 * the `ok()` member function in the `StatusOr<T>` returns `true` then it
143 * contains the expected result. Please consult the [`StatusOr<T>`
144 * documentation](#google::cloud::StatusOr) for more details.
145 *
146 * @code
147 * namespace gcs = google::cloud::storage;
148 * gcs::Client client = ...;
149 * google::cloud::StatusOr<gcs::BucketMetadata> bucket_metadata =
150 * client.GetBucketMetadata("my-bucket");
151 *
152 * if (!bucket_metadata) {
153 * std::cerr << "Error getting metadata for my-bucket: "
154 * << bucket_metadata.status() << "\n";
155 * return;
156 * }
157 *
158 * // Use bucket_metadata as a smart pointer here, e.g.:
159 * std::cout << "The generation for " << bucket_metadata->name() " is "
160 * << bucket_metadata->generation() << "\n";
161 * @endcode
162 *
163 * In addition, the @ref index "main page" contains examples using `StatusOr<T>`
164 * to handle errors.
165 *
166 * @par Optional Request Options
167 * Most of the member functions in this class can receive optional request
168 * options. For example, the default when reading multi-version objects is to
169 * retrieve the latest version:
170 *
171 * @code
172 * auto stream = gcs.ReadObject("my-bucket", "my-object");
173 * @endcode
174 *
175 * Some applications may want to retrieve specific versions. In this case
176 * just provide the `Generation` request option:
177 *
178 * @code
179 * auto stream = gcs.ReadObject(
180 * "my-bucket", "my-object", gcs::Generation(generation));
181 * @endcode
182 *
183 * Each function documents the types accepted as optional request options. These
184 * parameters can be specified in any order. Specifying a request option that is
185 * not applicable to a member function results in a compile-time error.
186 *
187 * All operations support the following common request options:
188 *
189 * - `Fields`: return a [partial response], which includes only the desired
190 * fields.
191 * - `QuotaUser`: attribute the request to this specific label for quota
192 * purposes.
193 * - `UserProject`: change the request costs (if applicable) to this GCP
194 * project.
195 * - `CustomHeader`: include a custom header with the request. These are
196 * typically used for testing, though they are sometimes helpful if
197 * environments where HTTPS traffic is mediated by a proxy.
198 * - `IfMatchEtag`: a pre-condition, the operation succeeds only if the resource
199 * ETag matches. Typically used in OCC loops ("change X only if its Etag is
200 * still Y"). Note that GCS sometimes ignores this header, we recommend you
201 * use the GCS specific pre-conditions (e.g., `IfGenerationMatch`,
202 * `IfMetagenerationMatch` and their `*NotMatch` counterparts) instead.
203 * - `IfNoneMatchEtag`: a pre-condition, abort the operation if the resource
204 * ETag has not changed. Typically used in caching ("return the contents of X
205 * only if the Etag is different from the last value I got, which was Y").
206 * Note that GCS sometimes ignores this header, we recommend you use the GCS
207 * specific pre-conditions (e.g., `IfGenerationMatch`, `IfMetagenerationMatch`
208 * and their `*NotMatch` counterparts) instead.
209 * - `UserIp`: attribute the request to this specific IP address for quota
210 * purpose. Not recommended, prefer `QuotaUser` instead.
211 *
212 * [partial response]:
213 * https://cloud.google.com/storage/docs/json_api#partial-response
214 *
215 * @par Per-operation Overrides
216 *
217 * In addition to the request options, which are passed on to the service to
218 * modify the request, you can specify options that override the local behavior
219 * of the library. For example, you can override the local retry policy:
220 *
221 * @snippet storage_client_per_operation_samples.cc change-retry-policy
222 *
223 * @par Retry, Backoff, and Idempotency Policies
224 *
225 * The library automatically retries requests that fail with transient errors,
226 * and follows the [recommended practice][exponential-backoff] to backoff
227 * between retries.
228 *
229 * The default policies are to continue retrying for up to 15 minutes, and to
230 * use truncated (at 5 minutes) exponential backoff, doubling the maximum
231 * backoff period between retries. Likewise, the idempotency policy is
232 * configured to retry all operations.
233 *
234 * The application can override these policies when constructing objects of this
235 * class. The documentation for the constructors show examples of this in
236 * action.
237 *
238 * [exponential-backoff]:
239 * https://cloud.google.com/storage/docs/exponential-backoff
240 *
241 * @see https://cloud.google.com/storage/ for an overview of GCS.
242 *
243 * @see https://cloud.google.com/storage/docs/key-terms for an introduction of
244 * the key terms used in GCS.
245 *
246 * @see https://cloud.google.com/storage/docs/json_api/ for an overview of the
247 * underlying API.
248 *
249 * @see https://cloud.google.com/docs/authentication/production for details
250 * about Application Default %Credentials.
251 *
252 * @see #google::cloud::StatusOr.
253 *
254 * @see `LimitedTimeRetryPolicy` and `LimitedErrorCountRetryPolicy` for
255 * alternative retry policies.
256 *
257 * @see `ExponentialBackoffPolicy` to configure different parameters for the
258 * exponential backoff policy.
259 *
260 * @see `AlwaysRetryIdempotencyPolicy` and `StrictIdempotencyPolicy` for
261 * alternative idempotency policies.
262 */
263class Client {
264 public:
265 /**
266 * Build a new client.
267 *
268 * @param opts the configuration parameters for the `Client`.
269 *
270 * @see #ClientOptionList for a list of useful options.
271 *
272 * @par Idempotency Policy Example
273 * @snippet storage_object_samples.cc insert object strict idempotency
274 *
275 * @par Modified Retry Policy Example
276 * @snippet storage_object_samples.cc insert object modified retry
277 *
278 * @par Change Credentials Example
279 * @snippet storage_client_initialization_samples.cc service-account-keyfile
280 */
281 explicit Client(Options opts = {});
282
283 /// @name Equality
284 ///@{
285 friend bool operator==(Client const& a, Client const& b) {
286 return a.raw_client_ == b.raw_client_;
287 }
288 friend bool operator!=(Client const& a, Client const& b) { return !(a == b); }
289 ///@}
290
291 /**
292 * @name Bucket operations.
293 *
294 * Buckets are the basic containers that hold your data. Everything that you
295 * store in GCS must be contained in a bucket. You can use buckets to organize
296 * your data and control access to your data, but unlike directories and
297 * folders, you cannot nest buckets.
298 *
299 * @see https://cloud.google.com/storage/docs/key-terms#buckets for more
300 * information about GCS buckets.
301 */
302 ///@{
303 /**
304 * Fetches the list of buckets for a given project.
305 *
306 * @param project_id the project to query.
307 * @param options a list of optional query parameters and/or request headers.
308 * Valid types for this operation include `MaxResults`, `Prefix`,
309 * `Projection`, and `UserProject`. `OverrideDefaultProject` is accepted,
310 * but has no effect.
311 *
312 * @par Idempotency
313 * This is a read-only operation and is always idempotent.
314 *
315 * @par Example
316 * @snippet storage_bucket_samples.cc list buckets for project
317 */
318 template <typename... Options>
319 ListBucketsReader ListBucketsForProject(std::string const& project_id,
320 Options&&... options) {
321 google::cloud::internal::OptionsSpan const span(
322 SpanOptions(std::forward<Options>(options)...));
323 internal::ListBucketsRequest request(project_id);
324 request.set_multiple_options(std::forward<Options>(options)...);
325 auto& client = raw_client_;
326 return google::cloud::internal::MakePaginationRange<ListBucketsReader>(
327 request,
328 [client](internal::ListBucketsRequest const& r) {
329 return client->ListBuckets(r);
330 },
331 [](internal::ListBucketsResponse r) { return std::move(r.items); });
332 }
333
334 /**
335 * Fetches the list of buckets for the default project.
336 *
337 * The default project is required to be configured in the `ClientOptions`
338 * used to construct this object. If the application does not set the project
339 * id in the `ClientOptions`, the value of the `GOOGLE_CLOUD_PROJECT` is
340 * used. If neither the environment variable is set, nor a value is set
341 * explicitly by the application, the returned `ListBucketsReader` will
342 * return an error status when used.
343 *
344 * @param options a list of optional query parameters and/or request headers.
345 * Valid types for this operation include `MaxResults`, `Prefix`,
346 * `Projection`, `UserProject`, and `OverrideDefaultProject`.
347 *
348 * @par Idempotency
349 * This is a read-only operation and is always idempotent.
350 *
351 * @par Example
352 * @snippet storage_bucket_samples.cc list buckets
353 */
354 template <typename... Options>
355 ListBucketsReader ListBuckets(Options&&... options) {
356 auto opts = SpanOptions(std::forward<Options>(options)...);
357 auto project_id = storage_internal::RequestProjectId(
358 GCP_ERROR_INFO(), opts, std::forward<Options>(options)...);
359 if (!project_id) {
360 return google::cloud::internal::MakeErrorPaginationRange<
361 ListBucketsReader>(std::move(project_id).status());
362 }
363 google::cloud::internal::OptionsSpan const span(std::move(opts));
364 return ListBucketsForProject(*std::move(project_id),
365 std::forward<Options>(options)...);
366 }
367
368 /**
369 * Creates a new Google Cloud Storage bucket using the default project. If
370 * the default project is not configured the server will reject the request,
371 * and this function returns the error status.
372 *
373 * @param bucket_name the name of the new bucket.
374 * @param metadata the metadata for the new Bucket. The `name` field is
375 * ignored in favor of @p bucket_name.
376 * @param options a list of optional query parameters and/or request headers.
377 * Valid types for this operation include `PredefinedAcl`,
378 * `PredefinedDefaultObjectAcl`, `Projection`, `UserProject`,
379 * and `OverrideDefaultProject`.
380 *
381 * @par Idempotency
382 * This operation is always idempotent. It fails if the bucket already exists.
383 *
384 * @par Example
385 * @snippet storage_bucket_samples.cc create bucket
386 *
387 * @see Before enabling Uniform Bucket Level Access please review the
388 * [feature documentation][ubla-link], as well as
389 * ["Should you use uniform bucket-level access ?"][ubla-should-link].
390 *
391 * [ubla-link]:
392 * https://cloud.google.com/storage/docs/uniform-bucket-level-access
393 * [ubla-should-link]:
394 * https://cloud.google.com/storage/docs/uniform-bucket-level-access#should-you-use
395 */
396 template <typename... Options>
397 StatusOr<BucketMetadata> CreateBucket(std::string bucket_name,
398 BucketMetadata metadata,
399 Options&&... options) {
400 auto opts = SpanOptions(std::forward<Options>(options)...);
401 auto project_id = storage_internal::RequestProjectId(
402 GCP_ERROR_INFO(), opts, std::forward<Options>(options)...);
403 if (!project_id) return std::move(project_id).status();
404 google::cloud::internal::OptionsSpan const span(std::move(opts));
405 metadata.set_name(std::move(bucket_name));
406 internal::CreateBucketRequest request(*std::move(project_id),
407 std::move(metadata));
408 request.set_multiple_options(std::forward<Options>(options)...);
409 return raw_client_->CreateBucket(request);
410 }
411
412 /**
413 * Creates a new Google Cloud Storage Bucket in a given project.
414 *
415 * @param bucket_name the name of the new bucket.
416 * @param project_id the id of the project that will host the new bucket.
417 * @param metadata the metadata for the new Bucket. The `name` field is
418 * ignored in favor of @p bucket_name.
419 * @param options a list of optional query parameters and/or request headers.
420 * Valid types for this operation include `PredefinedAcl`,
421 * `PredefinedDefaultObjectAcl`, `Projection`, and `UserProject`.
422 * `OverrideDefaultProject` is accepted, but has no effect.
423 *
424 * @par Idempotency
425 * This operation is always idempotent. It fails if the bucket already exists.
426 *
427 * @par Example
428 * @snippet storage_bucket_samples.cc create bucket for project
429 *
430 * @see Before enabling Uniform Bucket Level Access please review the
431 * [feature documentation][ubla-link], as well as
432 * ["Should you use uniform bucket-level access ?"][ubla-should-link].
433 *
434 * [ubla-link]:
435 * https://cloud.google.com/storage/docs/uniform-bucket-level-access
436 * [ubla-should-link]:
437 * https://cloud.google.com/storage/docs/uniform-bucket-level-access#should-you-use
438 */
439 template <typename... Options>
440 StatusOr<BucketMetadata> CreateBucketForProject(std::string bucket_name,
441 std::string project_id,
442 BucketMetadata metadata,
443 Options&&... options) {
444 google::cloud::internal::OptionsSpan const span(
445 SpanOptions(std::forward<Options>(options)...));
446 metadata.set_name(std::move(bucket_name));
447 internal::CreateBucketRequest request(std::move(project_id),
448 std::move(metadata));
449 request.set_multiple_options(std::forward<Options>(options)...);
450 return raw_client_->CreateBucket(request);
451 }
452
453 /**
454 * Fetches the bucket metadata.
455 *
456 * @param bucket_name query metadata information about this bucket.
457 * @param options a list of optional query parameters and/or request headers.
458 * Valid types for this operation include `IfMetagenerationMatch`,
459 * `IfMetagenerationNotMatch`, `UserProject`, and `Projection`.
460 *
461 * @par Idempotency
462 * This is a read-only operation and is always idempotent.
463 *
464 * @par Example
465 * @snippet storage_bucket_samples.cc get bucket metadata
466 */
467 template <typename... Options>
468 StatusOr<BucketMetadata> GetBucketMetadata(std::string const& bucket_name,
469 Options&&... options) {
470 google::cloud::internal::OptionsSpan const span(
471 SpanOptions(std::forward<Options>(options)...));
472 internal::GetBucketMetadataRequest request(bucket_name);
473 request.set_multiple_options(std::forward<Options>(options)...);
474 return raw_client_->GetBucketMetadata(request);
475 }
476
477 /**
478 * Deletes a Google Cloud Storage Bucket.
479 *
480 * @param bucket_name the bucket to be deleted.
481 * @param options a list of optional query parameters and/or request headers.
482 * Valid types for this operation include `IfMetagenerationMatch`,
483 * `IfMetagenerationNotMatch`, and `UserProject`.
484 *
485 * @par Idempotency
486 * This operation is only idempotent if restricted by pre-conditions, in this
487 * case, `IfMetagenerationMatch`.
488 *
489 * @par Example
490 * @snippet storage_bucket_samples.cc delete bucket
491 */
492 template <typename... Options>
493 Status DeleteBucket(std::string const& bucket_name, Options&&... options) {
494 google::cloud::internal::OptionsSpan const span(
495 SpanOptions(std::forward<Options>(options)...));
496 internal::DeleteBucketRequest request(bucket_name);
497 request.set_multiple_options(std::forward<Options>(options)...);
498 return raw_client_->DeleteBucket(request).status();
499 }
500
501 /**
502 * Updates the metadata in a Google Cloud Storage Bucket.
503 *
504 * A `Buckets: update` request changes *all* the writeable attributes of a
505 * bucket, in contrast, a `Buckets: patch` request only changes the subset of
506 * the attributes included in the request. This function creates a
507 * `Buckets: update` request to change the writable attributes in
508 * `BucketMetadata`.
509 *
510 * @param bucket_name the name of the new bucket.
511 * @param metadata the new metadata for the Bucket. The `name` field is
512 * ignored in favor of @p bucket_name.
513 * @param options a list of optional query parameters and/or request headers.
514 * Valid types for this operation include `IfMetagenerationMatch`,
515 * `IfMetagenerationNotMatch`, `PredefinedAcl`,
516 * `PredefinedDefaultObjectAcl`, `Projection`, and `UserProject`.
517 *
518 * @par Idempotency
519 * This operation is only idempotent if restricted by pre-conditions, in this
520 * case,`IfMetagenerationMatch`.
521 *
522 * @par Example
523 * @snippet storage_bucket_samples.cc update bucket
524 *
525 * @see Before enabling Uniform Bucket Level Access please review the
526 * [feature documentation][ubla-link], as well as
527 * ["Should you use uniform bucket-level access ?"][ubla-should-link].
528 *
529 * [ubla-link]:
530 * https://cloud.google.com/storage/docs/uniform-bucket-level-access
531 * [ubla-should-link]:
532 * https://cloud.google.com/storage/docs/uniform-bucket-level-access#should-you-use
533 */
534 template <typename... Options>
535 StatusOr<BucketMetadata> UpdateBucket(std::string bucket_name,
536 BucketMetadata metadata,
537 Options&&... options) {
538 google::cloud::internal::OptionsSpan const span(
539 SpanOptions(std::forward<Options>(options)...));
540 metadata.set_name(std::move(bucket_name));
541 internal::UpdateBucketRequest request(std::move(metadata));
542 request.set_multiple_options(std::forward<Options>(options)...);
543 return raw_client_->UpdateBucket(request);
544 }
545
546 /**
547 * Computes the difference between two BucketMetadata objects and patches a
548 * bucket based on that difference.
549 *
550 * A `Buckets: update` request changes *all* the writeable attributes of a
551 * bucket, in contrast, a `Buckets: patch` request only changes the subset of
552 * the attributes included in the request.
553 *
554 * This function creates a patch request to change the writeable attributes in
555 * @p original to the values in @p updated. Non-writeable attributes are
556 * ignored, and attributes not present in @p updated are removed. Typically
557 * this function is used after the application obtained a value with
558 * `GetBucketMetadata` and has modified these parameters.
559 *
560 * @param bucket_name the bucket to be updated.
561 * @param original the initial value of the bucket metadata.
562 * @param updated the updated value for the bucket metadata.
563 * @param options a list of optional query parameters and/or request headers.
564 * Valid types for this operation include `IfMetagenerationMatch`,
565 * `IfMetagenerationNotMatch`, `PredefinedAcl`,
566 * `PredefinedDefaultObjectAcl`, `Projection`, and `UserProject`.
567 *
568 * @par Idempotency
569 * This operation is only idempotent if restricted by pre-conditions, in this
570 * case, `IfMetagenerationMatch`.
571 *
572 * @par Example
573 * @snippet storage_bucket_samples.cc patch bucket storage class
574 *
575 * @see Before enabling Uniform Bucket Level Access please review the
576 * [feature documentation][ubla-link], as well as
577 * ["Should you use uniform bucket-level access?"][ubla-should-link].
578 *
579 * [ubla-link]:
580 * https://cloud.google.com/storage/docs/uniform-bucket-level-access
581 * [ubla-should-link]:
582 * https://cloud.google.com/storage/docs/uniform-bucket-level-access#should-you-use
583 */
584 template <typename... Options>
585 StatusOr<BucketMetadata> PatchBucket(std::string bucket_name,
586 BucketMetadata const& original,
587 BucketMetadata const& updated,
588 Options&&... options) {
589 google::cloud::internal::OptionsSpan const span(
590 SpanOptions(std::forward<Options>(options)...));
591 internal::PatchBucketRequest request(std::move(bucket_name), original,
592 updated);
593 request.set_multiple_options(std::forward<Options>(options)...);
594 return raw_client_->PatchBucket(request);
595 }
596
597 /**
598 * Patches the metadata in a Google Cloud Storage Bucket given a desired set
599 * changes.
600 *
601 * A `Buckets: update` request changes *all* the writeable attributes of a
602 * bucket, in contrast, a `Buckets: patch` request only changes the subset of
603 * the attributes included in the request. This function creates a patch
604 * request based on the given `BucketMetadataPatchBuilder` which represents
605 * the desired set of changes.
606 *
607 * @param bucket_name the bucket to be updated.
608 * @param builder the set of updates to perform in the Bucket.
609 * @param options a list of optional query parameters and/or request headers.
610 * Valid types for this operation include `IfMetagenerationMatch`,
611 * `IfMetagenerationNotMatch`, `PredefinedAcl`,
612 * `PredefinedDefaultObjectAcl`, `Projection`, and `UserProject`.
613 *
614 * @par Idempotency
615 * This operation is only idempotent if restricted by pre-conditions, in this
616 * case, `IfMetagenerationMatch`.
617 *
618 * @par Example
619 * @snippet storage_bucket_samples.cc patch bucket storage class with builder
620 *
621 * @see Before enabling Uniform Bucket Level Access please review the
622 * [feature documentation][ubla-link], as well as
623 * ["Should you use uniform bucket-level access ?"][ubla-should-link].
624 *
625 * [ubla-link]:
626 * https://cloud.google.com/storage/docs/uniform-bucket-level-access
627 * [ubla-should-link]:
628 * https://cloud.google.com/storage/docs/uniform-bucket-level-access#should-you-use
629 */
630 template <typename... Options>
631 StatusOr<BucketMetadata> PatchBucket(
632 std::string bucket_name, BucketMetadataPatchBuilder const& builder,
633 Options&&... options) {
634 google::cloud::internal::OptionsSpan const span(
635 SpanOptions(std::forward<Options>(options)...));
636 internal::PatchBucketRequest request(std::move(bucket_name), builder);
637 request.set_multiple_options(std::forward<Options>(options)...);
638 return raw_client_->PatchBucket(request);
639 }
640
641 /**
642 * Fetches the native
643 * [IAM policy](@ref google::cloud::storage::NativeIamPolicy) for a Bucket.
644 *
645 * Google Cloud Identity & Access Management (IAM) lets administrators
646 * authorize who can take action on specific resources, including Google
647 * Cloud Storage Buckets. This operation allows you to query the IAM policies
648 * for a Bucket. IAM policies are a superset of the Bucket ACL, changes
649 * to the Bucket ACL are reflected in the IAM policy, and vice-versa. The
650 * documentation describes
651 * [the
652 * mapping](https://cloud.google.com/storage/docs/access-control/iam#acls)
653 * between legacy Bucket ACLs and IAM policies.
654 *
655 * Consult
656 * [the
657 * documentation](https://cloud.google.com/storage/docs/access-control/iam)
658 * for a more detailed description of IAM policies and their use in
659 * Google Cloud Storage.
660 *
661 * @param bucket_name query metadata information about this bucket.
662 * @param options a list of optional query parameters and/or request headers.
663 * Valid types for this operation include `UserProject`.
664 *
665 * @par Idempotency
666 * This is a read-only operation and is always idempotent.
667 *
668 * @par Example
669 * @snippet storage_bucket_iam_samples.cc native get bucket iam policy
670 *
671 * @see #google::cloud::storage::NativeIamPolicy for details about the
672 * `NativeIamPolicy` class.
673 */
674 template <typename... Options>
676 std::string const& bucket_name, Options&&... options) {
677 google::cloud::internal::OptionsSpan const span(
678 SpanOptions(std::forward<Options>(options)...));
679 internal::GetBucketIamPolicyRequest request(bucket_name);
680 request.set_multiple_options(std::forward<Options>(options)...);
681 return raw_client_->GetNativeBucketIamPolicy(request);
682 }
683
684 /**
685 * Sets the native
686 * [IAM Policy](@ref google::cloud::storage::NativeIamPolicy) for a Bucket.
687 *
688 * Google Cloud Identity & Access Management (IAM) lets administrators
689 * authorize who can take action on specific resources, including Google
690 * Cloud Storage Buckets. This operation allows you to set the IAM policies
691 * for a Bucket. IAM policies are a superset of the Bucket ACL, changes
692 * to the Bucket ACL are reflected in the IAM policy, and vice-versa. The
693 * documentation describes
694 * [the
695 * mapping](https://cloud.google.com/storage/docs/access-control/iam#acls)
696 * between legacy Bucket ACLs and IAM policies.
697 *
698 * Consult
699 * [the
700 * documentation](https://cloud.google.com/storage/docs/access-control/iam)
701 * for a more detailed description of IAM policies their use in
702 * Google Cloud Storage.
703 *
704 * @note The server rejects requests where the ETag value of the policy does
705 * not match the current ETag. Effectively this means that applications must
706 * use `GetNativeBucketIamPolicy()` to fetch the current value and ETag
707 * before calling `SetNativeBucketIamPolicy()`. Applications should use
708 * optimistic concurrency control techniques to retry changes in case some
709 * other application modified the IAM policy between the
710 * `GetNativeBucketIamPolicy` and `SetNativeBucketIamPolicy` calls.
711 *
712 * @param bucket_name query metadata information about this bucket.
713 * @param iam_policy the new IAM policy.
714 * @param options a list of optional query parameters and/or request headers.
715 * Valid types for this operation include `UserProject`.
716 *
717 * @par Idempotency
718 * This operation is only idempotent if restricted by pre-conditions, in this
719 * case, `IfMetagenerationMatch`.
720 *
721 * @par Example: adding a new member
722 * @snippet storage_bucket_iam_samples.cc native add bucket iam member
723 *
724 * @par Example: removing a IAM member
725 * @snippet storage_bucket_iam_samples.cc native remove bucket iam member
726 *
727 * @see #google::cloud::storage::NativeIamPolicy for details about the
728 * `NativeIamPolicy` class.
729 */
730 template <typename... Options>
732 std::string const& bucket_name, NativeIamPolicy const& iam_policy,
733 Options&&... options) {
734 google::cloud::internal::OptionsSpan const span(
735 SpanOptions(std::forward<Options>(options)...));
736 internal::SetNativeBucketIamPolicyRequest request(bucket_name, iam_policy);
737 request.set_multiple_options(std::forward<Options>(options)...);
738 return raw_client_->SetNativeBucketIamPolicy(request);
739 }
740
741 /**
742 * Tests the IAM permissions of the caller against a Bucket.
743 *
744 * Google Cloud Identity & Access Management (IAM) lets administrators
745 * authorize who can take action on specific resources, including Google
746 * Cloud Storage Buckets. This operation tests the permissions of the caller
747 * for a Bucket. You must provide a list of permissions, this API will return
748 * the subset of those permissions that the current caller has in the given
749 * Bucket.
750 *
751 * Consult
752 * [the
753 * documentation](https://cloud.google.com/storage/docs/access-control/iam)
754 * for a more detailed description of IAM policies their use in
755 * Google Cloud Storage.
756 *
757 * @param bucket_name query metadata information about this bucket.
758 * @param permissions the list of permissions to check.
759 * @param options a list of optional query parameters and/or request headers.
760 * Valid types for this operation include `UserProject`.
761 *
762 * @par Idempotency
763 * This is a read-only operation and is always idempotent.
764 *
765 * @par Example
766 * @snippet storage_bucket_iam_samples.cc test bucket iam permissions
767 */
768 template <typename... Options>
769 StatusOr<std::vector<std::string>> TestBucketIamPermissions(
770 std::string bucket_name, std::vector<std::string> permissions,
771 Options&&... options) {
772 google::cloud::internal::OptionsSpan const span(
773 SpanOptions(std::forward<Options>(options)...));
774 internal::TestBucketIamPermissionsRequest request(std::move(bucket_name),
775 std::move(permissions));
776 request.set_multiple_options(std::forward<Options>(options)...);
777 auto result = raw_client_->TestBucketIamPermissions(request);
778 if (!result) {
779 return std::move(result).status();
780 }
781 return std::move(result.value().permissions);
782 }
783
784 /**
785 * Locks the retention policy for a bucket.
786 *
787 * @warning Locking a retention policy is an irreversible action. Once locked,
788 * you must delete the entire bucket in order to "remove" the bucket's
789 * retention policy. However, before you can delete the bucket, you must
790 * be able to delete all the objects in the bucket, which itself is only
791 * possible if all the objects have reached the retention period set by
792 * the retention policy.
793 *
794 * The [Bucket Lock
795 * feature](https://cloud.google.com/storage/docs/bucket-lock) allows you to
796 * configure a data retention policy for a Cloud Storage bucket that governs
797 * how long objects in the bucket must be retained. The feature also allows
798 * you to lock the data retention policy, permanently preventing the policy
799 * from being reduced or removed.
800 *
801 * @param bucket_name the name of the bucket.
802 * @param metageneration the expected value of the metageneration on the
803 * bucket. The request will fail if the metageneration does not match the
804 * current value.
805 * @param options a list of optional query parameters and/or request headers.
806 * Valid types for this operation include `UserProject`.
807 *
808 * @par Idempotency
809 * This operation is always idempotent because the `metageneration` parameter
810 * is always required, and it acts as a pre-condition on the operation.
811 *
812 * @par Example: lock the retention policy
813 * @snippet storage_retention_policy_samples.cc lock retention policy
814 *
815 * @par Example: get the current retention policy
816 * @snippet storage_retention_policy_samples.cc get retention policy
817 *
818 * @par Example: set the current retention policy
819 * @snippet storage_retention_policy_samples.cc set retention policy
820 *
821 * @par Example: remove the retention policy
822 * @snippet storage_retention_policy_samples.cc remove retention policy
823 *
824 * @see https://cloud.google.com/storage/docs/bucket-lock for a description of
825 * the Bucket Lock feature.
826 *
827 * @see https://cloud.google.com/storage/docs/using-bucket-lock for examples
828 * of how to use the Bucket Lock and retention policy features.
829 */
830 template <typename... Options>
832 std::string const& bucket_name, std::uint64_t metageneration,
833 Options&&... options) {
834 google::cloud::internal::OptionsSpan const span(
835 SpanOptions(std::forward<Options>(options)...));
836 internal::LockBucketRetentionPolicyRequest request(bucket_name,
837 metageneration);
838 request.set_multiple_options(std::forward<Options>(options)...);
839 return raw_client_->LockBucketRetentionPolicy(request);
840 }
841 ///@}
842
843 /**
844 * @name Object operations
845 *
846 * Objects are the individual pieces of data that you store in GCS. Objects
847 * have two components: *object data* and *object metadata*. Object data
848 * (sometimes referred to as *media*) is typically a file that you want
849 * to store in GCS. Object metadata is information that describe various
850 * object qualities.
851 *
852 * @see https://cloud.google.com/storage/docs/key-terms#objects for more
853 * information about GCS objects.
854 */
855 ///@{
856 /**
857 * Creates an object given its name and contents.
858 *
859 * @param bucket_name the name of the bucket that will contain the object.
860 * @param object_name the name of the object to be created.
861 * @param contents the contents (media) for the new object.
862 * @param options a list of optional query parameters and/or request headers.
863 * Valid types for this operation include `ContentEncoding`,
864 * `ContentType`, `Crc32cChecksumValue`, `DisableCrc32cChecksum`,
865 * `DisableMD5Hash`, `EncryptionKey`, `IfGenerationMatch`,
866 * `IfGenerationNotMatch`, `IfMetagenerationMatch`,
867 * `IfMetagenerationNotMatch`, `KmsKeyName`, `MD5HashValue`,
868 * `PredefinedAcl`, `Projection`, `UserProject`, and `WithObjectMetadata`.
869 *
870 * @par Idempotency
871 * This operation is only idempotent if restricted by pre-conditions, in this
872 * case, `IfGenerationMatch`.
873 *
874 * @par Example
875 * @snippet storage_object_samples.cc insert object
876 *
877 * @par Example
878 * @snippet storage_object_samples.cc insert object multipart
879 */
880 template <typename... Options>
881 StatusOr<ObjectMetadata> InsertObject(std::string const& bucket_name,
882 std::string const& object_name,
883 absl::string_view contents,
884 Options&&... options) {
885 google::cloud::internal::OptionsSpan const span(
886 SpanOptions(std::forward<Options>(options)...));
887 internal::InsertObjectMediaRequest request(bucket_name, object_name,
888 contents);
889 request.set_multiple_options(std::forward<Options>(options)...);
890 return raw_client_->InsertObjectMedia(request);
891 }
892
893 /// @overload InsertObject(std::string const& bucket_name, std::string const& object_name, absl::string_view contents, Options&&... options)
894 template <typename... Options>
895 StatusOr<ObjectMetadata> InsertObject(std::string const& bucket_name,
896 std::string const& object_name,
897 std::string const& contents,
898 Options&&... options) {
899 return InsertObject(bucket_name, object_name, absl::string_view(contents),
900 std::forward<Options>(options)...);
901 }
902
903 /// @overload InsertObject(std::string const& bucket_name, std::string const& object_name, absl::string_view contents, Options&&... options)
904 template <typename... Options>
905 StatusOr<ObjectMetadata> InsertObject(std::string const& bucket_name,
906 std::string const& object_name,
907 char const* contents,
908 Options&&... options) {
909 auto c =
910 contents == nullptr ? absl::string_view{} : absl::string_view{contents};
911 return InsertObject(bucket_name, object_name, c,
912 std::forward<Options>(options)...);
913 }
914
915 /**
916 * Copies an existing object.
917 *
918 * Use `CopyObject` to copy between objects in the same location and storage
919 * class. Copying objects across locations or storage classes can fail for
920 * large objects and retrying the operation will not succeed.
921 *
922 * @note Prefer using `RewriteObject()` to copy objects, `RewriteObject()` can
923 * copy objects to different locations, with different storage class,
924 * and/or with different encryption keys.
925 *
926 * @param source_bucket_name the name of the bucket that contains the object
927 * to be copied.
928 * @param source_object_name the name of the object to copy.
929 * @param destination_bucket_name the name of the bucket that will contain the
930 * new object.
931 * @param destination_object_name the name of the new object.
932 * @param options a list of optional query parameters and/or request headers.
933 * Valid types for this operation include `DestinationKmsKeyName`,
934 * `DestinationPredefinedAcl`,`EncryptionKey`,`IfGenerationMatch`,
935 * `IfGenerationNotMatch`, `IfMetagenerationMatch`,
936 * `IfMetagenerationNotMatch`, `IfSourceGenerationMatch`,
937 * `IfSourceGenerationNotMatch`, `IfSourceMetagenerationMatch`,
938 * `IfSourceMetagenerationNotMatch`, `Projection`, `SourceGeneration`,
939 * `SourceEncryptionKey`, `UserProject`, and `WithObjectMetadata`.
940 *
941 * @par Idempotency
942 * This operation is only idempotent if restricted by pre-conditions, in this
943 * case, `IfGenerationMatch`.
944 *
945 * @par Example
946 * @snippet storage_object_samples.cc copy object
947 *
948 * @par Example: copy an encrypted object
949 * @snippet storage_object_csek_samples.cc copy encrypted object
950 *
951 * @see https://cloud.google.com/storage/docs/json_api/v1/objects/copy for
952 * a full description of the advantages of `Objects: rewrite` over
953 * `Objects: copy`.
954 */
955 template <typename... Options>
956 StatusOr<ObjectMetadata> CopyObject(std::string source_bucket_name,
957 std::string source_object_name,
958 std::string destination_bucket_name,
959 std::string destination_object_name,
960 Options&&... options) {
961 google::cloud::internal::OptionsSpan const span(
962 SpanOptions(std::forward<Options>(options)...));
963 internal::CopyObjectRequest request(
964 std::move(source_bucket_name), std::move(source_object_name),
965 std::move(destination_bucket_name), std::move(destination_object_name));
966 request.set_multiple_options(std::forward<Options>(options)...);
967 return raw_client_->CopyObject(request);
968 }
969
970 /**
971 * Fetches the object metadata.
972 *
973 * @param bucket_name the bucket containing the object.
974 * @param object_name the object name.
975 * @param options a list of optional query parameters and/or request headers.
976 * Valid types for this operation include `Generation`,
977 * `IfGenerationMatch`, `IfGenerationNotMatch`, `IfMetagenerationMatch`,
978 * `IfMetagenerationNotMatch`, `Projection`, and `UserProject`.
979 *
980 * @par Idempotency
981 * This is a read-only operation and is always idempotent.
982 *
983 * @par Example
984 * @snippet storage_object_samples.cc get object metadata
985 */
986 template <typename... Options>
987 StatusOr<ObjectMetadata> GetObjectMetadata(std::string const& bucket_name,
988 std::string const& object_name,
989 Options&&... options) {
990 google::cloud::internal::OptionsSpan const span(
991 SpanOptions(std::forward<Options>(options)...));
992 internal::GetObjectMetadataRequest request(bucket_name, object_name);
993 request.set_multiple_options(std::forward<Options>(options)...);
994 return raw_client_->GetObjectMetadata(request);
995 }
996
997 /**
998 * Lists the objects in a bucket.
999 *
1000 * @param bucket_name the name of the bucket to list.
1001 * @param options a list of optional query parameters and/or request headers.
1002 * Valid types for this operation include `MaxResults`, `Prefix`,
1003 * `Delimiter`, `IncludeTrailingDelimiter`, `StartOffset`, `EndOffset`,
1004 * `Projection`, `UserProject`, and `Versions`.
1005 *
1006 * @par Idempotency
1007 * This is a read-only operation and is always idempotent.
1008 *
1009 * @par Example
1010 * @snippet storage_object_samples.cc list objects
1011 */
1012 template <typename... Options>
1013 ListObjectsReader ListObjects(std::string const& bucket_name,
1014 Options&&... options) {
1015 google::cloud::internal::OptionsSpan const span(
1016 SpanOptions(std::forward<Options>(options)...));
1017 internal::ListObjectsRequest request(bucket_name);
1018 request.set_multiple_options(std::forward<Options>(options)...);
1019 auto& client = raw_client_;
1020 return google::cloud::internal::MakePaginationRange<ListObjectsReader>(
1021 request,
1022 [client](internal::ListObjectsRequest const& r) {
1023 return client->ListObjects(r);
1024 },
1025 [](internal::ListObjectsResponse r) { return std::move(r.items); });
1026 }
1027
1028 /**
1029 * Lists the objects and prefixes in a bucket.
1030 *
1031 * @param bucket_name the name of the bucket to list.
1032 * @param options a list of optional query parameters and/or request headers.
1033 * Valid types for this operation include
1034 * `IfMetagenerationMatch`, `IfMetagenerationNotMatch`, `UserProject`,
1035 * `Projection`, `Prefix`, `Delimiter`, `IncludeTrailingDelimiter`,
1036 * `StartOffset`, `EndOffset`, and `Versions`.
1037 *
1038 * @par Idempotency
1039 * This is a read-only operation and is always idempotent.
1040 *
1041 * @par Example
1042 * @snippet storage_object_samples.cc list objects and prefixes
1043 */
1044 template <typename... Options>
1045 ListObjectsAndPrefixesReader ListObjectsAndPrefixes(
1046 std::string const& bucket_name, Options&&... options) {
1047 google::cloud::internal::OptionsSpan const span(
1048 SpanOptions(std::forward<Options>(options)...));
1049 internal::ListObjectsRequest request(bucket_name);
1050 request.set_multiple_options(std::forward<Options>(options)...);
1051 return google::cloud::internal::MakePaginationRange<
1052 ListObjectsAndPrefixesReader>(
1053 request,
1054 [client = raw_client_](internal::ListObjectsRequest const& r) {
1055 return client->ListObjects(r);
1056 },
1057 [](internal::ListObjectsResponse r) {
1058 std::vector<ObjectOrPrefix> result;
1059 result.reserve(r.items.size() + r.prefixes.size());
1060 for (auto& item : r.items) {
1061 result.emplace_back(std::move(item));
1062 }
1063 for (auto& prefix : r.prefixes) {
1064 result.emplace_back(std::move(prefix));
1065 }
1066 internal::SortObjectsAndPrefixes(result);
1067 return result;
1068 });
1069 }
1070
1071 /**
1072 * Reads the contents of an object.
1073 *
1074 * Returns an object derived from `std::istream` which can be used to read the
1075 * contents of the GCS blob. The application should check the `badbit` (e.g.
1076 * by calling `stream.bad()`) on the returned object to detect if there was
1077 * an error reading from the blob. If `badbit` is set, the application can
1078 * check the `status()` variable to get details about the failure.
1079 * Applications can also set the exception mask on the returned stream, in
1080 * which case an exception is thrown if an error is detected.
1081 *
1082 * @param bucket_name the name of the bucket that contains the object.
1083 * @param object_name the name of the object to be read.
1084 * @param options a list of optional query parameters and/or request headers.
1085 * Valid types for this operation include `DisableCrc32cChecksum`,
1086 * `DisableMD5Hash`, `EncryptionKey`, `Generation`, `IfGenerationMatch`,
1087 * `IfGenerationNotMatch`, `IfMetagenerationMatch`,
1088 * `IfMetagenerationNotMatch`, `ReadFromOffset`, `ReadRange`, `ReadLast`,
1089 * `UserProject`, and `AcceptEncoding`.
1090 *
1091 * @par Idempotency
1092 * This is a read-only operation and is always idempotent.
1093 *
1094 * @par Example
1095 * @snippet storage_object_samples.cc read object
1096 *
1097 * @par Example: read only a sub-range in the object.
1098 * @snippet storage_object_samples.cc read object range
1099 *
1100 * @par Example: read a object encrypted with a CSEK.
1101 * @snippet storage_object_csek_samples.cc read encrypted object
1102 *
1103 * @par Example: disable decompressive transcoding.
1104 * @snippet storage_object_samples.cc read object gzip
1105 */
1106 template <typename... Options>
1107 ObjectReadStream ReadObject(std::string const& bucket_name,
1108 std::string const& object_name,
1109 Options&&... options) {
1110 struct HasReadRange
1111 : public absl::disjunction<std::is_same<ReadRange, Options>...> {};
1112 struct HasReadFromOffset
1113 : public absl::disjunction<std::is_same<ReadFromOffset, Options>...> {};
1114 struct HasReadLast
1115 : public absl::disjunction<std::is_same<ReadLast, Options>...> {};
1116
1117 struct HasIncompatibleRangeOptions
1118 : public std::integral_constant<bool, HasReadLast::value &&
1119 (HasReadFromOffset::value ||
1120 HasReadRange::value)> {};
1121
1122 static_assert(!HasIncompatibleRangeOptions::value,
1123 "Cannot set ReadLast option with either ReadFromOffset or "
1124 "ReadRange.");
1125
1126 google::cloud::internal::OptionsSpan const span(
1127 SpanOptions(std::forward<Options>(options)...));
1128 internal::ReadObjectRangeRequest request(bucket_name, object_name);
1129 request.set_multiple_options(std::forward<Options>(options)...);
1130 return ReadObjectImpl(request);
1131 }
1132
1133 /**
1134 * Writes contents into an object.
1135 *
1136 * This creates a `std::ostream` object to upload contents. The application
1137 * can use either the regular `operator<<()`, or `std::ostream::write()` to
1138 * upload data.
1139 *
1140 * This function always uses [resumable uploads][resumable-link]. The
1141 * application can provide a `#RestoreResumableUploadSession()` option to
1142 * resume a previously created upload. The returned object has accessors to
1143 * query the session id and the next byte expected by GCS.
1144 *
1145 * @note When resuming uploads it is the application's responsibility to save
1146 * the session id to restart the upload later. Likewise, it is the
1147 * application's responsibility to query the next expected byte and send
1148 * the remaining data without gaps or duplications.
1149 *
1150 * For small uploads we recommend using `InsertObject`, consult
1151 * [the documentation][how-to-upload-link] for details.
1152 *
1153 * If the application does not provide a `#RestoreResumableUploadSession()`
1154 * option, or it provides the `#NewResumableUploadSession()` option then a new
1155 * resumable upload session is created.
1156 *
1157 * More information about buffering and recommendations around performance in
1158 * the `ObjectWriteStream` class documentation.
1159 *
1160 * @param bucket_name the name of the bucket that contains the object.
1161 * @param object_name the name of the object to be read.
1162 * @param options a list of optional query parameters and/or request headers.
1163 * Valid types for this operation include `ContentEncoding`, `ContentType`,
1164 * `Crc32cChecksumValue`, `DisableCrc32cChecksum`, `DisableMD5Hash`,
1165 * `EncryptionKey`, `IfGenerationMatch`, `IfGenerationNotMatch`,
1166 * `IfMetagenerationMatch`, `IfMetagenerationNotMatch`, `KmsKeyName`,
1167 * `MD5HashValue`, `PredefinedAcl`, `Projection`,
1168 * `UseResumableUploadSession`, `UserProject`, `WithObjectMetadata`,
1169 * `UploadContentLength`, `AutoFinalize`, and `UploadBufferSize`.
1170 *
1171 * @par Idempotency
1172 * This operation is only idempotent if restricted by pre-conditions, in this
1173 * case, `IfGenerationMatch`.
1174 *
1175 * @par Example
1176 * @snippet storage_object_samples.cc write object
1177 *
1178 * @par Example: write an object with a CMEK.
1179 * @snippet storage_object_cmek_samples.cc write object with kms key
1180 *
1181 * @par Example: starting a resumable upload.
1182 * @snippet storage_object_resumable_write_samples.cc start resumable upload
1183 *
1184 * @par Example: resuming a resumable upload.
1185 * @snippet storage_object_resumable_write_samples.cc resume resumable upload
1186 *
1187 * @see [Resumable Uploads][resumable-link] for more information about
1188 * resumable uploads.
1189 *
1190 * [resumable-link]: https://cloud.google.com/storage/docs/resumable-uploads
1191 * [how-to-upload-link]:
1192 * https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload
1193 */
1194 template <typename... Options>
1195 ObjectWriteStream WriteObject(std::string const& bucket_name,
1196 std::string const& object_name,
1197 Options&&... options) {
1198 google::cloud::internal::OptionsSpan const span(
1199 SpanOptions(std::forward<Options>(options)...));
1200 internal::ResumableUploadRequest request(bucket_name, object_name);
1201 request.set_multiple_options(std::forward<Options>(options)...);
1202 return WriteObjectImpl(request);
1203 }
1204
1205 /**
1206 * Uploads a file to an object.
1207 *
1208 * @note
1209 * Only regular files are supported. If you need to upload the results of
1210 * reading a device, Named Pipe, FIFO, or other type of file system object
1211 * that is **not** a regular file then `WriteObject()` is probably a better
1212 * alternative.
1213 *
1214 * @param file_name the name of the file to be uploaded.
1215 * @param bucket_name the name of the bucket that contains the object.
1216 * @param object_name the name of the object to be read.
1217 * @param options a list of optional query parameters and/or request headers.
1218 * Valid types for this operation include `ContentEncoding`, `ContentType`,
1219 * `Crc32cChecksumValue`, `DisableCrc32cChecksum`, `DisableMD5Hash`,
1220 * `EncryptionKey`, `IfGenerationMatch`, `IfGenerationNotMatch`,
1221 * `IfMetagenerationMatch`, `IfMetagenerationNotMatch`, `KmsKeyName`,
1222 * `MD5HashValue`, `PredefinedAcl`, `Projection`, `UserProject`,
1223 * `UploadFromOffset`, `UploadLimit` and `WithObjectMetadata`.
1224 *
1225 * @par Idempotency
1226 * This operation is only idempotent if restricted by pre-conditions, in this
1227 * case, `IfGenerationMatch`.
1228 *
1229 * @par Example
1230 * @snippet storage_object_file_transfer_samples.cc upload file
1231 *
1232 * @par Example: manually selecting a resumable upload
1233 * @snippet storage_object_file_transfer_samples.cc upload file resumable
1234 */
1235 template <typename... Options>
1236 StatusOr<ObjectMetadata> UploadFile(std::string const& file_name,
1237 std::string const& bucket_name,
1238 std::string const& object_name,
1239 Options&&... options) {
1240 // Determine, at compile time, which version of UploadFileImpl we should
1241 // call. This needs to be done at compile time because ObjectInsertMedia
1242 // does not support (nor should it support) the UseResumableUploadSession
1243 // option.
1244 using HasUseResumableUpload =
1245 absl::disjunction<std::is_same<UseResumableUploadSession, Options>...>;
1246 return UploadFileImpl(file_name, bucket_name, object_name,
1247 HasUseResumableUpload{},
1248 std::forward<Options>(options)...);
1249 }
1250
1251 /**
1252 * Cancel a resumable upload.
1253 *
1254 * @param upload_session_url the url of the upload session. Returned by
1255 * `ObjectWriteStream::resumable_session_id`.
1256 * @param options a list of optional query parameters and/or request headers.
1257 * Valid types for this operation include `UserProject`.
1258 *
1259 * @par Idempotency
1260 * This operation is always idempotent because it only acts on a specific
1261 * `upload_session_url`.
1262 */
1263 template <typename... Options>
1264 Status DeleteResumableUpload(std::string const& upload_session_url,
1265 Options&&... options) {
1266 google::cloud::internal::OptionsSpan const span(
1267 SpanOptions(std::forward<Options>(options)...));
1268 internal::DeleteResumableUploadRequest request(upload_session_url);
1269 request.set_multiple_options(std::forward<Options>(options)...);
1270 return raw_client_->DeleteResumableUpload(request).status();
1271 }
1272
1273 /**
1274 * Downloads a Cloud Storage object to a file.
1275 *
1276 * @param bucket_name the name of the bucket that contains the object.
1277 * @param object_name the name of the object to be downloaded.
1278 * @param file_name the name of the destination file that will have the object
1279 * media.
1280 * @param options a list of optional query parameters and/or request headers.
1281 * Valid types for this operation include `IfGenerationMatch`,
1282 * `IfGenerationNotMatch`, `IfMetagenerationMatch`,
1283 * `IfMetagenerationNotMatch`, `Generation`, `ReadFromOffset`, `ReadRange`,
1284 * and `UserProject`.
1285 *
1286 * @par Idempotency
1287 * This is a read-only operation and is always idempotent.
1288 *
1289 * @par Example
1290 * @snippet storage_object_file_transfer_samples.cc download file
1291 */
1292 template <typename... Options>
1293 Status DownloadToFile(std::string const& bucket_name,
1294 std::string const& object_name,
1295 std::string const& file_name, Options&&... options) {
1296 google::cloud::internal::OptionsSpan const span(
1297 SpanOptions(std::forward<Options>(options)...));
1298 internal::ReadObjectRangeRequest request(bucket_name, object_name);
1299 request.set_multiple_options(std::forward<Options>(options)...);
1300 return DownloadFileImpl(request, file_name);
1301 }
1302
1303 /**
1304 * Deletes an object.
1305 *
1306 * @param bucket_name the name of the bucket that contains the object.
1307 * @param object_name the name of the object to be deleted.
1308 * @param options a list of optional query parameters and/or request headers.
1309 * Valid types for this operation include `Generation`,
1310 * `IfGenerationMatch`, `IfGenerationNotMatch`, `IfMetagenerationMatch`,
1311 * `IfMetagenerationNotMatch`, and `UserProject`.
1312 *
1313 * @par Idempotency
1314 * This operation is only idempotent if:
1315 * - restricted by pre-conditions, in this case, `IfGenerationMatch`
1316 * - or, if it applies to only one object version via `Generation`.
1317 *
1318 * @par Example
1319 * @snippet storage_object_samples.cc delete object
1320 */
1321 template <typename... Options>
1322 Status DeleteObject(std::string const& bucket_name,
1323 std::string const& object_name, Options&&... options) {
1324 google::cloud::internal::OptionsSpan const span(
1325 SpanOptions(std::forward<Options>(options)...));
1326 internal::DeleteObjectRequest request(bucket_name, object_name);
1327 request.set_multiple_options(std::forward<Options>(options)...);
1328 return raw_client_->DeleteObject(request).status();
1329 }
1330
1331 /**
1332 * Updates the metadata in a Google Cloud Storage Object.
1333 *
1334 * @param bucket_name the name of the bucket that contains the object.
1335 * @param object_name the name of the object.
1336 * @param metadata the new metadata for the Object. Only the writeable fields
1337 * accepted by the `Objects: update` API are used, all other fields are
1338 * ignored. In particular, note that `bucket` and `name` are ignored in
1339 * favor of @p bucket_name and @p object_name.
1340 * @param options a list of optional query parameters and/or request headers.
1341 * Valid types for this operation include `Generation`, `EncryptionKey`,
1342 * `IfGenerationMatch`, `IfGenerationNotMatch`, `IfMetagenerationMatch`,
1343 * `IfMetagenerationNotMatch`, `PredefinedAcl`, `Projection`, and
1344 * `UserProject`.
1345 *
1346 * @par Idempotency
1347 * This operation is only idempotent if restricted by pre-conditions, in this
1348 * case, `IfMetagenerationMatch`.
1349 *
1350 * @par Example
1351 * @snippet storage_object_samples.cc update object metadata
1352 */
1353 template <typename... Options>
1354 StatusOr<ObjectMetadata> UpdateObject(std::string bucket_name,
1355 std::string object_name,
1356 ObjectMetadata metadata,
1357 Options&&... options) {
1358 google::cloud::internal::OptionsSpan const span(
1359 SpanOptions(std::forward<Options>(options)...));
1360 internal::UpdateObjectRequest request(
1361 std::move(bucket_name), std::move(object_name), std::move(metadata));
1362 request.set_multiple_options(std::forward<Options>(options)...);
1363 return raw_client_->UpdateObject(request);
1364 }
1365
1366 /**
1367 * Patches the metadata in a Google Cloud Storage Object.
1368 *
1369 * This function creates a patch request to change the writeable attributes in
1370 * @p original to the values in @p updated. Non-writeable attributes are
1371 * ignored, and attributes not present in @p updated are removed. Typically
1372 * this function is used after the application obtained a value with
1373 * `GetObjectMetadata` and has modified these parameters.
1374 *
1375 * @param bucket_name the bucket that contains the object to be updated.
1376 * @param object_name the object to be updated.
1377 * @param original the initial value of the object metadata.
1378 * @param updated the updated value for the object metadata.
1379 * @param options a list of optional query parameters and/or request headers.
1380 * Valid types for this operation include `Generation`, `EncryptionKey`,
1381 * `IfGenerationMatch`, `IfGenerationNotMatch`, `IfMetagenerationMatch`,
1382 * `IfMetagenerationNotMatch`, `PredefinedAcl`,
1383 * `Projection`, and `UserProject`.
1384 *
1385 * @par Idempotency
1386 * This operation is only idempotent if restricted by pre-conditions, in this
1387 * case, `IfMetagenerationMatch`.
1388 *
1389 * @par Example
1390 * @snippet storage_object_samples.cc patch object delete metadata
1391 */
1392 template <typename... Options>
1393 StatusOr<ObjectMetadata> PatchObject(std::string bucket_name,
1394 std::string object_name,
1395 ObjectMetadata const& original,
1396 ObjectMetadata const& updated,
1397 Options&&... options) {
1398 google::cloud::internal::OptionsSpan const span(
1399 SpanOptions(std::forward<Options>(options)...));
1400 internal::PatchObjectRequest request(
1401 std::move(bucket_name), std::move(object_name), original, updated);
1402 request.set_multiple_options(std::forward<Options>(options)...);
1403 return raw_client_->PatchObject(request);
1404 }
1405
1406 /**
1407 * Patches the metadata in a Google Cloud Storage Object.
1408 *
1409 * This function creates a patch request based on the given @p builder.
1410 * Typically this function is used when the application needs to set an
1411 * object's metadata fields regardless of their previous value (i.e. when
1412 * calling GetObjectMetadata first is not necessary).
1413 *
1414 * @param bucket_name the bucket that contains the object to be updated.
1415 * @param object_name the object to be updated.
1416 * @param builder the set of updates to perform in the Object metadata.
1417 * @param options a list of optional query parameters and/or request headers.
1418 * Valid types for this operation include `Generation`, `EncryptionKey`,
1419 * `IfGenerationMatch`, `IfGenerationNotMatch`, `IfMetagenerationMatch`,
1420 * `IfMetagenerationNotMatch`, `PredefinedAcl`, `EncryptionKey`,
1421 * `Projection`, and `UserProject`.
1422 *
1423 * @par Idempotency
1424 * This operation is only idempotent if restricted by pre-conditions, in this
1425 * case, `IfMetagenerationMatch`.
1426 *
1427 * @par Example
1428 * @snippet storage_object_samples.cc patch object content type
1429 */
1430 template <typename... Options>
1431 StatusOr<ObjectMetadata> PatchObject(
1432 std::string bucket_name, std::string object_name,
1433 ObjectMetadataPatchBuilder const& builder, Options&&... options) {
1434 google::cloud::internal::OptionsSpan const span(
1435 SpanOptions(std::forward<Options>(options)...));
1436 internal::PatchObjectRequest request(std::move(bucket_name),
1437 std::move(object_name), builder);
1438 request.set_multiple_options(std::forward<Options>(options)...);
1439 return raw_client_->PatchObject(request);
1440 }
1441
1442 /**
1443 * Composes existing objects into a new object in the same bucket.
1444 *
1445 * @param bucket_name the name of the bucket used for source object and
1446 * destination object.
1447 * @param source_objects objects used to compose `destination_object_name`.
1448 * @param destination_object_name the composed object name.
1449 * @param options a list of optional query parameters and/or request headers.
1450 * Valid types for this operation include
1451 * `DestinationPredefinedAcl`, `EncryptionKey`, `IfGenerationMatch`,
1452 * `IfMetagenerationMatch`, `KmsKeyName`, `UserProject`, and
1453 * `WithObjectMetadata`.
1454 *
1455 * @par Idempotency
1456 * This operation is only idempotent if restricted by pre-conditions, in this
1457 * case, `IfGenerationMatch`.
1458 *
1459 * @par Example
1460 * @snippet storage_object_samples.cc compose object
1461 *
1462 * @par Example: using encrypted objects with CSEK
1463 * @snippet storage_object_csek_samples.cc compose object csek
1464 */
1465 template <typename... Options>
1466 StatusOr<ObjectMetadata> ComposeObject(
1467 std::string bucket_name, std::vector<ComposeSourceObject> source_objects,
1468 std::string destination_object_name, Options&&... options) {
1469 google::cloud::internal::OptionsSpan const span(
1470 SpanOptions(std::forward<Options>(options)...));
1471 internal::ComposeObjectRequest request(std::move(bucket_name),
1472 std::move(source_objects),
1473 std::move(destination_object_name));
1474 request.set_multiple_options(std::forward<Options>(options)...);
1475 return raw_client_->ComposeObject(request);
1476 }
1477
1478 /**
1479 * Creates an `ObjectRewriter` to copy the source object.
1480 *
1481 * Applications use this function to reliably copy objects across [location
1482 * boundaries](https://cloud.google.com/storage/docs/locations), and to
1483 * rewrite objects with different encryption keys. The operation returns a
1484 * `ObjectRewriter`, which the application can use to initiate the copy and to
1485 * iterate if the copy requires more than one call to complete.
1486 *
1487 * @note Application developers should be aware that rewriting large objects
1488 * may take multiple hours. Multiple calls to `ObjectRewriter::Iterate()`
1489 * may be required to completely rewrite an object.
1490 *
1491 * @param source_bucket_name the name of the bucket containing the source
1492 * object.
1493 * @param source_object_name the name of the source object.
1494 * @param destination_bucket_name where the destination object will be
1495 * located.
1496 * @param destination_object_name what to name the destination object.
1497 * @param options a list of optional query parameters and/or request headers.
1498 * Valid types for this operation include `DestinationKmsKeyName`,
1499 * `DestinationPredefinedAcl`, `EncryptionKey`, `IfGenerationMatch`,
1500 * `IfGenerationNotMatch`, `IfMetagenerationMatch`,
1501 * `IfSourceGenerationMatch`, `IfSourceGenerationNotMatch`,
1502 * `IfSourceMetagenerationMatch`, `IfSourceMetagenerationNotMatch`,
1503 * `MaxBytesRewrittenPerCall`, `Projection`, `SourceEncryptionKey`,
1504 * `SourceGeneration`, `UserProject`, and `WithObjectMetadata`.
1505 *
1506 * @par Idempotency
1507 * This operation is only idempotent if restricted by pre-conditions, in this
1508 * case, `IfGenerationMatch`.
1509 *
1510 * @par Example
1511 * @snippet storage_object_rewrite_samples.cc rewrite object non blocking
1512 *
1513 * @par Example
1514 * @snippet storage_object_rewrite_samples.cc rewrite object resume
1515 */
1516 template <typename... Options>
1517 ObjectRewriter RewriteObject(std::string source_bucket_name,
1518 std::string source_object_name,
1519 std::string destination_bucket_name,
1520 std::string destination_object_name,
1521 Options&&... options) {
1522 return ResumeRewriteObject(
1523 std::move(source_bucket_name), std::move(source_object_name),
1524 std::move(destination_bucket_name), std::move(destination_object_name),
1525 std::string{}, std::forward<Options>(options)...);
1526 }
1527
1528 /**
1529 * Creates an `ObjectRewriter` to resume a previously created rewrite.
1530 *
1531 * Applications use this function to resume a rewrite operation, possibly
1532 * created with `RewriteObject()`. Rewrite can reliably copy objects across
1533 * location boundaries, and can rewrite objects with different encryption
1534 * keys. For large objects this operation can take a long time, thus
1535 * applications should consider checkpointing the rewrite token (accessible in
1536 * the `ObjectRewriter`) and restarting the operation in the event the program
1537 * is terminated.
1538 *
1539 * @note Application developers should be aware that rewriting large objects
1540 * may take multiple hours. Multiple calls to `ObjectRewriter::Iterate()`
1541 * may be required to completely rewrite an object.
1542 *
1543 * @param source_bucket_name the name of the bucket containing the source
1544 * object.
1545 * @param source_object_name the name of the source object.
1546 * @param destination_bucket_name where the destination object will be
1547 * located.
1548 * @param destination_object_name what to name the destination object.
1549 * @param rewrite_token the token from a previous successful rewrite
1550 * iteration. Can be the empty string, in which case this starts a new
1551 * rewrite operation.
1552 * @param options a list of optional query parameters and/or request headers.
1553 * Valid types for this operation include `DestinationKmsKeyName`,
1554 * `DestinationPredefinedAcl`, `EncryptionKey`, `IfGenerationMatch`,
1555 * `IfGenerationNotMatch`, `IfMetagenerationMatch`,
1556 * `IfSourceGenerationMatch`, `IfSourceGenerationNotMatch`,
1557 * `IfSourceMetagenerationMatch`, `IfSourceMetagenerationNotMatch`,
1558 * `MaxBytesRewrittenPerCall`, `Projection`, `SourceEncryptionKey`,
1559 * `SourceGeneration`, `UserProject`, and `WithObjectMetadata`.
1560 *
1561 * @par Idempotency
1562 * This operation is only idempotent if restricted by pre-conditions, in this
1563 * case, `IfGenerationMatch`.
1564 *
1565 * @par Example
1566 * @snippet storage_object_rewrite_samples.cc rewrite object resume
1567 */
1568 template <typename... Options>
1569 ObjectRewriter ResumeRewriteObject(std::string source_bucket_name,
1570 std::string source_object_name,
1571 std::string destination_bucket_name,
1572 std::string destination_object_name,
1573 std::string rewrite_token,
1574 Options&&... options) {
1575 google::cloud::internal::OptionsSpan const span(
1576 SpanOptions(std::forward<Options>(options)...));
1577 internal::RewriteObjectRequest request(
1578 std::move(source_bucket_name), std::move(source_object_name),
1579 std::move(destination_bucket_name), std::move(destination_object_name),
1580 std::move(rewrite_token));
1581 request.set_multiple_options(std::forward<Options>(options)...);
1582 return ObjectRewriter(raw_client_, std::move(request));
1583 }
1584
1585 /**
1586 * Rewrites the object, blocking until the rewrite completes, and returns the
1587 * resulting `ObjectMetadata`.
1588 *
1589 * Applications use this function to reliably copy objects across [location
1590 * boundaries](https://cloud.google.com/storage/docs/locations), and to
1591 * rewrite objects with different encryption keys. The operation blocks until
1592 * the rewrite completes, and returns the resulting `ObjectMetadata`.
1593 *
1594 * @note Application developers should be aware that rewriting large objects
1595 * may take multiple hours. In such cases the application should consider
1596 * using `RewriteObject()` or `ResumeRewriteObject()`.
1597 *
1598 * @param source_bucket_name the name of the bucket containing the source
1599 * object.
1600 * @param source_object_name the name of the source object.
1601 * @param destination_bucket_name where the destination object will be
1602 * located.
1603 * @param destination_object_name what to name the destination object.
1604 * @param options a list of optional query parameters and/or request headers.
1605 * Valid types for this operation include `DestinationKmsKeyName`,
1606 * `DestinationPredefinedAcl`, `EncryptionKey`, `IfGenerationMatch`,
1607 * `IfGenerationNotMatch`, `IfMetagenerationMatch`,
1608 * `IfSourceGenerationMatch`, `IfSourceGenerationNotMatch`,
1609 * `IfSourceMetagenerationMatch`, `IfSourceMetagenerationNotMatch`,
1610 * `MaxBytesRewrittenPerCall`, `Projection`, `SourceEncryptionKey`,
1611 * `SourceGeneration`, `UserProject`, and `WithObjectMetadata`.
1612 *
1613 * @return The metadata of the newly created object.
1614 *
1615 * @par Idempotency
1616 * This operation is only idempotent if restricted by pre-conditions, in this
1617 * case, `IfGenerationMatch`.
1618 *
1619 * @par Example
1620 * @snippet storage_object_rewrite_samples.cc rewrite object
1621 *
1622 * @par Example: using rewrite object to rotate the encryption key
1623 * @snippet storage_object_csek_samples.cc rotate encryption key
1624 *
1625 * @par Example: using rewrite object to rename an object
1626 * @snippet storage_object_rewrite_samples.cc rename object
1627 */
1628 template <typename... Options>
1630 std::string source_bucket_name, std::string source_object_name,
1631 std::string destination_bucket_name, std::string destination_object_name,
1632 Options&&... options) {
1633 return ResumeRewriteObject(std::move(source_bucket_name),
1634 std::move(source_object_name),
1635 std::move(destination_bucket_name),
1636 std::move(destination_object_name),
1637 std::string{}, std::forward<Options>(options)...)
1638 .Result();
1639 }
1640 ///@}
1641
1642 /**
1643 * @name Bucket Access Control List operations.
1644 *
1645 * You can control who has access to your GCS buckets and objects as
1646 * well as what level of access they have.
1647 *
1648 * @note In most cases, you should use IAM permissions instead of ACLs to
1649 * control access to buckets.
1650 *
1651 * @note All buckets are owned by the project owners group. Project owners
1652 * are granted `OWNER` permissions to all buckets inside their project.
1653 * Bucket and object ownership cannot be changed by modifying ACLs.
1654 *
1655 * @note When you apply a new ACL to a bucket, GCS adds `OWNER` permission to
1656 * the bucket if you omit the grants. You cannot remove the project owners
1657 * group from a bucket ACL. Attempts to do so will appear to succeed, but
1658 * the service will add the project owners group into the new ACL before
1659 * applying it.
1660 *
1661 * @see https://cloud.google.com/storage/docs/access-control/ for more
1662 * information about access control in GCS.
1663 *
1664 * @see
1665 * https://cloud.google.com/storage/docs/access-control/lists#defaultbuckets
1666 * for more details about the default owners for a bucket.
1667 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
1668 * the format of the @p entity parameters.
1669 * @see https://cloud.google.com/storage/docs/access-control/lists#permissions
1670 * for the format of the @p role parameters.
1671 */
1672 ///@{
1673 /**
1674 * Retrieves the list of `BucketAccessControl` items for a bucket.
1675 *
1676 * @param bucket_name the name of the bucket.
1677 * @param options a list of optional query parameters and/or request headers.
1678 * Valid types for this operation include `UserProject`.
1679 *
1680 * @par Idempotency
1681 * This is a read-only operation and is always idempotent.
1682 *
1683 * @par Example
1684 * @snippet storage_bucket_acl_samples.cc list bucket acl
1685 */
1686 template <typename... Options>
1687 StatusOr<std::vector<BucketAccessControl>> ListBucketAcl(
1688 std::string const& bucket_name, Options&&... options) {
1689 google::cloud::internal::OptionsSpan const span(
1690 SpanOptions(std::forward<Options>(options)...));
1691 internal::ListBucketAclRequest request(bucket_name);
1692 request.set_multiple_options(std::forward<Options>(options)...);
1693 auto items = raw_client_->ListBucketAcl(request);
1694 if (!items) {
1695 return std::move(items).status();
1696 }
1697 return std::move(items.value().items);
1698 }
1699
1700 /**
1701 * Creates a new entry in a bucket ACL.
1702 *
1703 * @param bucket_name the name of the bucket.
1704 * @param entity the name of the entity added to the ACL.
1705 * @param role the role of the entity.
1706 * @param options a list of optional query parameters and/or request headers.
1707 * Valid types for this operation include `UserProject`.
1708 *
1709 * @par Idempotency
1710 * This operation is only idempotent if restricted by pre-conditions. There
1711 * are no pre-conditions for this operation that can make it idempotent.
1712 *
1713 * @par Example
1714 * @snippet storage_bucket_acl_samples.cc create bucket acl
1715 *
1716 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
1717 * the format of the @p entity parameter.
1718 * @see https://cloud.google.com/storage/docs/access-control/lists#permissions
1719 * for the format of the @p role parameter.
1720 */
1721 template <typename... Options>
1722 StatusOr<BucketAccessControl> CreateBucketAcl(std::string const& bucket_name,
1723 std::string const& entity,
1724 std::string const& role,
1725 Options&&... options) {
1726 google::cloud::internal::OptionsSpan const span(
1727 SpanOptions(std::forward<Options>(options)...));
1728 internal::CreateBucketAclRequest request(bucket_name, entity, role);
1729 request.set_multiple_options(std::forward<Options>(options)...);
1730 return raw_client_->CreateBucketAcl(request);
1731 }
1732
1733 /**
1734 * Deletes an entry from a bucket ACL.
1735 *
1736 * @param bucket_name the name of the bucket.
1737 * @param entity the name of the entity added to the ACL.
1738 * @param options a list of optional query parameters and/or request headers.
1739 * Valid types for this operation include `UserProject`.
1740 *
1741 * @par Idempotency
1742 * This operation is only idempotent if restricted by pre-conditions. There
1743 * are no pre-conditions for this operation that can make it idempotent.
1744 *
1745 * @par Example
1746 * @snippet storage_bucket_acl_samples.cc delete bucket acl
1747 *
1748 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
1749 * the format of the @p entity parameter.
1750 */
1751 template <typename... Options>
1752 Status DeleteBucketAcl(std::string const& bucket_name,
1753 std::string const& entity, Options&&... options) {
1754 google::cloud::internal::OptionsSpan const span(
1755 SpanOptions(std::forward<Options>(options)...));
1756 internal::DeleteBucketAclRequest request(bucket_name, entity);
1757 request.set_multiple_options(std::forward<Options>(options)...);
1758 return raw_client_->DeleteBucketAcl(request).status();
1759 }
1760
1761 /**
1762 * Gets the value of an existing bucket ACL.
1763 *
1764 * @param bucket_name the name of the bucket to query.
1765 * @param entity the name of the entity to query.
1766 * @param options a list of optional query parameters and/or request headers.
1767 * Valid types for this operation include `UserProject`.
1768 *
1769 * @par Idempotency
1770 * This is a read-only operation and is always idempotent.
1771 *
1772 * @par Example
1773 * @snippet storage_bucket_acl_samples.cc get bucket acl
1774 *
1775 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
1776 * the format of the @p entity parameter.
1777 */
1778 template <typename... Options>
1779 StatusOr<BucketAccessControl> GetBucketAcl(std::string const& bucket_name,
1780 std::string const& entity,
1781 Options&&... options) {
1782 google::cloud::internal::OptionsSpan const span(
1783 SpanOptions(std::forward<Options>(options)...));
1784 internal::GetBucketAclRequest request(bucket_name, entity);
1785 request.set_multiple_options(std::forward<Options>(options)...);
1786 return raw_client_->GetBucketAcl(request);
1787 }
1788
1789 /**
1790 * Updates the value of an existing bucket ACL.
1791 *
1792 * @note
1793 * For changing BucketAccessControl, the Patch and Update APIs basically offer
1794 * the same functionality. The only field that can be modified by either API
1795 * is `role`, and it may only be set to a new value (it cannot be removed).
1796 * The API is offered for consistency with the other resource types where
1797 * Patch and Update APIs have different semantics.
1798 *
1799 * @param bucket_name the name of the bucket.
1800 * @param acl the new ACL value. Note that only the writable values of the ACL
1801 * will be modified by the server.
1802 * @param options a list of optional query parameters and/or request headers.
1803 * Valid types for this operation include `UserProject`.
1804 *
1805 * @par Idempotency
1806 * This operation is only idempotent if restricted by pre-conditions. There
1807 * are no pre-conditions for this operation that can make it idempotent.
1808 *
1809 * @par Example
1810 * @snippet storage_bucket_acl_samples.cc update bucket acl
1811 *
1812 * @see https://cloud.google.com/storage/docs/json_api/v1/bucketAccessControls
1813 * for additional details on what fields are writeable.
1814 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
1815 * the format of the @p entity parameter.
1816 * @see https://cloud.google.com/storage/docs/access-control/lists#permissions
1817 * for the format of the @p role parameter.
1818 */
1819 template <typename... Options>
1820 StatusOr<BucketAccessControl> UpdateBucketAcl(std::string const& bucket_name,
1821 BucketAccessControl const& acl,
1822 Options&&... options) {
1823 google::cloud::internal::OptionsSpan const span(
1824 SpanOptions(std::forward<Options>(options)...));
1825 internal::UpdateBucketAclRequest request(bucket_name, acl.entity(),
1826 acl.role());
1827 request.set_multiple_options(std::forward<Options>(options)...);
1828 return raw_client_->UpdateBucketAcl(request);
1829 }
1830
1831 /**
1832 * Patches the value of an existing bucket ACL.
1833 *
1834 * Computes the delta between a previous value for an BucketAccessControl and
1835 * the new value for an BucketAccessControl and apply that delta.
1836 *
1837 * @note
1838 * For changing BucketAccessControl, the Patch and Update APIs basically offer
1839 * the same functionality. The only field that can be modified by either API
1840 * is `role`, and it may only be set to a new value (it cannot be removed).
1841 * The API is offered for consistency with the other resource types where
1842 * Patch and Update APIs have different semantics.
1843 *
1844 * @param bucket_name the name of the bucket.
1845 * @param entity the identifier for the user, group, service account, or
1846 * predefined set of actors holding the permission.
1847 * @param original_acl the original ACL value.
1848 * @param new_acl the new ACL value. Note that only changes on writeable
1849 * fields will be accepted by the server.
1850 * @param options a list of optional query parameters and/or request headers.
1851 * Valid types for this operation include `UserProject`, and the standard
1852 * options available to all operations.
1853 *
1854 * @par Idempotency
1855 * This operation is only idempotent if restricted by pre-conditions. There
1856 * are no pre-conditions for this operation that can make it idempotent.
1857 *
1858 * @par Example
1859 * @snippet storage_bucket_acl_samples.cc patch bucket acl
1860 *
1861 * @see https://cloud.google.com/storage/docs/json_api/v1/bucketAccessControls
1862 * for additional details on what fields are writeable.
1863 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
1864 * the format of the @p entity parameters.
1865 */
1866 template <typename... Options>
1868 std::string const& bucket_name, std::string const& entity,
1869 BucketAccessControl const& original_acl,
1870 BucketAccessControl const& new_acl, Options&&... options) {
1871 google::cloud::internal::OptionsSpan const span(
1872 SpanOptions(std::forward<Options>(options)...));
1873 internal::PatchBucketAclRequest request(bucket_name, entity, original_acl,
1874 new_acl);
1875 request.set_multiple_options(std::forward<Options>(options)...);
1876 return raw_client_->PatchBucketAcl(request);
1877 }
1878
1879 /**
1880 * Patches the value of an existing bucket ACL.
1881 *
1882 * This API allows the application to patch an BucketAccessControl without
1883 * having to read the current value.
1884 *
1885 * @note
1886 * For changing BucketAccessControl, the Patch and Update APIs basically offer
1887 * the same functionality. The only field that can be modified by either API
1888 * is `role`, and it may only be set to a new value (it cannot be removed).
1889 * The API is offered for consistency with the other resource types where
1890 * Patch and Update APIs have different semantics.
1891 *
1892 * @param bucket_name the name of the bucket.
1893 * @param entity the identifier for the user, group, service account, or
1894 * predefined set of actors holding the permission.
1895 * @param builder a builder ready to create the patch.
1896 * @param options a list of optional query parameters and/or request
1897 * headers. Valid types for this operation include `Generation`,
1898 * `UserProject`, `IfMatchEtag`, and `IfNoneMatchEtag`.
1899 *
1900 * @par Idempotency
1901 * This operation is only idempotent if restricted by pre-conditions. There
1902 * are no pre-conditions for this operation that can make it idempotent.
1903 *
1904 * @par Example
1905 * @snippet storage_bucket_acl_samples.cc patch bucket acl no-read
1906 *
1907 * @see https://cloud.google.com/storage/docs/json_api/v1/bucketAccessControls
1908 * for additional details on what fields are writeable.
1909 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
1910 * the format of the @p entity parameters.
1911 */
1912 template <typename... Options>
1914 std::string const& bucket_name, std::string const& entity,
1915 BucketAccessControlPatchBuilder const& builder, Options&&... options) {
1916 google::cloud::internal::OptionsSpan const span(
1917 SpanOptions(std::forward<Options>(options)...));
1918 internal::PatchBucketAclRequest request(bucket_name, entity, builder);
1919 request.set_multiple_options(std::forward<Options>(options)...);
1920 return raw_client_->PatchBucketAcl(request);
1921 }
1922 ///@}
1923
1924 /**
1925 * @name Object Access Control List operations.
1926 *
1927 * You can control who has access to your GCS buckets and objects as
1928 * well as what level of access they have.
1929 *
1930 * @note When you upload (create) an object the entity that uploads the object
1931 * is listed as the object owner. This can be a user or a service account,
1932 * depending on what credentials are used to authenticate with GCS.
1933 * Object ownership cannot be changed by modifying ACLs. You can change
1934 * object ownership only by overwriting the object while authenticated as
1935 * the desired owner.
1936 *
1937 * @note When you apply a new ACL to an object, GCS adds `OWNER` permission to
1938 * the object if you omit the grants.
1939 *
1940 * @see https://cloud.google.com/storage/docs/access-control/ for more
1941 * information about access control in GCS.
1942 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
1943 * the format of the @p entity parameters.
1944 * @see https://cloud.google.com/storage/docs/access-control/lists#permissions
1945 * for the format of the @p role parameters.
1946 */
1947 ///@{
1948 /**
1949 * Retrieves the list of ObjectAccessControl items for an object.
1950 *
1951 * @param bucket_name the name of the bucket that contains the object.
1952 * @param object_name the name of the object to be deleted.
1953 * @param options a list of optional query parameters and/or request headers.
1954 * Valid types for this operation include `Generation`, and `UserProject`.
1955 *
1956 * @par Idempotency
1957 * This is a read-only operation and is always idempotent.
1958 *
1959 * @par Example
1960 * @snippet storage_object_acl_samples.cc list object acl
1961 */
1962 template <typename... Options>
1963 StatusOr<std::vector<ObjectAccessControl>> ListObjectAcl(
1964 std::string const& bucket_name, std::string const& object_name,
1965 Options&&... options) {
1966 google::cloud::internal::OptionsSpan const span(
1967 SpanOptions(std::forward<Options>(options)...));
1968 internal::ListObjectAclRequest request(bucket_name, object_name);
1969 request.set_multiple_options(std::forward<Options>(options)...);
1970 auto result = raw_client_->ListObjectAcl(request);
1971 if (!result) {
1972 return std::move(result).status();
1973 }
1974 return std::move(result.value().items);
1975 }
1976
1977 /**
1978 * Creates a new entry in the object ACL.
1979 *
1980 * @param bucket_name the name of the bucket that contains the object.
1981 * @param object_name the name of the object.
1982 * @param entity the name of the entity added to the ACL.
1983 * @param role the role of the entity.
1984 * @param options a list of optional query parameters and/or request headers.
1985 * Valid types for this operation include `Generation`, and `UserProject`.
1986 *
1987 * @par Idempotency
1988 * This operation is only idempotent if restricted by pre-conditions. There
1989 * are no pre-conditions for this operation that can make it idempotent.
1990 *
1991 * @par Example
1992 * @snippet storage_object_acl_samples.cc create object acl
1993 *
1994 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
1995 * the format of the @p entity parameter.
1996 * @see https://cloud.google.com/storage/docs/access-control/lists#permissions
1997 * for the format of the @p role parameter.
1998 */
1999 template <typename... Options>
2000 StatusOr<ObjectAccessControl> CreateObjectAcl(std::string const& bucket_name,
2001 std::string const& object_name,
2002 std::string const& entity,
2003 std::string const& role,
2004 Options&&... options) {
2005 google::cloud::internal::OptionsSpan const span(
2006 SpanOptions(std::forward<Options>(options)...));
2007 internal::CreateObjectAclRequest request(bucket_name, object_name, entity,
2008 role);
2009 request.set_multiple_options(std::forward<Options>(options)...);
2010 return raw_client_->CreateObjectAcl(request);
2011 }
2012
2013 /**
2014 * Deletes one access control entry in one object.
2015 *
2016 * @param bucket_name the name of the bucket that contains the object.
2017 * @param object_name the name of the object to be deleted.
2018 * @param entity the name of the entity (user, team, group) to be removed from
2019 * the Object's ACL.
2020 * @param options a list of optional query parameters and/or request headers.
2021 * Valid types for this operation include `Generation`, and `UserProject`.
2022 *
2023 * @par Idempotency
2024 * This operation is only idempotent if restricted by pre-conditions. There
2025 * are no pre-conditions for this operation that can make it idempotent.
2026 *
2027 * @par Example
2028 * @snippet storage_object_acl_samples.cc delete object acl
2029 *
2030 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2031 * the format of the @p entity parameters.
2032 */
2033 template <typename... Options>
2034 Status DeleteObjectAcl(std::string const& bucket_name,
2035 std::string const& object_name,
2036 std::string const& entity, Options&&... options) {
2037 google::cloud::internal::OptionsSpan const span(
2038 SpanOptions(std::forward<Options>(options)...));
2039 internal::DeleteObjectAclRequest request(bucket_name, object_name, entity);
2040 request.set_multiple_options(std::forward<Options>(options)...);
2041 return raw_client_->DeleteObjectAcl(request).status();
2042 }
2043
2044 /**
2045 * Gets the value of an existing object ACL.
2046 *
2047 * @param bucket_name the name of the bucket that contains the object.
2048 * @param object_name the name of the object.
2049 * @param entity the name of the entity added to the ACL.
2050 * @param options a list of optional query parameters and/or request headers.
2051 * Valid types for this operation include `Generation`, and `UserProject`.
2052 *
2053 * @par Idempotency
2054 * This is a read-only operation and is always idempotent.
2055 *
2056 * @par Example
2057 * @snippet storage_object_acl_samples.cc print file acl for user
2058 *
2059 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2060 * the format of the @p entity parameters.
2061 */
2062 template <typename... Options>
2063 StatusOr<ObjectAccessControl> GetObjectAcl(std::string const& bucket_name,
2064 std::string const& object_name,
2065 std::string const& entity,
2066 Options&&... options) {
2067 google::cloud::internal::OptionsSpan const span(
2068 SpanOptions(std::forward<Options>(options)...));
2069 internal::GetObjectAclRequest request(bucket_name, object_name, entity);
2070 request.set_multiple_options(std::forward<Options>(options)...);
2071 return raw_client_->GetObjectAcl(request);
2072 }
2073
2074 /**
2075 * Updates the value of an existing object ACL.
2076 *
2077 * @note
2078 * For changing ObjectAccessControl, the Patch and Update APIs basically offer
2079 * the same functionality. The only field that can be modified by either API
2080 * is `role`, and it may only be set to a new value (it cannot be removed).
2081 * The API is offered for consistency with the other resource types where
2082 * Patch and Update APIs have different semantics.
2083 *
2084 * @param bucket_name the name of the bucket that contains the object.
2085 * @param object_name the name of the object.
2086 * @param acl the new ACL value. Note that only the writable values of the ACL
2087 * will be modified by the server.
2088 * @param options a list of optional query parameters and/or request
2089 * Valid types for this operation include `Generation`, and `UserProject`.
2090 *
2091 * @par Idempotency
2092 * This operation is only idempotent if restricted by pre-conditions. There
2093 * are no pre-conditions for this operation that can make it idempotent.
2094 *
2095 * @par Example
2096 * @snippet storage_object_acl_samples.cc update object acl
2097 *
2098 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2099 * the format of the @p entity parameters.
2100 * @see https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls
2101 * for additional details on what fields are writeable.
2102 */
2103 template <typename... Options>
2104 StatusOr<ObjectAccessControl> UpdateObjectAcl(std::string const& bucket_name,
2105 std::string const& object_name,
2106 ObjectAccessControl const& acl,
2107 Options&&... options) {
2108 google::cloud::internal::OptionsSpan const span(
2109 SpanOptions(std::forward<Options>(options)...));
2110 internal::UpdateObjectAclRequest request(bucket_name, object_name,
2111 acl.entity(), acl.role());
2112 request.set_multiple_options(std::forward<Options>(options)...);
2113 return raw_client_->UpdateObjectAcl(request);
2114 }
2115
2116 /**
2117 * Patches the value of an existing object ACL.
2118 *
2119 * Compute the delta between a previous value for an ObjectAccessControl and
2120 * the new value for an ObjectAccessControl and apply that delta.
2121 *
2122 * @note
2123 * For changing ObjectAccessControl, the Patch and Update APIs basically offer
2124 * the same functionality. The only field that can be modified by either API
2125 * is `role`, and it may only be set to a new value (it cannot be removed).
2126 * The API is offered for consistency with the other resource types where
2127 * Patch and Update APIs have different semantics.
2128 *
2129 * @param bucket_name the name of the bucket that contains the object.
2130 * @param object_name the name of the object.
2131 * @param entity the identifier for the user, group, service account, or
2132 * predefined set of actors holding the permission.
2133 * @param original_acl the original ACL value.
2134 * @param new_acl the new ACL value. Note that only changes on writeable
2135 * fields will be accepted by the server.
2136 * @param options a list of optional query parameters and/or request
2137 * headers. Valid types for this operation include `Generation`,
2138 * `UserProject`, `IfMatchEtag`, and `IfNoneMatchEtag`.
2139 *
2140 * @par Idempotency
2141 * This operation is only idempotent if restricted by pre-conditions. There
2142 * are no pre-conditions for this operation that can make it idempotent.
2143 *
2144 * @par Example
2145 * @snippet storage_object_acl_samples.cc patch object acl
2146 *
2147 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2148 * the format of the @p entity parameters.
2149 * @see https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls
2150 * for additional details on what fields are writeable.
2151 */
2152 template <typename... Options>
2154 std::string const& bucket_name, std::string const& object_name,
2155 std::string const& entity, ObjectAccessControl const& original_acl,
2156 ObjectAccessControl const& new_acl, Options&&... options) {
2157 google::cloud::internal::OptionsSpan const span(
2158 SpanOptions(std::forward<Options>(options)...));
2159 internal::PatchObjectAclRequest request(bucket_name, object_name, entity,
2160 original_acl, new_acl);
2161 request.set_multiple_options(std::forward<Options>(options)...);
2162 return raw_client_->PatchObjectAcl(request);
2163 }
2164
2165 /**
2166 * Patches the value of an existing object ACL.
2167 *
2168 * This API allows the application to patch an ObjectAccessControl without
2169 * having to read the current value.
2170 *
2171 * @note
2172 * For changing ObjectAccessControl, the Patch and Update APIs basically offer
2173 * the same functionality. The only field that can be modified by either API
2174 * is `role`, and it may only be set to a new value (it cannot be removed).
2175 * The API is offered for consistency with the other resource types where
2176 * Patch and Update APIs have different semantics.
2177 *
2178 * @param bucket_name the name of the bucket that contains the object.
2179 * @param object_name the name of the object.
2180 * @param entity the identifier for the user, group, service account, or
2181 * predefined set of actors holding the permission.
2182 * @param builder a builder ready to create the patch.
2183 * @param options a list of optional query parameters and/or request
2184 * headers. Valid types for this operation include `Generation`,
2185 * `UserProject`, `IfMatchEtag`, and `IfNoneMatchEtag`.
2186 *
2187 * @par Idempotency
2188 * This operation is only idempotent if restricted by pre-conditions. There
2189 * are no pre-conditions for this operation that can make it idempotent.
2190 *
2191 * @par Example
2192 * @snippet storage_object_acl_samples.cc patch object acl no-read
2193 *
2194 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2195 * the format of the @p entity parameters.
2196 * @see https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls
2197 * for additional details on what fields are writeable.
2198 */
2199 template <typename... Options>
2201 std::string const& bucket_name, std::string const& object_name,
2202 std::string const& entity, ObjectAccessControlPatchBuilder const& builder,
2203 Options&&... options) {
2204 google::cloud::internal::OptionsSpan const span(
2205 SpanOptions(std::forward<Options>(options)...));
2206 internal::PatchObjectAclRequest request(bucket_name, object_name, entity,
2207 builder);
2208 request.set_multiple_options(std::forward<Options>(options)...);
2209 return raw_client_->PatchObjectAcl(request);
2210 }
2211 ///@}
2212
2213 /**
2214 * @name Bucket Default Object Access Control List operations.
2215 *
2216 * When you upload an object to GCS without specifying an ACL, the object is
2217 * created with the Default Object ACL for its bucket. These operations are
2218 * used to query and modify the Default Object ACL of a bucket.
2219 *
2220 * @see
2221 * https://cloud.google.com/storage/docs/access-control/lists#defaultobjects
2222 * for more information on default object ACLs.
2223 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2224 * the format of the @p entity parameters.
2225 * @see https://cloud.google.com/storage/docs/access-control/lists#permissions
2226 * for the format of the @p role parameters.
2227 */
2228 ///@{
2229 /**
2230 * Retrieves the default object ACL for a bucket as a vector of
2231 * `ObjectAccessControl` items.
2232 *
2233 * The default object ACL sets the ACL for any object created in the bucket,
2234 * unless a different ACL is specified when the object is created.
2235 *
2236 * @param bucket_name the name of the bucket.
2237 * @param options a list of optional query parameters and/or request headers.
2238 * Valid types for this operation include `IfMetagenerationMatch`,
2239 * `IfMetagenerationNotMatch` and `UserProject`.
2240 *
2241 * @par Idempotency
2242 * This is a read-only operation and is always idempotent.
2243 *
2244 * @par Example
2245 * @snippet storage_default_object_acl_samples.cc list default object acl
2246 *
2247 * @see
2248 * https://cloud.google.com/storage/docs/access-control/create-manage-lists#defaultobjects
2249 */
2250 template <typename... Options>
2251 StatusOr<std::vector<ObjectAccessControl>> ListDefaultObjectAcl(
2252 std::string const& bucket_name, Options&&... options) {
2253 google::cloud::internal::OptionsSpan const span(
2254 SpanOptions(std::forward<Options>(options)...));
2255 internal::ListDefaultObjectAclRequest request(bucket_name);
2256 request.set_multiple_options(std::forward<Options>(options)...);
2257 auto response = raw_client_->ListDefaultObjectAcl(request);
2258 if (!response) {
2259 return std::move(response).status();
2260 }
2261 return std::move(response.value().items);
2262 }
2263
2264 /**
2265 * Creates a new entry in the default object ACL for a bucket.
2266 *
2267 * The default object ACL sets the ACL for any object created in the bucket,
2268 * unless a different ACL is specified when the object is created.
2269 *
2270 * @param bucket_name the name of the bucket.
2271 * @param entity the name of the entity added to the ACL.
2272 * @param role the role of the entity.
2273 * @param options a list of optional query parameters and/or request headers.
2274 * Valid types for this operation include `UserProject`.
2275 *
2276 * @par Idempotency
2277 * This operation is only idempotent if restricted by pre-conditions. There
2278 * are no pre-conditions for this operation that can make it idempotent.
2279 *
2280 * @par Example
2281 * @snippet storage_default_object_acl_samples.cc create default object acl
2282 *
2283 * @see
2284 * https://cloud.google.com/storage/docs/access-control/create-manage-lists#defaultobjects
2285 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2286 * the format of the @p entity parameter.
2287 * @see https://cloud.google.com/storage/docs/access-control/lists#permissions
2288 * for the format of the @p role parameter.
2289 */
2290 template <typename... Options>
2292 std::string const& bucket_name, std::string const& entity,
2293 std::string const& role, Options&&... options) {
2294 google::cloud::internal::OptionsSpan const span(
2295 SpanOptions(std::forward<Options>(options)...));
2296 internal::CreateDefaultObjectAclRequest request(bucket_name, entity, role);
2297 request.set_multiple_options(std::forward<Options>(options)...);
2298 return raw_client_->CreateDefaultObjectAcl(request);
2299 }
2300
2301 /**
2302 * Deletes an entry from the default object ACL in a bucket.
2303 *
2304 * The default object ACL sets the ACL for any object created in the bucket,
2305 * unless a different ACL is specified when the object is created.
2306 *
2307 * @param bucket_name the name of the bucket.
2308 * @param entity the name of the entity added to the ACL.
2309 * @param options a list of optional query parameters and/or request headers.
2310 * Valid types for this operation include `UserProject`.
2311 *
2312 * @par Idempotency
2313 * This operation is only idempotent if restricted by pre-conditions. There
2314 * are no pre-conditions for this operation that can make it idempotent.
2315 *
2316 * @par Example
2317 * @snippet storage_default_object_acl_samples.cc delete default object acl
2318 *
2319 * @see
2320 * https://cloud.google.com/storage/docs/access-control/create-manage-lists#defaultobjects
2321 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2322 * the format of the @p entity parameter.
2323 */
2324 template <typename... Options>
2325 Status DeleteDefaultObjectAcl(std::string const& bucket_name,
2326 std::string const& entity,
2327 Options&&... options) {
2328 google::cloud::internal::OptionsSpan const span(
2329 SpanOptions(std::forward<Options>(options)...));
2330 internal::DeleteDefaultObjectAclRequest request(bucket_name, entity);
2331 request.set_multiple_options(std::forward<Options>(options)...);
2332 return raw_client_->DeleteDefaultObjectAcl(request).status();
2333 }
2334
2335 /**
2336 * Gets the value of a default object ACL in a bucket.
2337 *
2338 * The default object ACL sets the ACL for any object created in the bucket,
2339 * unless a different ACL is specified when the object is created.
2340 *
2341 * @param bucket_name the name of the bucket.
2342 * @param entity the name of the entity.
2343 * @param options a list of optional query parameters and/or request headers.
2344 * Valid types for this operation include `UserProject`.
2345 *
2346 * @par Idempotency
2347 * This is a read-only operation and is always idempotent.
2348 *
2349 * @par Example
2350 * @snippet storage_default_object_acl_samples.cc get default object acl
2351 *
2352 * @see
2353 * https://cloud.google.com/storage/docs/access-control/create-manage-lists#defaultobjects
2354 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2355 * the format of the @p entity parameter.
2356 */
2357 template <typename... Options>
2359 std::string const& bucket_name, std::string const& entity,
2360 Options&&... options) {
2361 google::cloud::internal::OptionsSpan const span(
2362 SpanOptions(std::forward<Options>(options)...));
2363 internal::GetDefaultObjectAclRequest request(bucket_name, entity);
2364 request.set_multiple_options(std::forward<Options>(options)...);
2365 return raw_client_->GetDefaultObjectAcl(request);
2366 }
2367
2368 /**
2369 * Updates the value of an existing default object ACL.
2370 *
2371 * The default object ACL sets the ACL for any object created in the bucket,
2372 * unless a different ACL is specified when the object is created.
2373 *
2374 * @note
2375 * For changing default object access controls, the Patch and Update APIs
2376 * basically offer the same functionality. The only field that can be modified
2377 * by either API is `role`, and it may only be set to a new value (it cannot
2378 * be removed). The API is offered for consistency with the other resource
2379 * types where Patch and Update APIs have different semantics.
2380 *
2381 * @param bucket_name the name of the bucket.
2382 * @param acl the new ACL value. Note that only the writable values of the ACL
2383 * will be modified by the server.
2384 * @param options a list of optional query parameters and/or request
2385 * Valid types for this operation include `UserProject`.
2386 *
2387 * @par Idempotency
2388 * This operation is only idempotent if restricted by pre-conditions. There
2389 * are no pre-conditions for this operation that can make it idempotent.
2390 *
2391 * @par Example
2392 * @snippet storage_default_object_acl_samples.cc update default object acl
2393 *
2394 * @see
2395 * https://cloud.google.com/storage/docs/access-control/create-manage-lists#defaultobjects
2396 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2397 * the format of the @p entity parameter.
2398 * @see https://cloud.google.com/storage/docs/access-control/lists#permissions
2399 * for the format of the @p role parameter.
2400 */
2401 template <typename... Options>
2403 std::string const& bucket_name, ObjectAccessControl const& acl,
2404 Options&&... options) {
2405 google::cloud::internal::OptionsSpan const span(
2406 SpanOptions(std::forward<Options>(options)...));
2407 internal::UpdateDefaultObjectAclRequest request(bucket_name, acl.entity(),
2408 acl.role());
2409 request.set_multiple_options(std::forward<Options>(options)...);
2410 return raw_client_->UpdateDefaultObjectAcl(request);
2411 }
2412
2413 /**
2414 * Patches the value of an existing default object ACL.
2415 *
2416 * Compute the delta between a previous and new values for a default object
2417 * access control, and apply that delta.
2418 *
2419 * @note
2420 * For changing default object access controls, the Patch and Update APIs
2421 * basically offer the same functionality. The only field that can be modified
2422 * by either API is `role`, and it may only be set to a new value (it cannot
2423 * be removed). The API is offered for consistency with the other resource
2424 * types where Patch and Update APIs have different semantics.
2425 *
2426 * @param bucket_name the name of the bucket.
2427 * @param entity the identifier for the user, group, service account, or
2428 * predefined set of actors holding the permission.
2429 * @param original_acl the original ACL value.
2430 * @param new_acl the new ACL value. Note that only changes on writeable
2431 * fields will be accepted by the server.
2432 * @param options a list of optional query parameters and/or request
2433 * headers. Valid types for this operation include `UserProject`, as well
2434 * as the standard parameters, such as `IfMatchEtag`, and
2435 * `IfNoneMatchEtag`.
2436 *
2437 * @par Idempotency
2438 * This operation is only idempotent if restricted by pre-conditions. There
2439 * are no pre-conditions for this operation that can make it idempotent.
2440 *
2441 * @par Example
2442 * @snippet storage_default_object_acl_samples.cc patch default object acl
2443 *
2444 * @see
2445 * https://cloud.google.com/storage/docs/access-control/create-manage-lists#defaultobjects
2446 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2447 * the format of the @p entity parameter.
2448 * @see https://cloud.google.com/storage/docs/access-control/lists#permissions
2449 * for the format of the @p role parameter.
2450 */
2451 template <typename... Options>
2453 std::string const& bucket_name, std::string const& entity,
2454 ObjectAccessControl const& original_acl,
2455 ObjectAccessControl const& new_acl, Options&&... options) {
2456 google::cloud::internal::OptionsSpan const span(
2457 SpanOptions(std::forward<Options>(options)...));
2458 internal::PatchDefaultObjectAclRequest request(bucket_name, entity,
2459 original_acl, new_acl);
2460 request.set_multiple_options(std::forward<Options>(options)...);
2461 return raw_client_->PatchDefaultObjectAcl(request);
2462 }
2463
2464 /**
2465 * Patches the value of an existing default object ACL.
2466 *
2467 * This API allows the application to patch an ObjectAccessControl without
2468 * having to read the current value.
2469 *
2470 * @note
2471 * For changing default object access controls, the Patch and Update APIs
2472 * basically offer the same functionality. The only field that can be modified
2473 * by either API is `role`, and it may only be set to a new value (it cannot
2474 * be removed). The API is offered for consistency with the other resource
2475 * types where Patch and Update APIs have different semantics.
2476 *
2477 * @param bucket_name the name of the bucket.
2478 * @param entity the identifier for the user, group, service account, or
2479 * predefined set of actors holding the permission.
2480 * @param builder a builder ready to create the patch.
2481 * @param options a list of optional query parameters and/or request
2482 * headers. Valid types for this operation include `UserProject`, as well
2483 * as the standard parameters, such as `IfMatchEtag`, and
2484 * `IfNoneMatchEtag`.
2485 *
2486 * @par Idempotency
2487 * This operation is only idempotent if restricted by pre-conditions. There
2488 * are no pre-conditions for this operation that can make it idempotent.
2489 *
2490 * @par Example
2491 * @snippet storage_default_object_acl_samples.cc patch no-read
2492 *
2493 * @see
2494 * https://cloud.google.com/storage/docs/access-control/create-manage-lists#defaultobjects
2495 * @see https://cloud.google.com/storage/docs/access-control/lists#scopes for
2496 * the format of the @p entity parameter.
2497 * @see https://cloud.google.com/storage/docs/access-control/lists#permissions
2498 * for the format of the @p role parameter.
2499 */
2500 template <typename... Options>
2502 std::string const& bucket_name, std::string const& entity,
2503 ObjectAccessControlPatchBuilder const& builder, Options&&... options) {
2504 google::cloud::internal::OptionsSpan const span(
2505 SpanOptions(std::forward<Options>(options)...));
2506 internal::PatchDefaultObjectAclRequest request(bucket_name, entity,
2507 builder);
2508 request.set_multiple_options(std::forward<Options>(options)...);
2509 return raw_client_->PatchDefaultObjectAcl(request);
2510 }
2511 ///@}
2512
2513 /**
2514 * @name Service account operations.
2515 *
2516 * Service accounts allow applications to authenticate and access GCP
2517 * resources and services. When acting on your behalf, GCS uses such a service
2518 * account. GCS creates one service account per project. These operations
2519 * allow you to query the GCS service account for a project.
2520 *
2521 * @see https://cloud.google.com/storage/docs/projects#service-accounts for
2522 * more information on service accounts.
2523 */
2524 ///@{
2525 /**
2526 * Gets the GCS service account for a given project.
2527 *
2528 * A service account is a special Google account that belongs to your
2529 * application, virtual machine, or to a Google service when acting on your
2530 * behalf. This API allows you to discover the GCS service account for the
2531 * @p project_id project.
2532 *
2533 * @param project_id the project to query.
2534 * @param options a list of optional query parameters and/or request headers.
2535 * Valid types for this operation include `UserProject`.
2536 * `OverrideDefaultProject` is accepted, but has no effect.
2537 *
2538 * @par Idempotency
2539 * This is a read-only operation and is always idempotent.
2540 *
2541 * @par Example
2542 * @snippet storage_service_account_samples.cc get service account for project
2543 *
2544 * @see https://cloud.google.com/iam/docs/service-accounts for general
2545 * information on Google Cloud Platform service accounts.
2546 */
2547 template <typename... Options>
2549 std::string const& project_id, Options&&... options) {
2550 google::cloud::internal::OptionsSpan const span(
2551 SpanOptions(std::forward<Options>(options)...));
2552 internal::GetProjectServiceAccountRequest request(project_id);
2553 request.set_multiple_options(std::forward<Options>(options)...);
2554 return raw_client_->GetServiceAccount(request);
2555 }
2556
2557 /**
2558 * Gets the GCS service account for the default project.
2559 *
2560 * A service account is a special Google account that belongs to your
2561 * application, virtual machine, or to a Google service when acting on your
2562 * behalf. This API allows you to discover the GCS service account for the
2563 * default project associated with this object.
2564 *
2565 * The default project is required to be configured in the `ClientOptions`
2566 * used to construct this object. If the application does not set the project
2567 * id in the `ClientOptions`, the value of the `GOOGLE_CLOUD_PROJECT` is
2568 * used. If neither the environment variable is set, nor a value is set
2569 * explicitly by the application, the server will reject the request and this
2570 * function will return the error status.
2571 *
2572 * @param options a list of optional query parameters and/or request headers.
2573 * Valid types for this operation include `UserProject`,
2574 * and `OverrideDefaultProject`.
2575 *
2576 * @par Idempotency
2577 * This is a read-only operation and is always idempotent.
2578 *
2579 * @par Example
2580 * @snippet storage_service_account_samples.cc get service account
2581 *
2582 * @see https://cloud.google.com/iam/docs/service-accounts for general
2583 * information on Google Cloud Platform service accounts.
2584 */
2585 template <typename... Options>
2586 StatusOr<ServiceAccount> GetServiceAccount(Options&&... options) {
2587 auto opts = SpanOptions(std::forward<Options>(options)...);
2588 auto project_id = storage_internal::RequestProjectId(
2589 GCP_ERROR_INFO(), opts, std::forward<Options>(options)...);
2590 if (!project_id) return std::move(project_id).status();
2591 google::cloud::internal::OptionsSpan const span(std::move(opts));
2592 internal::GetProjectServiceAccountRequest request(*std::move(project_id));
2593 request.set_multiple_options(std::forward<Options>(options)...);
2594 return raw_client_->GetServiceAccount(request);
2595 }
2596
2597 /**
2598 * List the available HMAC keys.
2599 *
2600 * @warning This GCS feature is not GA, it is subject to change without
2601 * notice.
2602 *
2603 * @param options a list of optional query parameters and/or request headers.
2604 * In addition to the options common to all requests, this operation
2605 * accepts `Deleted` `MaxResults`, `OverrideDefaultProject`,
2606 * and `ServiceAccountFilter`.
2607 *
2608 * @return A range to iterate over the available HMAC keys.
2609 *
2610 * @par Idempotency
2611 * This is a read-only operation and is always idempotent.
2612 *
2613 * @par Example
2614 * @snippet storage_service_account_samples.cc list hmac keys
2615 *
2616 * @par Example
2617 * @snippet storage_service_account_samples.cc list hmac keys service account
2618 *
2619 * @see https://cloud.google.com/storage/docs/authentication/hmackeys for
2620 * general information on using HMAC keys for Google Cloud Platform
2621 * service accounts authentication.
2622 *
2623 * @see https://cloud.google.com/storage/docs/authentication/managing-hmackeys
2624 * for a detailed description on how to use the feature.
2625 */
2626 template <typename... Options>
2627 ListHmacKeysReader ListHmacKeys(Options&&... options) {
2628 auto opts = SpanOptions(std::forward<Options>(options)...);
2629 auto project_id = storage_internal::RequestProjectId(
2630 GCP_ERROR_INFO(), opts, std::forward<Options>(options)...);
2631 if (!project_id) {
2632 return google::cloud::internal::MakeErrorPaginationRange<
2633 ListHmacKeysReader>(std::move(project_id).status());
2634 }
2635 google::cloud::internal::OptionsSpan const span(std::move(opts));
2636
2637 internal::ListHmacKeysRequest request(*std::move(project_id));
2638 request.set_multiple_options(std::forward<Options>(options)...);
2639 return google::cloud::internal::MakePaginationRange<ListHmacKeysReader>(
2640 request,
2641 [stub = raw_client_](internal::ListHmacKeysRequest const& r) {
2642 return stub->ListHmacKeys(r);
2643 },
2644 [](internal::ListHmacKeysResponse r) { return std::move(r.items); });
2645 }
2646
2647 /**
2648 * Create a new HMAC key.
2649 *
2650 * @param service_account the service account email where you want to create
2651 * the new HMAC key.
2652 * @param options a list of optional query parameters and/or request headers.
2653 * In addition to the options common to all requests, this operation
2654 * accepts `OverrideDefaultProject`.
2655 *
2656 * @return This operation returns the new HMAC key metadata *and* the HMAC key
2657 * secret (encoded as a base64 string). This is the only request that
2658 * returns the secret.
2659 *
2660 * @par Idempotency
2661 * This operation is not idempotent. Retrying the operation will create a new
2662 * key each time.
2663 *
2664 * @par Example
2665 * @snippet storage_service_account_samples.cc create hmac key
2666 *
2667 * @par Example
2668 * @snippet storage_service_account_samples.cc create hmac key project
2669 *
2670 * @see https://cloud.google.com/storage/docs/authentication/hmackeys for
2671 * general information on using HMAC keys for Google Cloud Platform
2672 * service accounts authentication.
2673 *
2674 * @see https://cloud.google.com/storage/docs/authentication/managing-hmackeys
2675 * for a detailed description on how to use the feature.
2676 */
2677 template <typename... Options>
2678 StatusOr<std::pair<HmacKeyMetadata, std::string>> CreateHmacKey(
2679 std::string service_account, Options&&... options) {
2680 auto opts = SpanOptions(std::forward<Options>(options)...);
2681 auto project_id = storage_internal::RequestProjectId(
2682 GCP_ERROR_INFO(), opts, std::forward<Options>(options)...);
2683 if (!project_id) return std::move(project_id).status();
2684 google::cloud::internal::OptionsSpan const span(std::move(opts));
2685
2686 internal::CreateHmacKeyRequest request(*std::move(project_id),
2687 std::move(service_account));
2688 request.set_multiple_options(std::forward<Options>(options)...);
2689 auto result = raw_client_->CreateHmacKey(request);
2690 if (!result) return std::move(result).status();
2691 return std::make_pair(std::move(result->metadata),
2692 std::move(result->secret));
2693 }
2694
2695 /**
2696 * Delete a HMAC key in a given project.
2697 *
2698 * @param access_id the HMAC key `access_id()` that you want to delete. Each
2699 * HMAC key is assigned an `access_id()` attribute at creation time.
2700 * @param options a list of optional query parameters and/or request headers.
2701 * In addition to the options common to all requests, this operation
2702 * accepts `OverrideDefaultProject`.
2703 *
2704 * @return This operation returns the new HMAC key metadata.
2705 *
2706 * @par Idempotency
2707 * This operation is always idempotent. An access id identifies a single HMAC
2708 * key, calling the operation multiple times can succeed only once.
2709 *
2710 * @par Example
2711 * @snippet storage_service_account_samples.cc delete hmac key
2712 *
2713 * @see https://cloud.google.com/storage/docs/authentication/hmackeys for
2714 * general information on using HMAC keys for Google Cloud Platform
2715 * service accounts authentication.
2716 *
2717 * @see https://cloud.google.com/storage/docs/authentication/managing-hmackeys
2718 * for a detailed description on how to use the feature.
2719 */
2720 template <typename... Options>
2721 Status DeleteHmacKey(std::string access_id, Options&&... options) {
2722 auto opts = SpanOptions(std::forward<Options>(options)...);
2723 auto project_id = storage_internal::RequestProjectId(
2724 GCP_ERROR_INFO(), opts, std::forward<Options>(options)...);
2725 if (!project_id) return std::move(project_id).status();
2726 google::cloud::internal::OptionsSpan const span(std::move(opts));
2727
2728 internal::DeleteHmacKeyRequest request(*std::move(project_id),
2729 std::move(access_id));
2730 request.set_multiple_options(std::forward<Options>(options)...);
2731 return raw_client_->DeleteHmacKey(request).status();
2732 }
2733
2734 /**
2735 * Get an existing HMAC key in a given project.
2736 *
2737 * @param access_id the HMAC key `access_id()` that you want to delete. Each
2738 * HMAC key is assigned an `access_id()` attribute at creation time.
2739 * @param options a list of optional query parameters and/or request headers.
2740 * In addition to the options common to all requests, this operation
2741 * accepts `OverrideDefaultProject`.
2742 *
2743 * @return This operation returns the new HMAC key metadata.
2744 *
2745 * @par Idempotency
2746 * This is a read-only operation and therefore it is always idempotent.
2747 *
2748 * @par Example
2749 * @snippet storage_service_account_samples.cc get hmac key
2750 *
2751 * @see https://cloud.google.com/storage/docs/authentication/hmackeys for
2752 * general information on using HMAC keys for Google Cloud Platform
2753 * service accounts authentication.
2754 *
2755 * @see https://cloud.google.com/storage/docs/authentication/managing-hmackeys
2756 * for a detailed description on how to use the feature.
2757 */
2758 template <typename... Options>
2759 StatusOr<HmacKeyMetadata> GetHmacKey(std::string access_id,
2760 Options&&... options) {
2761 auto opts = SpanOptions(std::forward<Options>(options)...);
2762 auto project_id = storage_internal::RequestProjectId(
2763 GCP_ERROR_INFO(), opts, std::forward<Options>(options)...);
2764 if (!project_id) return std::move(project_id).status();
2765 google::cloud::internal::OptionsSpan const span(std::move(opts));
2766
2767 internal::GetHmacKeyRequest request(*std::move(project_id),
2768 std::move(access_id));
2769 request.set_multiple_options(std::forward<Options>(options)...);
2770 return raw_client_->GetHmacKey(request);
2771 }
2772
2773 /**
2774 * Update an existing HMAC key in a given project.
2775 *
2776 * @param access_id the HMAC key `access_id()` that you want to delete. Each
2777 * HMAC key is assigned an `access_id()` attribute at creation time.
2778 * @param resource the desired changes to the HMAC key resource. Only the
2779 * `state` field may be changed. The `etag` field may be set but it is
2780 * only used as a pre-condition, the application cannot set the `etag`.
2781 * @param options a list of optional query parameters and/or request headers.
2782 * In addition to the options common to all requests, this operation
2783 * accepts `OverrideDefaultProject`.
2784 *
2785 * @return This operation returns the new HMAC key metadata.
2786 *
2787 * @par Idempotency
2788 * This operation is only idempotent if the `etag` attribute in @p resource
2789 * is set, or if the `IfMatchEtag` option is set.
2790 *
2791 * @par Example
2792 * @snippet storage_service_account_samples.cc update hmac key
2793 *
2794 * @see https://cloud.google.com/storage/docs/authentication/hmackeys for
2795 * general information on using HMAC keys for Google Cloud Platform
2796 * service accounts authentication.
2797 *
2798 * @see https://cloud.google.com/storage/docs/authentication/managing-hmackeys
2799 * for a detailed description on how to use the feature.
2800 */
2801 template <typename... Options>
2802 StatusOr<HmacKeyMetadata> UpdateHmacKey(std::string access_id,
2803 HmacKeyMetadata resource,
2804 Options&&... options) {
2805 auto opts = SpanOptions(std::forward<Options>(options)...);
2806 auto project_id = storage_internal::RequestProjectId(
2807 GCP_ERROR_INFO(), opts, std::forward<Options>(options)...);
2808 if (!project_id) return std::move(project_id).status();
2809 google::cloud::internal::OptionsSpan const span(std::move(opts));
2810
2811 internal::UpdateHmacKeyRequest request(
2812 *std::move(project_id), std::move(access_id), std::move(resource));
2813 request.set_multiple_options(std::forward<Options>(options)...);
2814 return raw_client_->UpdateHmacKey(request);
2815 }
2816 ///@}
2817
2818 /// @name Signed URL support operations.
2819 ///@{
2820 /**
2821 * Create a V2 signed URL for the given parameters.
2822 *
2823 * @note By default URLs created with this function expire after 7 days.
2824 *
2825 * @note The application must ensure that any URL created with this function
2826 * is a valid request via the XML API. For example, the options for
2827 * bucket requests may include a sub-resource (e.g. `WithBilling()`) but
2828 * not all sub-resources are valid for objects. Likewise, only a single
2829 * sub-resource may be retrieved in each request.
2830 *
2831 * @param verb the operation allowed through this signed URL, `GET`, `POST`,
2832 * `PUT`, `HEAD`, etc. are valid values.
2833 * @param bucket_name the name of the bucket.
2834 * @param object_name the name of the object, note that the object may not
2835 * exist for signed URLs that upload new objects. Use an empty string for
2836 * requests that only affect a bucket.
2837 * @param options a list of optional parameters for the signed URL, this
2838 * include: `ExpirationTime`, `MD5HashValue`, `ContentType`,
2839 * `SigningAccount`, `SigningAccountDelegates`,
2840 * `AddExtensionHeaderOption`, `AddQueryParameterOption`, and
2841 * `AddSubResourceOption`. Note that only the last `AddSubResourceOption`
2842 * option has any effect.
2843 *
2844 * @par Helper Functions
2845 *
2846 * The following functions create a `AddSubResourceOption` with less
2847 * opportunities for typos in the sub-resource name: `WithAcl()`,
2848 * `WithBilling()`, `WithCompose()`, `WithCors()`, `WithEncryption()`,
2849 * `WithEncryptionConfig()`, `WithLifecycle()`, `WithLocation()`,
2850 * `WithLogging()`, `WithStorageClass()`, and `WithTagging()`.
2851 *
2852 * Likewise, the following helper functions can create properly formatted
2853 * `AddExtensionHeaderOption` objects: `WithGeneration()`,
2854 * `WithGenerationMarker()`, `WithMarker()`,
2855 * `WithResponseContentDisposition()`, `WithResponseContentType()`, and
2856 * `WithUserProject()`.
2857 *
2858 * @return the signed URL.
2859 *
2860 * @par Example
2861 * @snippet storage_signed_url_v2_samples.cc sign url v2
2862 *
2863 * @par Example
2864 * @snippet storage_signed_url_v2_samples.cc create put signed url v2
2865 *
2866 * @see https://cloud.google.com/storage/docs/access-control/signed-urls for
2867 * a general description of signed URLs and how they can be used.
2868 *
2869 * @see https://cloud.google.com/storage/docs/xml-api/overview for a detailed
2870 * description of the XML API.
2871 */
2872 template <typename... Options>
2873 StatusOr<std::string> CreateV2SignedUrl(std::string verb,
2874 std::string bucket_name,
2875 std::string object_name,
2876 Options&&... options) {
2877 google::cloud::internal::OptionsSpan const span(
2878 SpanOptions(std::forward<Options>(options)...));
2879 internal::V2SignUrlRequest request(std::move(verb), std::move(bucket_name),
2880 std::move(object_name));
2881 request.set_multiple_options(std::forward<Options>(options)...);
2882 return SignUrlV2(request);
2883 }
2884
2885 /**
2886 * Create a V4 signed URL for the given parameters.
2887 *
2888 * @note By default URLs created with this function expire after 7 days.
2889 *
2890 * @note The application must ensure that any URL created with this function
2891 * is a valid request via the XML API. For example, the options for
2892 * bucket requests may include a sub-resource (e.g. `WithBilling()`) but
2893 * not all sub-resources are valid for objects. Likewise, only a single
2894 * sub-resource may be retrieved in each request.
2895 *
2896 * @param verb the operation allowed through this signed URL, `GET`, `POST`,
2897 * `PUT`, `HEAD`, etc. are valid values.
2898 * @param bucket_name the name of the bucket.
2899 * @param object_name the name of the object, note that the object may not
2900 * exist for signed URLs that upload new objects. Use an empty string for
2901 * requests that only affect a bucket.
2902 * @param options a list of optional parameters for the signed URL, this
2903 * include: `SignedUrlTimestamp`, `SignedUrlDuration`, `MD5HashValue`,
2904 * `ContentType`, `SigningAccount`, `SigningAccountDelegates`,
2905 * `AddExtensionHeaderOption`, `AddQueryParameterOption`, and
2906 * `AddSubResourceOption`. Note that only the last `AddSubResourceOption`
2907 * option has any effect.
2908 *
2909 * @par Helper Functions
2910 *
2911 * The following functions create a `AddSubResourceOption` with less
2912 * opportunities for typos in the sub-resource name: `WithAcl()`,
2913 * `WithBilling()`, `WithCompose()`, `WithCors()`, `WithEncryption()`,
2914 * `WithEncryptionConfig()`, `WithLifecycle()`, `WithLocation()`,
2915 * `WithLogging()`, `WithStorageClass()`, and `WithTagging()`.
2916 *
2917 * Likewise, the following helper functions can create properly formatted
2918 * `AddExtensionHeaderOption` objects: `WithGeneration()`,
2919 * `WithGenerationMarker()`, `WithMarker()`,
2920 * `WithResponseContentDisposition()`, `WithResponseContentType()`, and
2921 * `WithUserProject()`.
2922 *
2923 * @return the signed URL.
2924 *
2925 * @par Example
2926 * @snippet storage_signed_url_v4_samples.cc sign url v4
2927 *
2928 * @par Example
2929 * @snippet storage_signed_url_v4_samples.cc create put signed url v4
2930 *
2931 * @see https://cloud.google.com/storage/docs/access-control/signed-urls for
2932 * a general description of signed URLs and how they can be used.
2933 *
2934 * @see https://cloud.google.com/storage/docs/xml-api/overview for a detailed
2935 * description of the XML API.
2936 */
2937 template <typename... Options>
2938 StatusOr<std::string> CreateV4SignedUrl(std::string verb,
2939 std::string bucket_name,
2940 std::string object_name,
2941 Options&&... options) {
2942 google::cloud::internal::OptionsSpan const span(
2943 SpanOptions(std::forward<Options>(options)...));
2944 internal::V4SignUrlRequest request(std::move(verb), std::move(bucket_name),
2945 std::move(object_name));
2946 request.set_multiple_options(std::forward<Options>(options)...);
2947 return SignUrlV4(std::move(request));
2948 }
2949 ///@}
2950
2951 /**
2952 * Create a signed policy document.
2953 *
2954 * @note The application must ensure that any document created with this
2955 * function contains valid conditions. This function does not do any error
2956 * checking, e.g. that a `ExactMatchObject()` condition contains two
2957 * elements. Using the provided helper functions can prevent errors.
2958 *
2959 * @note It is the application's responsibility to construct a POST request
2960 * based on the value returned by this function. For example, a web
2961 * application can create a HTML form containing these fields, the result of
2962 * which is a POST request to GCS.
2963 *
2964 * @param document the policy document.
2965 * @param options a list of optional parameters, this includes:
2966 * `SigningAccount`, and `SigningAccountDelegates`.
2967 *
2968 * @par Helper Functions
2969 * The following functions create a `PolicyDocumentCondition` with less
2970 * opportunities for typos: `StartsWith()`, `ExactMatchObject()`,
2971 * `ExactMatch()`, `ContentLengthRange()`.
2972 *
2973 * @par Example
2974 * @snippet storage_policy_doc_samples.cc create signed policy document
2975 *
2976 * @see
2977 * https://cloud.google.com/storage/docs/xml-api/post-object#policydocument
2978 * for a general description of policy documents and how they can be used.
2979 *
2980 * @see https://cloud.google.com/storage/docs/xml-api/overview for a detailed
2981 * description of the XML API.
2982 */
2983 template <typename... Options>
2985 PolicyDocument document, Options&&... options) {
2986 google::cloud::internal::OptionsSpan const span(
2987 SpanOptions(std::forward<Options>(options)...));
2988 internal::PolicyDocumentRequest request(std::move(document));
2989 request.set_multiple_options(std::forward<Options>(options)...);
2990 return SignPolicyDocument(request);
2991 }
2992
2993 /**
2994 * Create a signed V4 policy document.
2995 *
2996 * @note The application must ensure that any document created with this
2997 * function contains valid conditions. This function does not do any error
2998 * checking, e.g. that a `ExactMatchObject()` condition contains two
2999 * elements. Using the provided helper functions can prevent errors.
3000 *
3001 * @note It is the application's responsibility to construct a POST request
3002 * based on the value returned by this function. For example, a web
3003 * application can create a HTML form containing these fields, the result of
3004 * which is a POST request to GCS.
3005 *
3006 * @param document the policy document.
3007 * @param options a list of optional parameters, this includes:
3008 * `AddExtensionFieldOption`, `BucketBoundHostname`, `PredefinedAcl`,
3009 * `Scheme`, `SigningAccountDelegates`, `SigningAccount`, `VirtualHostname`
3010 *
3011 * @par Helper Functions
3012 * The following functions create a `PolicyDocumentCondition` with less
3013 * opportunities for typos: `StartsWith()`, `ExactMatchObject()`,
3014 * `ExactMatch()`, `ContentLengthRange()`.
3015 *
3016 * @par Example
3017 * @snippet storage_policy_doc_samples.cc create signed policy document v4
3018 *
3019 * @see
3020 * https://cloud.google.com/storage/docs/xml-api/post-object#policydocument
3021 * for a general description of policy documents and how they can be used.
3022 *
3023 * @see https://cloud.google.com/storage/docs/xml-api/overview for a detailed
3024 * description of the XML API.
3025 */
3026 template <typename... Options>
3028 PolicyDocumentV4 document, Options&&... options) {
3029 google::cloud::internal::OptionsSpan const span(
3030 SpanOptions(std::forward<Options>(options)...));
3031 internal::PolicyDocumentV4Request request(std::move(document));
3032 request.set_multiple_options(std::forward<Options>(options)...);
3033 return SignPolicyDocumentV4(std::move(request));
3034 }
3035
3036 /**
3037 * @name Pub/Sub operations.
3038 *
3039 * Cloud Pub/Sub Notifications sends information about changes to objects in
3040 * your buckets to Cloud Pub/Sub, where the information is added to a Cloud
3041 * Pub/Sub topic of your choice in the form of messages.
3042 *
3043 * @see https://cloud.google.com/storage/docs/pubsub-notifications for more
3044 * information about Cloud Pub/Sub in the context of GCS.
3045 */
3046 ///@{
3047 /**
3048 * Retrieves the list of Notifications for a Bucket.
3049 *
3050 * Cloud Pub/Sub Notifications sends information about changes to objects
3051 * in your buckets to Google Cloud Pub/Sub service.
3052 *
3053 * @param bucket_name the name of the bucket.
3054 * @param options a list of optional query parameters and/or request headers.
3055 * Valid types for this operation include `UserProject`.
3056 *
3057 * @par Idempotency
3058 * This is a read-only operation and is always idempotent.
3059 *
3060 * @par Example
3061 * @snippet storage_notification_samples.cc list notifications
3062 */
3063 template <typename... Options>
3064 StatusOr<std::vector<NotificationMetadata>> ListNotifications(
3065 std::string const& bucket_name, Options&&... options) {
3066 google::cloud::internal::OptionsSpan const span(
3067 SpanOptions(std::forward<Options>(options)...));
3068 internal::ListNotificationsRequest request(bucket_name);
3069 request.set_multiple_options(std::forward<Options>(options)...);
3070 auto result = raw_client_->ListNotifications(request);
3071 if (!result) {
3072 return std::move(result).status();
3073 }
3074 return std::move(result.value().items);
3075 }
3076
3077 /**
3078 * Creates a new notification config for a Bucket.
3079 *
3080 * Cloud Pub/Sub Notifications send information about changes to objects
3081 * in your buckets to Google Cloud Pub/Sub service. You can create multiple
3082 * notifications per Bucket, with different topics and filtering options.
3083 *
3084 * @param bucket_name the name of the bucket.
3085 * @param topic_name the Google Cloud Pub/Sub topic that will receive the
3086 * notifications. This requires the full name of the topic, i.e.:
3087 * `projects/<PROJECT_ID>/topics/<TOPIC_ID>`.
3088 * @param metadata define any optional parameters for the notification, such
3089 * as the list of event types, or any custom attributes.
3090 * @param options a list of optional query parameters and/or request headers.
3091 * Valid types for this operation include `UserProject`.
3092 *
3093 * @par Idempotency
3094 * This operation is only idempotent if restricted by pre-conditions. There
3095 * are no pre-conditions for this operation that can make it idempotent.
3096 *
3097 * @par Example
3098 * @snippet storage_notification_samples.cc create notification
3099 *
3100 * @see https://cloud.google.com/storage/docs/pubsub-notifications for general
3101 * information on Cloud Pub/Sub Notifications for Google Cloud Storage.
3102 *
3103 * @see https://cloud.google.com/pubsub/ for general information on Google
3104 * Cloud Pub/Sub service.
3105 */
3106 template <typename... Options>
3108 std::string const& bucket_name, std::string const& topic_name,
3109 NotificationMetadata metadata, Options&&... options) {
3110 google::cloud::internal::OptionsSpan const span(
3111 SpanOptions(std::forward<Options>(options)...));
3112 return CreateNotification(bucket_name, topic_name,
3113 payload_format::JsonApiV1(), std::move(metadata),
3114 std::forward<Options>(options)...);
3115 }
3116
3117 /**
3118 * Creates a new notification config for a Bucket.
3119 *
3120 * Cloud Pub/Sub Notifications send information about changes to objects
3121 * in your buckets to Google Cloud Pub/Sub service. You can create multiple
3122 * notifications per Bucket, with different topics and filtering options.
3123 *
3124 * @param bucket_name the name of the bucket.
3125 * @param topic_name the Google Cloud Pub/Sub topic that will receive the
3126 * notifications. This requires the full name of the topic, i.e.:
3127 * `projects/<PROJECT_ID>/topics/<TOPIC_ID>`.
3128 * @param payload_format how will the data be formatted in the notifications,
3129 * consider using the helpers in the `payload_format` namespace, or
3130 * specify one of the valid formats defined in:
3131 * https://cloud.google.com/storage/docs/json_api/v1/notifications
3132 * @param metadata define any optional parameters for the notification, such
3133 * as the list of event types, or any custom attributes.
3134 * @param options a list of optional query parameters and/or request headers.
3135 * Valid types for this operation include `UserProject`.
3136 *
3137 * @par Idempotency
3138 * This operation is only idempotent if restricted by pre-conditions. There
3139 * are no pre-conditions for this operation that can make it idempotent.
3140 *
3141 * @par Example
3142 * @snippet storage_notification_samples.cc create notification
3143 *
3144 * @see https://cloud.google.com/storage/docs/pubsub-notifications for general
3145 * information on Cloud Pub/Sub Notifications for Google Cloud Storage.
3146 *
3147 * @see https://cloud.google.com/pubsub/ for general information on Google
3148 * Cloud Pub/Sub service.
3149 */
3150 template <typename... Options>
3152 std::string const& bucket_name, std::string const& topic_name,
3153 std::string const& payload_format, NotificationMetadata metadata,
3154 Options&&... options) {
3155 google::cloud::internal::OptionsSpan const span(
3156 SpanOptions(std::forward<Options>(options)...));
3157 metadata.set_topic(topic_name).set_payload_format(payload_format);
3158 internal::CreateNotificationRequest request(bucket_name, metadata);
3159 request.set_multiple_options(std::forward<Options>(options)...);
3160 return raw_client_->CreateNotification(request);
3161 }
3162
3163 /**
3164 * Gets the details about a notification config in a given Bucket.
3165 *
3166 * Cloud Pub/Sub Notifications sends information about changes to objects
3167 * in your buckets to Google Cloud Pub/Sub service. You can create multiple
3168 * notifications per Bucket, with different topics and filtering options. This
3169 * function fetches the detailed information for a given notification config.
3170 *
3171 * @param bucket_name the name of the bucket.
3172 * @param notification_id the id of the notification config.
3173 * @param options a list of optional query parameters and/or request headers.
3174 * Valid types for this operation include `UserProject`.
3175 *
3176 * @par Idempotency
3177 * This is a read-only operation and is always idempotent.
3178 *
3179 * @par Example
3180 * @snippet storage_notification_samples.cc get notification
3181 *
3182 * @see https://cloud.google.com/storage/docs/pubsub-notifications for general
3183 * information on Cloud Pub/Sub Notifications for Google Cloud Storage.
3184 *
3185 * @see https://cloud.google.com/pubsub/ for general information on Google
3186 * Cloud Pub/Sub service.
3187 */
3188 template <typename... Options>
3190 std::string const& bucket_name, std::string const& notification_id,
3191 Options&&... options) {
3192 google::cloud::internal::OptionsSpan const span(
3193 SpanOptions(std::forward<Options>(options)...));
3194 internal::GetNotificationRequest request(bucket_name, notification_id);
3195 request.set_multiple_options(std::forward<Options>(options)...);
3196 return raw_client_->GetNotification(request);
3197 }
3198
3199 /**
3200 * Delete an existing notification config in a given Bucket.
3201 *
3202 * Cloud Pub/Sub Notifications sends information about changes to objects
3203 * in your buckets to Google Cloud Pub/Sub service. You can create multiple
3204 * notifications per Bucket, with different topics and filtering options. This
3205 * function deletes one of the notification configs.
3206 *
3207 * @param bucket_name the name of the bucket.
3208 * @param notification_id the id of the notification config.
3209 * @param options a list of optional query parameters and/or request headers.
3210 * Valid types for this operation include `UserProject`.
3211 *
3212 * @par Idempotency
3213 * This operation is always idempotent because it only acts on a specific
3214 * `notification_id`, the state after calling this function multiple times is
3215 * to delete that notification. New notifications get different ids.
3216 *
3217 * @par Example
3218 * @snippet storage_notification_samples.cc delete notification
3219 *
3220 * @see https://cloud.google.com/storage/docs/pubsub-notifications for general
3221 * information on Cloud Pub/Sub Notifications for Google Cloud Storage.
3222 *
3223 * @see https://cloud.google.com/pubsub/ for general information on Google
3224 * Cloud Pub/Sub service.
3225 */
3226 template <typename... Options>
3227 Status DeleteNotification(std::string const& bucket_name,
3228 std::string const& notification_id,
3229 Options&&... options) {
3230 google::cloud::internal::OptionsSpan const span(
3231 SpanOptions(std::forward<Options>(options)...));
3232 internal::DeleteNotificationRequest request(bucket_name, notification_id);
3233 request.set_multiple_options(std::forward<Options>(options)...);
3234 return std::move(raw_client_->DeleteNotification(request)).status();
3235 }
3236 ///@}
3237
3238 /**
3239 * Creates the default client type given the options.
3240 *
3241 * @param options the client options, these are used to control credentials,
3242 * buffer sizes, etc.
3243 * @param policies the client policies, these control the behavior of the
3244 * client, for example, how to backoff when an operation needs to be
3245 * retried, or what operations cannot be retried because they are not
3246 * idempotent.
3247 *
3248 * @deprecated use the constructor from `google::cloud::Options` instead.
3249 */
3250 template <typename... Policies>
3251 explicit Client(ClientOptions options, Policies&&... policies)
3252 : Client(InternalOnly{}, internal::ApplyPolicies(
3253 internal::MakeOptions(std::move(options)),
3254 std::forward<Policies>(policies)...)) {}
3255
3256 /**
3257 * Creates the default client type given the credentials and policies.
3258 *
3259 * @param credentials a set of credentials to initialize the `ClientOptions`.
3260 * @param policies the client policies, these control the behavior of the
3261 * client, for example, how to backoff when an operation needs to be
3262 * retried, or what operations cannot be retried because they are not
3263 * idempotent.
3264 *
3265 * @deprecated use the constructor from `google::cloud::Options` instead.
3266 */
3267 template <typename... Policies>
3268 explicit Client(std::shared_ptr<oauth2::Credentials> credentials,
3269 Policies&&... policies)
3270 : Client(InternalOnly{},
3271 internal::ApplyPolicies(
3272 internal::DefaultOptions(std::move(credentials), {}),
3273 std::forward<Policies>(policies)...)) {}
3274
3275 /**
3276 * Create a Client using ClientOptions::CreateDefaultClientOptions().
3277 *
3278 * @deprecated use the constructor from `google::cloud::Options` instead.
3279 */
3280 static StatusOr<Client> CreateDefaultClient();
3281
3282 /// Builds a client and maybe override the retry, idempotency, and/or backoff
3283 /// policies.
3284 /// @deprecated This was intended only for test code, applications should not
3285 /// use it.
3286 template <typename... Policies>
3287#if !defined(_MSC_VER) || _MSC_VER >= 1920
3288 GOOGLE_CLOUD_CPP_DEPRECATED(
3289 "applications should not need this."
3290 " Please use the constructors from ClientOptions instead."
3291 " For mocking, please use testing::ClientFromMock() instead."
3292 " Please file a bug at https://github.com/googleapis/google-cloud-cpp"
3293 " if you have a use-case not covered by these.")
3294#endif // _MSC_VER
3295 // NOLINTNEXTLINE(performance-unnecessary-value-param)
3296 explicit Client(std::shared_ptr<internal::RawClient> client,
3297 Policies&&... policies)
3298 : Client(InternalOnlyNoDecorations{},
3299 CreateDefaultInternalClient(
3300 internal::ApplyPolicies(
3301 internal::DefaultOptions(
3302 client->client_options().credentials(), {}),
3303 std::forward<Policies>(policies)...),
3304 client)) {
3305 }
3306
3307 /// Define a tag to disable automatic decorations of the RawClient.
3308 struct NoDecorations {};
3309
3310 /// Builds a client with a specific RawClient, without decorations.
3311 /// @deprecated This was intended only for test code, applications should not
3312 /// use it.
3313 GOOGLE_CLOUD_CPP_DEPRECATED(
3314 "applications should not need this."
3315 " Please file a bug at https://github.com/googleapis/google-cloud-cpp"
3316 " if you do.")
3317 explicit Client(std::shared_ptr<internal::RawClient> client, NoDecorations)
3318 : Client(InternalOnlyNoDecorations{}, std::move(client)) {}
3319
3320 /// Access the underlying `RawClient`.
3321 /// @deprecated Only intended for implementors, do not use.
3322 GOOGLE_CLOUD_CPP_DEPRECATED(
3323 "applications should not need this."
3324 " Please file a bug at https://github.com/googleapis/google-cloud-cpp"
3325 " if you do.")
3326 std::shared_ptr<internal::RawClient> raw_client() const {
3327 return raw_client_;
3328 }
3329
3330 private:
3331 friend class internal::NonResumableParallelUploadState;
3332 friend class internal::ResumableParallelUploadState;
3333 friend internal::ClientImplDetails;
3334
3335 struct InternalOnly {};
3336 struct InternalOnlyNoDecorations {};
3337
3338 Client(InternalOnly, Options const& opts)
3339 : raw_client_(CreateDefaultInternalClient(opts)) {}
3340 Client(InternalOnlyNoDecorations, std::shared_ptr<internal::RawClient> c)
3341 : raw_client_(std::move(c)) {}
3342
3343 static std::shared_ptr<internal::RawClient> CreateDefaultInternalClient(
3344 Options const& opts, std::shared_ptr<internal::RawClient> client);
3345 static std::shared_ptr<internal::RawClient> CreateDefaultInternalClient(
3346 Options const& opts);
3347
3348 ObjectReadStream ReadObjectImpl(
3349 internal::ReadObjectRangeRequest const& request);
3350
3351 ObjectWriteStream WriteObjectImpl(
3352 internal::ResumableUploadRequest const& request);
3353
3354 template <typename... RequestOptions>
3355 google::cloud::Options SpanOptions(RequestOptions&&... o) const {
3356 return google::cloud::internal::GroupOptions(
3357 raw_client_->options(), std::forward<RequestOptions>(o)...);
3358 }
3359
3360 // The version of UploadFile() where UseResumableUploadSession is one of the
3361 // options. Note how this does not use InsertObjectMedia at all.
3362 template <typename... Options>
3363 StatusOr<ObjectMetadata> UploadFileImpl(std::string const& file_name,
3364 std::string const& bucket_name,
3365 std::string const& object_name,
3366 std::true_type,
3367 Options&&... options) {
3368 google::cloud::internal::OptionsSpan const span(
3369 SpanOptions(std::forward<Options>(options)...));
3370 internal::ResumableUploadRequest request(bucket_name, object_name);
3371 request.set_multiple_options(std::forward<Options>(options)...);
3372 return UploadFileResumable(file_name, std::move(request));
3373 }
3374
3375 // The version of UploadFile() where UseResumableUploadSession is *not* one of
3376 // the options. In this case we can use InsertObjectMediaRequest because it
3377 // is safe.
3378 template <typename... Options>
3379 StatusOr<ObjectMetadata> UploadFileImpl(std::string const& file_name,
3380 std::string const& bucket_name,
3381 std::string const& object_name,
3382 std::false_type,
3383 Options&&... options) {
3384 google::cloud::internal::OptionsSpan const span(
3385 SpanOptions(std::forward<Options>(options)...));
3386 std::size_t file_size = 0;
3387 if (UseSimpleUpload(file_name, file_size)) {
3388 internal::InsertObjectMediaRequest request(bucket_name, object_name,
3389 std::string{});
3390 request.set_multiple_options(std::forward<Options>(options)...);
3391 return UploadFileSimple(file_name, file_size, request);
3392 }
3393 internal::ResumableUploadRequest request(bucket_name, object_name);
3394 request.set_multiple_options(std::forward<Options>(options)...);
3395 return UploadFileResumable(file_name, std::move(request));
3396 }
3397
3398 static bool UseSimpleUpload(std::string const& file_name, std::size_t& size);
3399
3400 StatusOr<ObjectMetadata> UploadFileSimple(
3401 std::string const& file_name, std::size_t file_size,
3402 internal::InsertObjectMediaRequest request);
3403
3404 StatusOr<ObjectMetadata> UploadFileResumable(
3405 std::string const& file_name, internal::ResumableUploadRequest request);
3406
3407 StatusOr<ObjectMetadata> UploadStreamResumable(
3408 std::istream& source,
3409 internal::ResumableUploadRequest const& request) const;
3410
3411 Status DownloadFileImpl(internal::ReadObjectRangeRequest const& request,
3412 std::string const& file_name);
3413
3414 /// Determine the email used to sign a blob.
3415 std::string SigningEmail(SigningAccount const& signing_account) const;
3416
3417 /// Represents the result of signing a blob, including the key used in the
3418 /// signature.
3419 struct SignBlobResponseRaw {
3420 std::string key_id;
3421 std::vector<std::uint8_t> signed_blob;
3422 };
3423
3424 /// Sign a blob.
3425 StatusOr<SignBlobResponseRaw> SignBlobImpl(
3426 SigningAccount const& signing_account, std::string const& string_to_sign);
3427
3428 StatusOr<std::string> SignUrlV2(internal::V2SignUrlRequest const& request);
3429 StatusOr<std::string> SignUrlV4(internal::V4SignUrlRequest request);
3430
3431 StatusOr<PolicyDocumentResult> SignPolicyDocument(
3432 internal::PolicyDocumentRequest const& request);
3433 StatusOr<PolicyDocumentV4Result> SignPolicyDocumentV4(
3434 internal::PolicyDocumentV4Request request);
3435
3436 std::shared_ptr<internal::RawClient> raw_client_;
3437};
3438
3439/**
3440 * Create a random prefix for object names.
3441 *
3442 * This is useful for operations which require a unique prefix for temporary
3443 * files.
3444 *
3445 * This is a helper function and it doesn't communicate with GCS, so there is a
3446 * very small chance that names generated this way aren't unique. The chance
3447 * should be small enough to fit any error budget.
3448 *
3449 * Given the same `prefix`, the randomly generated names will have the same
3450 * length.
3451 *
3452 * @param prefix the prefix of the prefix to be created.
3453 * @return the random prefix name
3454 */
3455std::string CreateRandomPrefixName(std::string const& prefix = "");
3456
3457namespace internal {
3458struct ClientImplDetails {
3459 static std::shared_ptr<RawClient> GetRawClient(Client& c) {
3460 return c.raw_client_;
3461 }
3462
3463 static StatusOr<ObjectMetadata> UploadStreamResumable(
3464 Client& client, std::istream& source,
3465 internal::ResumableUploadRequest const& request) {
3466 return client.UploadStreamResumable(source, request);
3467 }
3468
3469 template <typename... Policies>
3470 static Client CreateClient(std::shared_ptr<internal::RawClient> c,
3471 Policies&&... p) {
3472 auto opts =
3473 internal::ApplyPolicies(c->options(), std::forward<Policies>(p)...);
3474 return Client(Client::InternalOnlyNoDecorations{},
3475 Client::CreateDefaultInternalClient(opts, std::move(c)));
3476 }
3477
3478 static Client CreateWithoutDecorations(
3479 std::shared_ptr<internal::RawClient> c) {
3480 return Client(Client::InternalOnlyNoDecorations{}, std::move(c));
3481 }
3482};
3483
3484// Just a wrapper to allow for using in `google::cloud::internal::apply`.
3485struct DeleteApplyHelper {
3486 template <typename... Options>
3487 Status operator()(Options... options) const {
3488 return client.DeleteObject(bucket_name, object_name, Generation(generation),
3489 std::move(options)...);
3490 }
3491
3492 Client& client;
3493 std::string bucket_name;
3494 std::string object_name;
3495 std::int64_t generation;
3496};
3497
3498// Just a wrapper to allow for using in `google::cloud::internal::apply`.
3499struct InsertObjectApplyHelper {
3500 template <typename... Options>
3501 StatusOr<ObjectMetadata> operator()(Options... options) const {
3502 return client.InsertObject(bucket_name, object_name, std::move(contents),
3503 std::move(options)...);
3504 }
3505
3506 Client& client;
3507 std::string const& bucket_name;
3508 std::string const& object_name;
3509 std::string contents;
3510};
3511
3512/**
3513 * Create a "marker" object to ensure that two tasks cannot share a prefix.
3514 *
3515 * @param client the client on which to perform the operation.
3516 * @param bucket_name the name of the bucket that will contain the object.
3517 * @param prefix the prefix of the objects to be deleted.
3518 * @param options a list of optional query parameters and/or request headers.
3519 * Valid types for this operation include `EncryptionKey` `KmsKeyName`,
3520 * `PredefinedAcl`, `Projection`, `UserProject`, and `WithObjectMetadata`.
3521 * Contrary to the public API, invalid options will be silently ignored
3522 * for ease of use.
3523 * @return the metadata of the marker
3524 */
3525template <typename... Options>
3526StatusOr<ObjectMetadata> LockPrefix(Client& client,
3527 std::string const& bucket_name,
3528 std::string const& prefix,
3529 Options&&... options) {
3530 return google::cloud::internal::apply(
3531 internal::InsertObjectApplyHelper{client, bucket_name, prefix, ""},
3532 std::tuple_cat(
3533 std::make_tuple(IfGenerationMatch(0)),
3534 internal::StaticTupleFilter<
3535 internal::Among<EncryptionKey, KmsKeyName, PredefinedAcl,
3536 Projection, UserProject>::TPred>(
3537 std::forward_as_tuple(std::forward<Options>(options)...))));
3538}
3539
3540} // namespace internal
3541
3542/**
3543 * Delete objects whose names match a given prefix
3544 *
3545 * @param client the client on which to perform the operation.
3546 * @param bucket_name the name of the bucket that will contain the object.
3547 * @param prefix the prefix of the objects to be deleted.
3548 * @param options a list of optional query parameters and/or request headers.
3549 * Valid types for this operation include `QuotaUser`, `UserIp`,
3550 * `UserProject` and `Versions`.
3551 */
3552template <typename... Options>
3553Status DeleteByPrefix(Client& client, std::string const& bucket_name,
3554 std::string const& prefix, Options&&... options) {
3555 using internal::NotAmong;
3556 using internal::StaticTupleFilter;
3557
3558 auto all_options = std::tie(options...);
3559
3560 static_assert(
3561 std::tuple_size<
3562 decltype(StaticTupleFilter<
3563 NotAmong<QuotaUser, UserIp, UserProject, Versions>::TPred>(
3564 all_options))>::value == 0,
3565 "This functions accepts only options of type QuotaUser, UserIp, "
3566 "UserProject or Versions.");
3567 auto status = Status{};
3568 for (auto& object :
3569 client.ListObjects(bucket_name, Projection::NoAcl(), Prefix(prefix),
3570 std::forward<Options>(options)...)) {
3571 if (!object) return std::move(object).status();
3572 auto del = google::cloud::internal::apply(
3573 internal::DeleteApplyHelper{client, object->bucket(), object->name(),
3574 object->generation()},
3575 StaticTupleFilter<NotAmong<Versions>::TPred>(all_options));
3576 // We ignore kNotFound because we are trying to delete the object anyway.
3577 if (del.ok() || status.code() == StatusCode::kNotFound) continue;
3578 status = std::move(del);
3579 }
3580 return status;
3581}
3582
3583namespace internal {
3584
3585// Just a wrapper to allow for use in `google::cloud::internal::apply`.
3586struct ComposeApplyHelper {
3587 template <typename... Options>
3588 StatusOr<ObjectMetadata> operator()(Options... options) const {
3589 return client.ComposeObject(
3590 std::move(bucket_name), std::move(source_objects),
3591 std::move(destination_object_name), std::move(options)...);
3592 }
3593
3594 Client& client;
3595 std::string bucket_name;
3596 std::vector<ComposeSourceObject> source_objects;
3597 std::string destination_object_name;
3598};
3599
3600// A helper to defer deletion of temporary GCS objects.
3601class ScopedDeleter {
3602 public:
3603 // The actual deletion depends on local's types in a very non-trivial way,
3604 // so we abstract this away by providing the function to delete one object.
3605 explicit ScopedDeleter(std::function<Status(std::string, std::int64_t)> df);
3606 ScopedDeleter(ScopedDeleter const&) = delete;
3607 ScopedDeleter& operator=(ScopedDeleter const&) = delete;
3608 ~ScopedDeleter();
3609
3610 /// Defer object's deletion to this objects destruction (or ExecuteDelete())
3611 void Add(ObjectMetadata const& object);
3612
3613 /// Defer object's deletion to this objects destruction (or ExecuteDelete())
3614 void Add(std::string object_name, std::int64_t generation);
3615
3616 /// Execute all the deferred deletions now.
3617 Status ExecuteDelete();
3618
3619 void Enable(bool enable) { enabled_ = enable; }
3620
3621 private:
3622 bool enabled_ = true;
3623 std::function<Status(std::string, std::int64_t)> delete_fun_;
3624 std::vector<std::pair<std::string, std::int64_t>> object_list_;
3625};
3626
3627} // namespace internal
3628
3629/**
3630 * Compose existing objects into a new object in the same bucket.
3631 *
3632 * Contrary to `Client::ComposeObject`, this function doesn't have a limit on
3633 * the number of source objects.
3634 *
3635 * The implementation may need to perform multiple Client::ComposeObject calls
3636 * to create intermediate, temporary objects which are then further composed.
3637 * Due to the lack of atomicity of this series of operations, stray temporary
3638 * objects might be left over if there are transient failures. In order to allow
3639 * the user to easily control for such situations, the user is expected to
3640 * provide a unique @p prefix parameter, which will become the prefix of all the
3641 * temporary objects created by this function. Once this function finishes, the
3642 * user may safely remove all objects with the provided prefix (e.g. via
3643 * DeleteByPrefix()). We recommend using CreateRandomPrefixName() for selecting
3644 * a random prefix within a bucket.
3645 *
3646 * @param client the client on which to perform the operations needed by this
3647 * function
3648 * @param bucket_name the name of the bucket used for source object and
3649 * destination object.
3650 * @param source_objects objects used to compose `destination_object_name`.
3651 * @param destination_object_name the composed object name.
3652 * @param prefix prefix for temporary objects created by this function; there
3653 * should not be any objects with this prefix; in order to avoid race
3654 * conditions, this function will create an object with this name
3655 * @param ignore_cleanup_failures if the composition succeeds but cleanup of
3656 * temporary objects fails, depending on this parameter either a success
3657 * will be returned (`true`) or the relevant cleanup error (`false`)
3658 * @param options a list of optional query parameters and/or request headers.
3659 * Valid types for this operation include `DestinationPredefinedAcl`,
3660 * `EncryptionKey`, `IfGenerationMatch`, `IfMetagenerationMatch`
3661 * `KmsKeyName`, `QuotaUser`, `UserIp`, `UserProject` and
3662 * `WithObjectMetadata`.
3663 *
3664 * @par Idempotency
3665 * This operation is not idempotent. While each request performed by this
3666 * function is retried based on the client policies, the operation itself stops
3667 * on the first request that fails.
3668 *
3669 * @par Example
3670 * @snippet storage_object_samples.cc compose object from many
3671 */
3672template <typename... Options>
3673StatusOr<ObjectMetadata> ComposeMany(
3674 Client& client, std::string const& bucket_name,
3675 std::vector<ComposeSourceObject> source_objects, std::string const& prefix,
3676 std::string destination_object_name, bool ignore_cleanup_failures,
3677 Options&&... options) {
3678 using internal::Among;
3679 using internal::NotAmong;
3680 using internal::StaticTupleFilter;
3681 std::size_t const max_num_objects = 32;
3682
3683 if (source_objects.empty()) {
3684 return Status(StatusCode::kInvalidArgument,
3685 "ComposeMany requires at least one source object.");
3686 }
3687
3688 auto all_options = std::make_tuple(options...);
3689
3690 // TODO(#3247): this list of type should somehow be generated
3691 static_assert(
3692 std::tuple_size<
3693 decltype(StaticTupleFilter<
3697 WithObjectMetadata>::TPred>(all_options))>::value ==
3698 0,
3699 "This functions accepts only options of type DestinationPredefinedAcl, "
3700 "EncryptionKey, IfGenerationMatch, IfMetagenerationMatch, KmsKeyName, "
3701 "QuotaUser, UserIp, UserProject or WithObjectMetadata.");
3702
3703 internal::ScopedDeleter deleter(
3704 [&](std::string const& object_name, std::int64_t generation) {
3705 return google::cloud::internal::apply(
3706 internal::DeleteApplyHelper{client, bucket_name, object_name,
3707 generation},
3708 StaticTupleFilter<Among<QuotaUser, UserProject, UserIp>::TPred>(
3709 all_options));
3710 });
3711
3712 auto lock = internal::LockPrefix(client, bucket_name, prefix, "",
3713 std::make_tuple(options...));
3714 if (!lock) {
3715 return Status(
3716 lock.status().code(),
3717 "Failed to lock prefix for ComposeMany: " + lock.status().message());
3718 }
3719 deleter.Add(*lock);
3720
3721 std::size_t num_tmp_objects = 0;
3722 auto tmpobject_name_gen = [&num_tmp_objects, &prefix] {
3723 return prefix + ".compose-tmp-" + std::to_string(num_tmp_objects++);
3724 };
3725
3726 auto to_source_objects = [](std::vector<ObjectMetadata> objects) {
3727 std::vector<ComposeSourceObject> sources(objects.size());
3728 std::transform(objects.begin(), objects.end(), sources.begin(),
3729 [](ObjectMetadata const& m) {
3731 });
3732 return sources;
3733 };
3734
3735 auto composer = [&](std::vector<ComposeSourceObject> compose_range,
3736 bool is_final) -> StatusOr<ObjectMetadata> {
3737 if (is_final) {
3738 return google::cloud::internal::apply(
3739 internal::ComposeApplyHelper{client, bucket_name,
3740 std::move(compose_range),
3741 std::move(destination_object_name)},
3742 all_options);
3743 }
3744 return google::cloud::internal::apply(
3745 internal::ComposeApplyHelper{client, bucket_name,
3746 std::move(compose_range),
3747 tmpobject_name_gen()},
3748 StaticTupleFilter<
3749 NotAmong<IfGenerationMatch, IfMetagenerationMatch>::TPred>(
3750 all_options));
3751 };
3752
3753 auto reduce = [&](std::vector<ComposeSourceObject> source_objects)
3754 -> StatusOr<std::vector<ObjectMetadata>> {
3755 std::vector<ObjectMetadata> objects;
3756 for (auto range_begin = source_objects.begin();
3757 range_begin != source_objects.end();) {
3758 std::size_t range_size = std::min<std::size_t>(
3759 std::distance(range_begin, source_objects.end()), max_num_objects);
3760 auto range_end = std::next(range_begin, range_size);
3761 std::vector<ComposeSourceObject> compose_range(range_size);
3762 std::move(range_begin, range_end, compose_range.begin());
3763
3764 bool const is_final_composition =
3765 source_objects.size() <= max_num_objects;
3766 auto object = composer(std::move(compose_range), is_final_composition);
3767 if (!object) {
3768 return std::move(object).status();
3769 }
3770 objects.push_back(*std::move(object));
3771 if (!is_final_composition) {
3772 deleter.Add(objects.back());
3773 }
3774 range_begin = range_end;
3775 }
3776 return objects;
3777 };
3778
3779 StatusOr<ObjectMetadata> result;
3780 do {
3781 StatusOr<std::vector<ObjectMetadata>> objects = reduce(source_objects);
3782 if (!objects) {
3783 return objects.status();
3784 }
3785 if (objects->size() == 1) {
3786 if (!ignore_cleanup_failures) {
3787 auto delete_status = deleter.ExecuteDelete();
3788 if (!delete_status.ok()) {
3789 return delete_status;
3790 }
3791 }
3792 result = std::move((*objects)[0]);
3793 break;
3794 }
3795 source_objects = to_source_objects(*std::move(objects));
3796 } while (source_objects.size() > 1);
3797 return result;
3798}
3799
3800GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
3801} // namespace storage
3802} // namespace cloud
3803} // namespace google
3804
3805#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_CLIENT_H
Status && status() &&
Status const & status() const &
StatusCode code() const
Status(StatusCode code, std::string message, ErrorInfo info={})
Prepares a patch for a BucketAccessControl resource.
Definition: bucket_access_control.h:188
Wraps the bucketAccessControl resource in Google Cloud Storage.
Definition: bucket_access_control.h:39
std::string const & entity() const
Definition: bucket_access_control.h:75
std::string const & role() const
Definition: bucket_access_control.h:85
Prepares a patch for the Bucket resource.
Definition: bucket_metadata.h:614
Represents a Google Cloud Storage Bucket Metadata object.
Definition: bucket_metadata.h:53
BucketMetadata & set_name(std::string v)
Changes the name.
Definition: bucket_metadata.h:383
Describes the configuration for a storage::Client object.
Definition: client_options.h:107
std::shared_ptr< oauth2::Credentials > credentials() const
Definition: client_options.h:150
The Google Cloud Storage (GCS) Client.
Definition: client.h:263
StatusOr< NativeIamPolicy > SetNativeBucketIamPolicy(std::string const &bucket_name, NativeIamPolicy const &iam_policy, Options &&... options)
Sets the native IAM Policy for a Bucket.
Definition: client.h:731
StatusOr< ServiceAccount > GetServiceAccount(Options &&... options)
Gets the GCS service account for the default project.
Definition: client.h:2586
StatusOr< ObjectMetadata > UpdateObject(std::string bucket_name, std::string object_name, ObjectMetadata metadata, Options &&... options)
Updates the metadata in a Google Cloud Storage Object.
Definition: client.h:1354
StatusOr< std::vector< ObjectAccessControl > > ListDefaultObjectAcl(std::string const &bucket_name, Options &&... options)
Retrieves the default object ACL for a bucket as a vector of ObjectAccessControl items.
Definition: client.h:2251
StatusOr< ObjectMetadata > RewriteObjectBlocking(std::string source_bucket_name, std::string source_object_name, std::string destination_bucket_name, std::string destination_object_name, Options &&... options)
Rewrites the object, blocking until the rewrite completes, and returns the resulting ObjectMetadata.
Definition: client.h:1629
StatusOr< BucketMetadata > CreateBucket(std::string bucket_name, BucketMetadata metadata, Options &&... options)
Creates a new Google Cloud Storage bucket using the default project.
Definition: client.h:397
StatusOr< std::vector< ObjectAccessControl > > ListObjectAcl(std::string const &bucket_name, std::string const &object_name, Options &&... options)
Retrieves the list of ObjectAccessControl items for an object.
Definition: client.h:1963
StatusOr< BucketAccessControl > GetBucketAcl(std::string const &bucket_name, std::string const &entity, Options &&... options)
Gets the value of an existing bucket ACL.
Definition: client.h:1779
Client(std::shared_ptr< oauth2::Credentials > credentials, Policies &&... policies)
Creates the default client type given the credentials and policies.
Definition: client.h:3268
ListObjectsAndPrefixesReader ListObjectsAndPrefixes(std::string const &bucket_name, Options &&... options)
Lists the objects and prefixes in a bucket.
Definition: client.h:1045
friend bool operator!=(Client const &a, Client const &b)
Definition: client.h:288
StatusOr< ObjectAccessControl > UpdateObjectAcl(std::string const &bucket_name, std::string const &object_name, ObjectAccessControl const &acl, Options &&... options)
Updates the value of an existing object ACL.
Definition: client.h:2104
Status DownloadToFile(std::string const &bucket_name, std::string const &object_name, std::string const &file_name, Options &&... options)
Downloads a Cloud Storage object to a file.
Definition: client.h:1293
StatusOr< ObjectMetadata > InsertObject(std::string const &bucket_name, std::string const &object_name, char const *contents, Options &&... options)
Creates an object given its name and contents.
Definition: client.h:905
Client(ClientOptions options, Policies &&... policies)
Creates the default client type given the options.
Definition: client.h:3251
std::shared_ptr< internal::RawClient > raw_client() const
Access the underlying RawClient.
Definition: client.h:3326
StatusOr< std::string > CreateV2SignedUrl(std::string verb, std::string bucket_name, std::string object_name, Options &&... options)
Create a V2 signed URL for the given parameters.
Definition: client.h:2873
Client(Options opts={})
Build a new client.
StatusOr< ObjectAccessControl > UpdateDefaultObjectAcl(std::string const &bucket_name, ObjectAccessControl const &acl, Options &&... options)
Updates the value of an existing default object ACL.
Definition: client.h:2402
StatusOr< ObjectMetadata > InsertObject(std::string const &bucket_name, std::string const &object_name, std::string const &contents, Options &&... options)
Creates an object given its name and contents.
Definition: client.h:895
ListBucketsReader ListBucketsForProject(std::string const &project_id, Options &&... options)
Fetches the list of buckets for a given project.
Definition: client.h:319
static StatusOr< Client > CreateDefaultClient()
Create a Client using ClientOptions::CreateDefaultClientOptions().
StatusOr< BucketMetadata > GetBucketMetadata(std::string const &bucket_name, Options &&... options)
Fetches the bucket metadata.
Definition: client.h:468
StatusOr< std::vector< std::string > > TestBucketIamPermissions(std::string bucket_name, std::vector< std::string > permissions, Options &&... options)
Tests the IAM permissions of the caller against a Bucket.
Definition: client.h:769
StatusOr< BucketMetadata > PatchBucket(std::string bucket_name, BucketMetadataPatchBuilder const &builder, Options &&... options)
Patches the metadata in a Google Cloud Storage Bucket given a desired set changes.
Definition: client.h:631
StatusOr< BucketAccessControl > PatchBucketAcl(std::string const &bucket_name, std::string const &entity, BucketAccessControlPatchBuilder const &builder, Options &&... options)
Patches the value of an existing bucket ACL.
Definition: client.h:1913
StatusOr< std::string > CreateV4SignedUrl(std::string verb, std::string bucket_name, std::string object_name, Options &&... options)
Create a V4 signed URL for the given parameters.
Definition: client.h:2938
StatusOr< ObjectAccessControl > PatchObjectAcl(std::string const &bucket_name, std::string const &object_name, std::string const &entity, ObjectAccessControlPatchBuilder const &builder, Options &&... options)
Patches the value of an existing object ACL.
Definition: client.h:2200
StatusOr< ObjectAccessControl > PatchObjectAcl(std::string const &bucket_name, std::string const &object_name, std::string const &entity, ObjectAccessControl const &original_acl, ObjectAccessControl const &new_acl, Options &&... options)
Patches the value of an existing object ACL.
Definition: client.h:2153
StatusOr< ObjectAccessControl > PatchDefaultObjectAcl(std::string const &bucket_name, std::string const &entity, ObjectAccessControlPatchBuilder const &builder, Options &&... options)
Patches the value of an existing default object ACL.
Definition: client.h:2501
StatusOr< ObjectMetadata > PatchObject(std::string bucket_name, std::string object_name, ObjectMetadata const &original, ObjectMetadata const &updated, Options &&... options)
Patches the metadata in a Google Cloud Storage Object.
Definition: client.h:1393
Status DeleteObject(std::string const &bucket_name, std::string const &object_name, Options &&... options)
Deletes an object.
Definition: client.h:1322
StatusOr< BucketMetadata > UpdateBucket(std::string bucket_name, BucketMetadata metadata, Options &&... options)
Updates the metadata in a Google Cloud Storage Bucket.
Definition: client.h:535
StatusOr< std::pair< HmacKeyMetadata, std::string > > CreateHmacKey(std::string service_account, Options &&... options)
Create a new HMAC key.
Definition: client.h:2678
StatusOr< std::vector< NotificationMetadata > > ListNotifications(std::string const &bucket_name, Options &&... options)
Retrieves the list of Notifications for a Bucket.
Definition: client.h:3064
StatusOr< ObjectMetadata > UploadFile(std::string const &file_name, std::string const &bucket_name, std::string const &object_name, Options &&... options)
Uploads a file to an object.
Definition: client.h:1236
StatusOr< NotificationMetadata > CreateNotification(std::string const &bucket_name, std::string const &topic_name, std::string const &payload_format, NotificationMetadata metadata, Options &&... options)
Creates a new notification config for a Bucket.
Definition: client.h:3151
StatusOr< ObjectAccessControl > GetObjectAcl(std::string const &bucket_name, std::string const &object_name, std::string const &entity, Options &&... options)
Gets the value of an existing object ACL.
Definition: client.h:2063
StatusOr< ObjectAccessControl > PatchDefaultObjectAcl(std::string const &bucket_name, std::string const &entity, ObjectAccessControl const &original_acl, ObjectAccessControl const &new_acl, Options &&... options)
Patches the value of an existing default object ACL.
Definition: client.h:2452
StatusOr< BucketAccessControl > CreateBucketAcl(std::string const &bucket_name, std::string const &entity, std::string const &role, Options &&... options)
Creates a new entry in a bucket ACL.
Definition: client.h:1722
Client(std::shared_ptr< internal::RawClient > client, NoDecorations)
Builds a client with a specific RawClient, without decorations.
Definition: client.h:3317
ObjectRewriter RewriteObject(std::string source_bucket_name, std::string source_object_name, std::string destination_bucket_name, std::string destination_object_name, Options &&... options)
Creates an ObjectRewriter to copy the source object.
Definition: client.h:1517
StatusOr< NotificationMetadata > GetNotification(std::string const &bucket_name, std::string const &notification_id, Options &&... options)
Gets the details about a notification config in a given Bucket.
Definition: client.h:3189
ObjectRewriter ResumeRewriteObject(std::string source_bucket_name, std::string source_object_name, std::string destination_bucket_name, std::string destination_object_name, std::string rewrite_token, Options &&... options)
Creates an ObjectRewriter to resume a previously created rewrite.
Definition: client.h:1569
Status DeleteBucketAcl(std::string const &bucket_name, std::string const &entity, Options &&... options)
Deletes an entry from a bucket ACL.
Definition: client.h:1752
ObjectWriteStream WriteObject(std::string const &bucket_name, std::string const &object_name, Options &&... options)
Writes contents into an object.
Definition: client.h:1195
ObjectReadStream ReadObject(std::string const &bucket_name, std::string const &object_name, Options &&... options)
Reads the contents of an object.
Definition: client.h:1107
StatusOr< ObjectMetadata > ComposeObject(std::string bucket_name, std::vector< ComposeSourceObject > source_objects, std::string destination_object_name, Options &&... options)
Composes existing objects into a new object in the same bucket.
Definition: client.h:1466
StatusOr< ObjectAccessControl > GetDefaultObjectAcl(std::string const &bucket_name, std::string const &entity, Options &&... options)
Gets the value of a default object ACL in a bucket.
Definition: client.h:2358
Client(std::shared_ptr< internal::RawClient > client, Policies &&... policies)
Builds a client and maybe override the retry, idempotency, and/or backoff policies.
Definition: client.h:3296
StatusOr< NativeIamPolicy > GetNativeBucketIamPolicy(std::string const &bucket_name, Options &&... options)
Fetches the native IAM policy for a Bucket.
Definition: client.h:675
StatusOr< std::vector< BucketAccessControl > > ListBucketAcl(std::string const &bucket_name, Options &&... options)
Retrieves the list of BucketAccessControl items for a bucket.
Definition: client.h:1687
StatusOr< ObjectMetadata > PatchObject(std::string bucket_name, std::string object_name, ObjectMetadataPatchBuilder const &builder, Options &&... options)
Patches the metadata in a Google Cloud Storage Object.
Definition: client.h:1431
Status DeleteNotification(std::string const &bucket_name, std::string const &notification_id, Options &&... options)
Delete an existing notification config in a given Bucket.
Definition: client.h:3227
StatusOr< HmacKeyMetadata > GetHmacKey(std::string access_id, Options &&... options)
Get an existing HMAC key in a given project.
Definition: client.h:2759
friend bool operator==(Client const &a, Client const &b)
Definition: client.h:285
StatusOr< ObjectMetadata > CopyObject(std::string source_bucket_name, std::string source_object_name, std::string destination_bucket_name, std::string destination_object_name, Options &&... options)
Copies an existing object.
Definition: client.h:956
StatusOr< ObjectAccessControl > CreateDefaultObjectAcl(std::string const &bucket_name, std::string const &entity, std::string const &role, Options &&... options)
Creates a new entry in the default object ACL for a bucket.
Definition: client.h:2291
Status DeleteBucket(std::string const &bucket_name, Options &&... options)
Deletes a Google Cloud Storage Bucket.
Definition: client.h:493
ListBucketsReader ListBuckets(Options &&... options)
Fetches the list of buckets for the default project.
Definition: client.h:355
StatusOr< BucketMetadata > CreateBucketForProject(std::string bucket_name, std::string project_id, BucketMetadata metadata, Options &&... options)
Creates a new Google Cloud Storage Bucket in a given project.
Definition: client.h:440
StatusOr< NotificationMetadata > CreateNotification(std::string const &bucket_name, std::string const &topic_name, NotificationMetadata metadata, Options &&... options)
Creates a new notification config for a Bucket.
Definition: client.h:3107
StatusOr< ObjectAccessControl > CreateObjectAcl(std::string const &bucket_name, std::string const &object_name, std::string const &entity, std::string const &role, Options &&... options)
Creates a new entry in the object ACL.
Definition: client.h:2000
StatusOr< BucketAccessControl > PatchBucketAcl(std::string const &bucket_name, std::string const &entity, BucketAccessControl const &original_acl, BucketAccessControl const &new_acl, Options &&... options)
Patches the value of an existing bucket ACL.
Definition: client.h:1867
ListObjectsReader ListObjects(std::string const &bucket_name, Options &&... options)
Lists the objects in a bucket.
Definition: client.h:1013
Status DeleteDefaultObjectAcl(std::string const &bucket_name, std::string const &entity, Options &&... options)
Deletes an entry from the default object ACL in a bucket.
Definition: client.h:2325
StatusOr< BucketMetadata > PatchBucket(std::string bucket_name, BucketMetadata const &original, BucketMetadata const &updated, Options &&... options)
Computes the difference between two BucketMetadata objects and patches a bucket based on that differe...
Definition: client.h:585
StatusOr< ObjectMetadata > InsertObject(std::string const &bucket_name, std::string const &object_name, absl::string_view contents, Options &&... options)
Creates an object given its name and contents.
Definition: client.h:881
StatusOr< ObjectMetadata > GetObjectMetadata(std::string const &bucket_name, std::string const &object_name, Options &&... options)
Fetches the object metadata.
Definition: client.h:987
Status DeleteResumableUpload(std::string const &upload_session_url, Options &&... options)
Cancel a resumable upload.
Definition: client.h:1264
StatusOr< PolicyDocumentV4Result > GenerateSignedPostPolicyV4(PolicyDocumentV4 document, Options &&... options)
Create a signed V4 policy document.
Definition: client.h:3027
StatusOr< ServiceAccount > GetServiceAccountForProject(std::string const &project_id, Options &&... options)
Gets the GCS service account for a given project.
Definition: client.h:2548
StatusOr< BucketAccessControl > UpdateBucketAcl(std::string const &bucket_name, BucketAccessControl const &acl, Options &&... options)
Updates the value of an existing bucket ACL.
Definition: client.h:1820
StatusOr< PolicyDocumentResult > CreateSignedPolicyDocument(PolicyDocument document, Options &&... options)
Create a signed policy document.
Definition: client.h:2984
StatusOr< BucketMetadata > LockBucketRetentionPolicy(std::string const &bucket_name, std::uint64_t metageneration, Options &&... options)
Locks the retention policy for a bucket.
Definition: client.h:831
Status DeleteHmacKey(std::string access_id, Options &&... options)
Delete a HMAC key in a given project.
Definition: client.h:2721
Status DeleteObjectAcl(std::string const &bucket_name, std::string const &object_name, std::string const &entity, Options &&... options)
Deletes one access control entry in one object.
Definition: client.h:2034
StatusOr< HmacKeyMetadata > UpdateHmacKey(std::string access_id, HmacKeyMetadata resource, Options &&... options)
Update an existing HMAC key in a given project.
Definition: client.h:2802
ListHmacKeysReader ListHmacKeys(Options &&... options)
List the available HMAC keys.
Definition: client.h:2627
Represents the metadata for a Google Cloud Storage HmacKeyResource.
Definition: hmac_key_metadata.h:45
Represent the result of a GetIamPolicy or SetIamPolicy request.
Definition: iam_policy.h:150
Represents the metadata for a Google Cloud Storage Notification resource.
Definition: notification_metadata.h:43
NotificationMetadata & set_payload_format(std::string v)
Definition: notification_metadata.h:131
NotificationMetadata & set_topic(std::string v)
Definition: notification_metadata.h:139
Prepares a patch for an ObjectAccessControl resource.
Definition: object_access_control.h:200
Wraps the objectAccessControl resource in Google Cloud Storage.
Definition: object_access_control.h:39
std::string const & entity() const
Definition: object_access_control.h:77
std::string const & role() const
Definition: object_access_control.h:87
Prepares a patch for the Bucket resource.
Definition: object_metadata.h:564
Represents the metadata for a Google Cloud Storage Object.
Definition: object_metadata.h:94
std::string const & name() const
The object name, including bucket and generation.
Definition: object_metadata.h:386
std::int64_t generation() const
The object generation.
Definition: object_metadata.h:259
Defines a std::basic_istream<char> to read from a GCS Object.
Definition: object_read_stream.h:33
Complete long running object rewrite operations.
Definition: object_rewriter.h:49
ObjectRewriter(std::shared_ptr< internal::RawClient > client, internal::RewriteObjectRequest request)
Defines a std::basic_ostream<char> to write to a GCS object.
Definition: object_write_stream.h:131
Represents the metadata for a Google Cloud Storage service account.
Definition: service_account.h:30
Interface for OAuth 2.0 credentials used to access Google Cloud services.
Definition: credentials.h:47
Authentication components for Google Cloud Storage.
Definition: anonymous_credentials.h:26
Contains functions to return well-known notification payload format names.
Definition: notification_payload_format.h:25
char const * JsonApiV1()
Definition: notification_payload_format.h:26
Contains all the Google Cloud Storage C++ client APIs.
Definition: auto_finalize.h:24
Status DeleteByPrefix(Client &client, std::string const &bucket_name, std::string const &prefix, Options &&... options)
Delete objects whose names match a given prefix.
Definition: client.h:3553
std::string CreateRandomPrefixName(std::string const &prefix="")
Create a random prefix for object names.
StatusOr< ObjectMetadata > ComposeMany(Client &client, std::string const &bucket_name, std::vector< ComposeSourceObject > source_objects, std::string const &prefix, std::string destination_object_name, bool ignore_cleanup_failures, Options &&... options)
Compose existing objects into a new object in the same bucket.
Definition: client.h:3673
Define a tag to disable automatic decorations of the RawClient.
Definition: client.h:3308
Defines one of the source objects for a compose operation.
Definition: object_metadata.h:44
Set the ACL to a predefined value when copying Objects.
Definition: well_known_parameters.h:357
An optional parameter to set the Customer-Supplied Encryption key.
Definition: well_known_headers.h:183
Set the version of an object to operate on.
Definition: well_known_parameters.h:145
A pre-condition: the request succeeds only if the object generation matches.
Definition: well_known_parameters.h:157
A pre-condition: the request succeeds if the metadata generation matches.
Definition: well_known_parameters.h:181
Configure the Customer-Managed Encryption Key (CMEK) for an upload.
Definition: well_known_parameters.h:274
Define a policy document result.
Definition: policy_document.h:176
Define a policy document result V4.
Definition: policy_document.h:192
Define a policy document V4.
Definition: policy_document.h:147
Define a policy document.
Definition: policy_document.h:130
Set the ACL to predefined values when creating Buckets or Objects.
Definition: well_known_parameters.h:320
Restrict list operations to entries starting with this value.
Definition: well_known_parameters.h:436
Controls what metadata fields are included in the response.
Definition: well_known_parameters.h:507
static Projection NoAcl()
Definition: well_known_parameters.h:511
Sets the user for this operation for quota enforcement purposes.
Definition: well_known_parameters.h:524
Download all the data from the GCS object starting at the given offset.
Definition: download_options.h:54
Read last N bytes from the GCS object.
Definition: download_options.h:65
Request only a portion of the GCS object in a ReadObject operation.
Definition: download_options.h:38
Specify the service account used to sign a blob.
Definition: signed_url_options.h:186
Request a resumable upload, restoring a previous session if necessary.
Definition: upload_options.h:38
Sets the user IP on an operation for quota enforcement purposes.
Definition: user_ip_option.h:43
Set the project used for billing in "requester pays" Buckets.
Definition: well_known_parameters.h:573
Control if all versions of an object should be included when listing objects.
Definition: well_known_parameters.h:587
A request option to define the object metadata attributes.
Definition: object_metadata.h:622