-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathobject_requests.h
613 lines (523 loc) · 22.5 KB
/
object_requests.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_OBJECT_REQUESTS_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_OBJECT_REQUESTS_H
#include "google/cloud/storage/auto_finalize.h"
#include "google/cloud/storage/download_options.h"
#include "google/cloud/storage/hashing_options.h"
#include "google/cloud/storage/include_folders_as_prefixes.h"
#include "google/cloud/storage/internal/const_buffer.h"
#include "google/cloud/storage/internal/generic_object_request.h"
#include "google/cloud/storage/internal/hash_function.h"
#include "google/cloud/storage/internal/hash_values.h"
#include "google/cloud/storage/internal/http_response.h"
#include "google/cloud/storage/object_metadata.h"
#include "google/cloud/storage/override_unlocked_retention.h"
#include "google/cloud/storage/soft_deleted.h"
#include "google/cloud/storage/upload_options.h"
#include "google/cloud/storage/version.h"
#include "google/cloud/storage/well_known_parameters.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
#include <map>
#include <memory>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace internal {
/**
* Represents a request to the `Objects: list` API.
*/
class ListObjectsRequest
: public GenericRequest<ListObjectsRequest, MaxResults, Prefix, Delimiter,
IncludeFoldersAsPrefixes, IncludeTrailingDelimiter,
StartOffset, EndOffset, MatchGlob, Projection,
SoftDeleted, UserProject, Versions> {
public:
ListObjectsRequest() = default;
explicit ListObjectsRequest(std::string bucket_name)
: bucket_name_(std::move(bucket_name)) {}
std::string const& bucket_name() const { return bucket_name_; }
std::string const& page_token() const { return page_token_; }
ListObjectsRequest& set_page_token(std::string page_token) {
page_token_ = std::move(page_token);
return *this;
}
private:
std::string bucket_name_;
std::string page_token_;
};
std::ostream& operator<<(std::ostream& os, ListObjectsRequest const& r);
struct ListObjectsResponse {
static StatusOr<ListObjectsResponse> FromHttpResponse(
std::string const& payload);
static StatusOr<ListObjectsResponse> FromHttpResponse(
HttpResponse const& response);
std::string next_page_token;
std::vector<ObjectMetadata> items;
std::vector<std::string> prefixes;
};
std::ostream& operator<<(std::ostream& os, ListObjectsResponse const& r);
/**
* Represents a request to the `Objects: get` API.
*/
class GetObjectMetadataRequest
: public GenericObjectRequest<
GetObjectMetadataRequest, Generation, IfGenerationMatch,
IfGenerationNotMatch, IfMetagenerationMatch, IfMetagenerationNotMatch,
Projection, SoftDeleted, UserProject> {
public:
using GenericObjectRequest::GenericObjectRequest;
};
std::ostream& operator<<(std::ostream& os, GetObjectMetadataRequest const& r);
/**
* Refactors common attributes for `InsertObject*` requests.
*/
template <typename Derived>
using InsertObjectRequestImpl = GenericObjectRequest<
Derived, ContentEncoding, ContentType, Crc32cChecksumValue,
DisableCrc32cChecksum, DisableMD5Hash, EncryptionKey, IfGenerationMatch,
IfGenerationNotMatch, IfMetagenerationMatch, IfMetagenerationNotMatch,
KmsKeyName, MD5HashValue, PredefinedAcl, Projection, UserProject,
UploadFromOffset, UploadLimit, WithObjectMetadata>;
/**
* Represents a request to the `Objects: insert` API with a string for the
* media.
*
* This request type is used to upload objects with media that completely
* fits in memory. Such requests are simpler than uploading objects streaming
* objects.
*/
class InsertObjectMediaRequest
: public InsertObjectRequestImpl<InsertObjectMediaRequest> {
public:
InsertObjectMediaRequest();
InsertObjectMediaRequest(std::string bucket_name, std::string object_name,
absl::string_view payload);
absl::string_view payload() const { return payload_; }
void set_payload(absl::string_view payload);
template <typename... O>
InsertObjectMediaRequest& set_multiple_options(O&&... o) {
InsertObjectRequestImpl<InsertObjectMediaRequest>::set_multiple_options(
std::forward<O>(o)...);
reset_hash_function();
return *this;
}
HashFunction& hash_function() const { return *hash_function_; }
///@{
/**
* @name Backwards compatibility.
*
* While this class is in the internal namespace, the storage library
* requires applications to use parts of the internal namespace in mocks.
*
* These functions are only provided for backwards compatibility. The library
* no longer uses them, and mocks (if any) should migrate to payload() and
* set_payload().
*/
[[deprecated("use payload() instead")]] std::string const& contents() const;
[[deprecated("use set_payload() instead")]] void set_contents(std::string v);
///@}
private:
void reset_hash_function();
absl::string_view payload_;
std::shared_ptr<HashFunction> hash_function_;
mutable std::string contents_;
mutable bool dirty_ = true;
};
HashValues FinishHashes(InsertObjectMediaRequest const& request);
std::ostream& operator<<(std::ostream& os, InsertObjectMediaRequest const& r);
/**
* Represents a request to the `Objects: copy` API.
*/
class CopyObjectRequest
: public GenericRequest<
CopyObjectRequest, DestinationKmsKeyName, DestinationPredefinedAcl,
EncryptionKey, IfGenerationMatch, IfGenerationNotMatch,
IfMetagenerationMatch, IfMetagenerationNotMatch,
IfSourceGenerationMatch, IfSourceGenerationNotMatch,
IfSourceMetagenerationMatch, IfSourceMetagenerationNotMatch,
Projection, SourceGeneration, SourceEncryptionKey, UserProject,
WithObjectMetadata> {
public:
CopyObjectRequest() = default;
CopyObjectRequest(std::string source_bucket, std::string source_object,
std::string destination_bucket,
std::string destination_object)
: source_bucket_(std::move(source_bucket)),
source_object_(std::move(source_object)),
destination_bucket_(std::move(destination_bucket)),
destination_object_(std::move(destination_object)) {}
std::string const& source_bucket() const { return source_bucket_; }
std::string const& source_object() const { return source_object_; }
std::string const& destination_bucket() const { return destination_bucket_; }
std::string const& destination_object() const { return destination_object_; }
private:
std::string source_bucket_;
std::string source_object_;
std::string destination_bucket_;
std::string destination_object_;
};
std::ostream& operator<<(std::ostream& os, CopyObjectRequest const& r);
/**
* Represents a request to the `Objects: get` API with `alt=media`.
*/
class ReadObjectRangeRequest
: public GenericObjectRequest<
ReadObjectRangeRequest, DisableCrc32cChecksum, DisableMD5Hash,
EncryptionKey, Generation, IfGenerationMatch, IfGenerationNotMatch,
IfMetagenerationMatch, IfMetagenerationNotMatch, ReadFromOffset,
ReadRange, ReadLast, UserProject, AcceptEncoding> {
public:
using GenericObjectRequest::GenericObjectRequest;
bool RequiresNoCache() const;
bool RequiresRangeHeader() const;
std::string RangeHeader() const;
std::string RangeHeaderValue() const;
std::int64_t StartingByte() const;
};
std::ostream& operator<<(std::ostream& os, ReadObjectRangeRequest const& r);
/**
* Represents a request to the `Objects: delete` API.
*/
class DeleteObjectRequest
: public GenericObjectRequest<DeleteObjectRequest, Generation,
IfGenerationMatch, IfGenerationNotMatch,
IfMetagenerationMatch,
IfMetagenerationNotMatch, UserProject> {
public:
using GenericObjectRequest::GenericObjectRequest;
};
std::ostream& operator<<(std::ostream& os, DeleteObjectRequest const& r);
/**
* Represents a request to the `Objects: update` API.
*/
class UpdateObjectRequest
: public GenericObjectRequest<
UpdateObjectRequest, Generation, EncryptionKey, IfGenerationMatch,
IfGenerationNotMatch, IfMetagenerationMatch, IfMetagenerationNotMatch,
OverrideUnlockedRetention, PredefinedAcl, Projection, UserProject> {
public:
UpdateObjectRequest() = default;
explicit UpdateObjectRequest(std::string bucket_name, std::string object_name,
ObjectMetadata metadata)
: GenericObjectRequest(std::move(bucket_name), std::move(object_name)),
metadata_(std::move(metadata)) {}
/// Returns the request as the JSON API payload.
std::string json_payload() const;
ObjectMetadata const& metadata() const { return metadata_; }
private:
ObjectMetadata metadata_;
};
std::ostream& operator<<(std::ostream& os, UpdateObjectRequest const& r);
/**
* Represents a request to the `Objects: compose` API.
*/
class ComposeObjectRequest
: public GenericObjectRequest<ComposeObjectRequest, EncryptionKey,
DestinationPredefinedAcl, KmsKeyName,
IfGenerationMatch, IfMetagenerationMatch,
UserProject, WithObjectMetadata> {
public:
ComposeObjectRequest() = default;
explicit ComposeObjectRequest(std::string bucket_name,
std::vector<ComposeSourceObject> source_objects,
std::string destination_object_name);
std::vector<ComposeSourceObject> source_objects() const {
return source_objects_;
}
/// Returns the request as the JSON API payload.
std::string JsonPayload() const;
private:
std::vector<ComposeSourceObject> source_objects_;
};
std::ostream& operator<<(std::ostream& os, ComposeObjectRequest const& r);
/**
* Represents a request to the `Objects: patch` API.
*/
class PatchObjectRequest
: public GenericObjectRequest<
PatchObjectRequest, Generation, IfGenerationMatch,
IfGenerationNotMatch, IfMetagenerationMatch, IfMetagenerationNotMatch,
OverrideUnlockedRetention, PredefinedAcl, EncryptionKey, Projection,
UserProject,
// PredefinedDefaultObjectAcl has no effect in an `Objects: patch`
// request. We are keeping it here for backwards compatibility. It
// was introduced in error (should have been PredefinedAcl), and it
// was never documented. The cost of keeping it is small, and there
// is very little motivation to remove it.
PredefinedDefaultObjectAcl> {
public:
PatchObjectRequest() = default;
explicit PatchObjectRequest(std::string bucket_name, std::string object_name,
ObjectMetadata const& original,
ObjectMetadata const& updated);
explicit PatchObjectRequest(std::string bucket_name, std::string object_name,
ObjectMetadataPatchBuilder patch);
ObjectMetadataPatchBuilder const& patch() const { return patch_; }
std::string payload() const { return patch_.BuildPatch(); }
private:
ObjectMetadataPatchBuilder patch_;
};
std::ostream& operator<<(std::ostream& os, PatchObjectRequest const& r);
/**
* Represents a request to the `Objects: rewrite` API.
*/
class RewriteObjectRequest
: public GenericRequest<
RewriteObjectRequest, DestinationKmsKeyName, DestinationPredefinedAcl,
EncryptionKey, IfGenerationMatch, IfGenerationNotMatch,
IfMetagenerationMatch, IfMetagenerationNotMatch,
IfSourceGenerationMatch, IfSourceGenerationNotMatch,
IfSourceMetagenerationMatch, IfSourceMetagenerationNotMatch,
MaxBytesRewrittenPerCall, Projection, SourceEncryptionKey,
SourceGeneration, UserProject, WithObjectMetadata> {
public:
RewriteObjectRequest() = default;
RewriteObjectRequest(std::string source_bucket, std::string source_object,
std::string destination_bucket,
std::string destination_object,
std::string rewrite_token)
: source_bucket_(std::move(source_bucket)),
source_object_(std::move(source_object)),
destination_bucket_(std::move(destination_bucket)),
destination_object_(std::move(destination_object)),
rewrite_token_(std::move(rewrite_token)) {}
std::string const& source_bucket() const { return source_bucket_; }
std::string const& source_object() const { return source_object_; }
std::string const& destination_bucket() const { return destination_bucket_; }
std::string const& destination_object() const { return destination_object_; }
std::string const& rewrite_token() const { return rewrite_token_; }
void set_rewrite_token(std::string v) { rewrite_token_ = std::move(v); }
private:
std::string source_bucket_;
std::string source_object_;
std::string destination_bucket_;
std::string destination_object_;
std::string rewrite_token_;
};
std::ostream& operator<<(std::ostream& os, RewriteObjectRequest const& r);
/// Holds an `Objects: rewrite` response.
struct RewriteObjectResponse {
static StatusOr<RewriteObjectResponse> FromHttpResponse(
std::string const& payload);
static StatusOr<RewriteObjectResponse> FromHttpResponse(
HttpResponse const& response);
std::uint64_t total_bytes_rewritten;
std::uint64_t object_size;
bool done;
std::string rewrite_token;
ObjectMetadata resource;
};
std::ostream& operator<<(std::ostream& os, RewriteObjectResponse const& r);
/**
* Represents a request to the `Objects: restore` API
*/
class RestoreObjectRequest
: public GenericObjectRequest<
RestoreObjectRequest, Generation, CopySourceAcl, EncryptionKey,
IfGenerationMatch, IfGenerationNotMatch, IfMetagenerationMatch,
IfMetagenerationNotMatch, Projection, UserProject> {
public:
RestoreObjectRequest() = default;
explicit RestoreObjectRequest(std::string bucket_name,
std::string object_name,
std::int64_t generation)
: bucket_name_(std::move(bucket_name)),
object_name_(std::move(object_name)),
generation_(std::move(generation)) {}
std::string const& bucket_name() const { return bucket_name_; }
std::string const& object_name() const { return object_name_; }
std::int64_t const& generation() const { return generation_; }
private:
std::string bucket_name_;
std::string object_name_;
std::int64_t generation_;
};
std::ostream& operator<<(std::ostream& os, RestoreObjectRequest const& r);
/**
* Represents a request to start a resumable upload in `Objects: insert`.
*
* This request type is used to start resumable uploads. A resumable upload is
* started with a `Objects: insert` request with the `uploadType=resumable`
* query parameter. The payload for the initial request includes the (optional)
* object metadata. The response includes a URL to send requests that upload
* the media.
*/
class ResumableUploadRequest
: public GenericObjectRequest<
ResumableUploadRequest, ContentEncoding, ContentType,
Crc32cChecksumValue, DisableCrc32cChecksum, DisableMD5Hash,
EncryptionKey, IfGenerationMatch, IfGenerationNotMatch,
IfMetagenerationMatch, IfMetagenerationNotMatch, KmsKeyName,
MD5HashValue, PredefinedAcl, Projection, UseResumableUploadSession,
UserProject, UploadFromOffset, UploadLimit, WithObjectMetadata,
UploadContentLength, AutoFinalize, UploadBufferSize> {
public:
ResumableUploadRequest() = default;
ResumableUploadRequest(std::string bucket_name, std::string object_name)
: GenericObjectRequest(std::move(bucket_name), std::move(object_name)) {}
};
std::ostream& operator<<(std::ostream& os, ResumableUploadRequest const& r);
struct CreateResumableUploadResponse {
static StatusOr<CreateResumableUploadResponse> FromHttpResponse(
HttpResponse response);
std::string upload_id;
};
bool operator==(CreateResumableUploadResponse const& lhs,
CreateResumableUploadResponse const& rhs);
inline bool operator!=(CreateResumableUploadResponse const& lhs,
CreateResumableUploadResponse const& rhs) {
return !(lhs == rhs);
}
std::ostream& operator<<(std::ostream& os,
CreateResumableUploadResponse const& r);
/**
* A request to cancel a resumable upload.
*/
class DeleteResumableUploadRequest
: public GenericRequest<DeleteResumableUploadRequest, UserProject> {
public:
DeleteResumableUploadRequest() = default;
explicit DeleteResumableUploadRequest(std::string upload_session_url)
: upload_session_url_(std::move(upload_session_url)) {}
std::string const& upload_session_url() const { return upload_session_url_; }
private:
std::string upload_session_url_;
};
std::ostream& operator<<(std::ostream& os,
DeleteResumableUploadRequest const& r);
/**
* A request to send one chunk in an upload session.
*/
class UploadChunkRequest
: public GenericRequest<UploadChunkRequest, UserProject> {
public:
UploadChunkRequest() = default;
// A non-final chunk.
UploadChunkRequest(std::string upload_session_url, std::uint64_t offset,
ConstBufferSequence payload,
std::shared_ptr<HashFunction> hash_function);
// A chunk that finalizes the upload.
UploadChunkRequest(std::string upload_session_url, std::uint64_t offset,
ConstBufferSequence payload,
std::shared_ptr<HashFunction> hash_function,
HashValues known_hashes);
std::string const& upload_session_url() const { return upload_session_url_; }
std::uint64_t offset() const { return offset_; }
absl::optional<std::uint64_t> upload_size() const { return upload_size_; }
ConstBufferSequence const& payload() const { return payload_; }
[[deprecated("use known_hashes() and hash_function()")]] HashValues const&
full_object_hashes() const {
return known_object_hashes_;
}
HashValues const& known_object_hashes() const { return known_object_hashes_; }
HashFunction& hash_function() const { return *hash_function_; }
bool last_chunk() const { return upload_size_.has_value(); }
std::size_t payload_size() const { return TotalBytes(payload_); }
std::string RangeHeader() const;
std::string RangeHeaderValue() const;
/**
* Returns the request to continue writing at @p new_offset.
*
* @note the result of calling this with an out of range value is undefined
* behavior.
*/
UploadChunkRequest RemainingChunk(std::uint64_t new_offset) const;
// Chunks must be multiples of 256 KiB:
// https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload
static constexpr std::size_t kChunkSizeQuantum = 256 * 1024UL;
static std::size_t RoundUpToQuantum(std::size_t max_chunk_size) {
// If you are tempted to use bit manipulation to do this, modern compilers
// known how to optimize this (coryan tested this at godbolt.org):
// https://godbolt.org/z/xxUsjg
if (max_chunk_size % kChunkSizeQuantum == 0) {
return max_chunk_size;
}
auto n = max_chunk_size / kChunkSizeQuantum;
return (n + 1) * kChunkSizeQuantum;
}
private:
std::string upload_session_url_;
std::uint64_t offset_ = 0;
absl::optional<std::uint64_t> upload_size_;
ConstBufferSequence payload_;
std::shared_ptr<HashFunction> hash_function_;
HashValues known_object_hashes_;
};
HashValues FinishHashes(UploadChunkRequest const& request);
std::ostream& operator<<(std::ostream& os, UploadChunkRequest const& r);
/**
* A request to query the status of a resumable upload.
*/
class QueryResumableUploadRequest
: public GenericRequest<QueryResumableUploadRequest> {
public:
QueryResumableUploadRequest() = default;
explicit QueryResumableUploadRequest(std::string upload_session_url)
: upload_session_url_(std::move(upload_session_url)) {}
std::string const& upload_session_url() const { return upload_session_url_; }
private:
std::string upload_session_url_;
};
std::ostream& operator<<(std::ostream& os,
QueryResumableUploadRequest const& r);
StatusOr<std::uint64_t> ParseRangeHeader(std::string const& range);
/**
* The response from uploading a chunk and querying a resumable upload.
*
* We use the same type to represent the response for a UploadChunkRequest and a
* QueryResumableUploadRequest because they are the same response. Once a chunk
* is successfully uploaded the response is the new status for the resumable
* upload.
*/
struct QueryResumableUploadResponse {
static StatusOr<QueryResumableUploadResponse> FromHttpResponse(
HttpResponse response);
QueryResumableUploadResponse() = default;
QueryResumableUploadResponse(
absl::optional<std::uint64_t> cs,
absl::optional<google::cloud::storage::ObjectMetadata> p)
: committed_size(std::move(cs)), payload(std::move(p)) {}
QueryResumableUploadResponse(
absl::optional<std::uint64_t> cs,
absl::optional<google::cloud::storage::ObjectMetadata> p,
std::multimap<std::string, std::string> rm)
: committed_size(std::move(cs)),
payload(std::move(p)),
request_metadata(std::move(rm)) {}
absl::optional<std::uint64_t> committed_size;
absl::optional<google::cloud::storage::ObjectMetadata> payload;
std::multimap<std::string, std::string> request_metadata;
};
bool operator==(QueryResumableUploadResponse const& lhs,
QueryResumableUploadResponse const& rhs);
inline bool operator!=(QueryResumableUploadResponse const& lhs,
QueryResumableUploadResponse const& rhs) {
return !(lhs == rhs);
}
std::ostream& operator<<(std::ostream& os,
QueryResumableUploadResponse const& r);
} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
} // namespace cloud
} // namespace google
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_OBJECT_REQUESTS_H