google_cloud_storage/http/objects/rewrite.rs
1use reqwest_middleware::{ClientWithMiddleware as Client, RequestBuilder};
2
3use crate::http::object_access_controls::{PredefinedObjectAcl, Projection};
4use crate::http::objects::{Encryption, Object};
5use crate::http::Escape;
6
7#[derive(Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, Debug, Default)]
8#[serde(rename_all = "camelCase")]
9pub struct RewriteObjectRequest {
10 /// Name of the bucket in which to store the new object. Overrides the provided
11 /// object metadata's bucket value, if any.
12 #[serde(skip_serializing)]
13 pub destination_bucket: String,
14 /// Name of the new object. Required when the object metadata is not otherwise provided.
15 /// Overrides the object metadata's name value, if any. For information about how to
16 /// URL encode object names to be path safe, see Encoding URI path parts.
17 #[serde(skip_serializing)]
18 pub destination_object: String,
19 /// Name of the bucket in which to find the source object.
20 #[serde(skip_serializing)]
21 pub source_bucket: String,
22 ///Name of the source object. For information about how to URL encode object names
23 /// to be path safe, see Encoding URI path parts.
24 #[serde(skip_serializing)]
25 pub source_object: String,
26 /// If set, only deletes the bucket if its metageneration matches this value.
27 pub if_destination_metageneration_match: Option<i64>,
28 /// If set, only deletes the bucket if its metageneration does not match this
29 /// value.
30 pub if_destination_metageneration_not_match: Option<i64>,
31 /// If set, only deletes the bucket if its metageneration matches this value.
32 pub if_source_metageneration_match: Option<i64>,
33 /// If set, only deletes the bucket if its metageneration does not match this
34 /// value.
35 pub if_source_metageneration_not_match: Option<i64>,
36 /// Resource name of the Cloud KMS key that will be used to encrypt the object. The Cloud KMS key must be located in same location as the object.
37 /// If the parameter is not specified, the request uses the destination bucket's default encryption key,
38 /// if any, or the Google-managed encryption key.
39 pub destination_kms_key_name: Option<String>,
40 /// Apply a predefined set of access controls to the destination object.
41 /// Acceptable values are:
42 /// authenticatedRead: Object owner gets OWNER access, and allAuthenticatedUsers get READER access.
43 /// bucketOwnerFullControl: Object owner gets OWNER access, and project team owners get OWNER access.
44 /// bucketOwnerRead: Object owner gets OWNER access, and project team owners get READER access.
45 /// private: Object owner gets OWNER access.
46 /// projectPrivate: Object owner gets OWNER access, and project team members get access according to their roles.
47 /// publicRead: Object owner gets OWNER access, and allUsers get READER access.
48 /// If iamConfiguration.uniformBucketLevelAccess.enabled is set to true,
49 /// requests that include this parameter fail with a 400 Bad Request response.
50 pub destination_predefined_object_acl: Option<PredefinedObjectAcl>,
51 /// The maximum number of bytes that will be rewritten per rewrite request.
52 /// Most callers shouldn't need to specify this parameter - it is primarily in place to
53 /// support testing. If specified the value must be an integral multiple of 1 MiB (1048576).
54 /// Also, this only applies to requests where the source and destination span
55 /// locations and/or storage classes. Finally,
56 /// this value must not change across rewrite calls else you'll get an error
57 /// that the rewriteToken is invalid.
58 pub max_bytes_rewritten_per_call: Option<i64>,
59 /// Set of properties to return. Defaults to noAcl,
60 /// unless the object resource specifies the acl property, when it defaults to full.
61 /// Acceptable values are:
62 /// full: Include all properties.
63 /// noAcl: Omit the owner, acl property.
64 pub projection: Option<Projection>,
65 /// If present, selects a specific revision of the source object (as opposed to the latest version, the default).
66 pub source_generation: Option<i64>,
67 /// Include this field (from the previous rewrite response) on each rewrite request
68 /// after the first one, until the rewrite response 'done' flag is true.
69 /// Calls that provide a rewriteToken can omit all other request fields,
70 /// but if included those fields must match the values provided in the first rewrite request.
71 pub rewrite_token: Option<String>,
72 /// Destination object metadata.
73 #[serde(skip_serializing)]
74 pub destination_metadata: Option<Object>,
75 /// Source encryption setting
76 #[serde(skip_serializing)]
77 pub source_encryption: Option<Encryption>,
78 /// Destination encryption setting
79 #[serde(skip_serializing)]
80 pub destination_encryption: Option<Encryption>,
81}
82
83/// A rewrite response.
84#[derive(Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, Debug)]
85#[serde(rename_all = "camelCase")]
86pub struct RewriteObjectResponse {
87 /// The total bytes written so far, which can be used to provide a waiting user
88 /// with a progress indicator. This property is always present in the response.
89 #[serde(deserialize_with = "crate::http::from_str")]
90 pub total_bytes_rewritten: i64,
91 /// The total size of the object being copied in bytes. This property is always
92 /// present in the response.
93 #[serde(deserialize_with = "crate::http::from_str")]
94 pub object_size: i64,
95 /// `true` if the copy is finished; otherwise, `false` if
96 /// the copy is in progress. This property is always present in the response.
97 pub done: bool,
98 /// A token to use in subsequent requests to continue copying data. This token
99 /// is present in the response only when there is more data to copy.
100 pub rewrite_token: Option<String>,
101 /// A resource containing the metadata for the copied-to object. This property
102 /// is present in the response only when copying completes.
103 pub resource: Option<Object>,
104}
105
106pub(crate) fn build(base_url: &str, client: &Client, req: &RewriteObjectRequest) -> RequestBuilder {
107 let url = format!(
108 "{}/b/{}/o/{}/rewriteTo/b/{}/o/{}",
109 base_url,
110 req.source_bucket.escape(),
111 req.source_object.escape(),
112 req.destination_bucket.escape(),
113 req.destination_object.escape()
114 );
115 let mut builder = client.post(url).query(&req).json(&req.destination_metadata);
116 if let Some(e) = &req.destination_encryption {
117 builder = e.with_headers(builder)
118 }
119 if let Some(e) = &req.source_encryption {
120 builder
121 .header("X-Goog-Copy-Source-Encryption-Algorithm", &e.encryption_algorithm)
122 .header("X-Goog-Copy-Source-Encryption-Key", &e.encryption_key)
123 .header("X-Goog-Copy-Source-Encryption-Key-Sha256", &e.encryption_key_sha256)
124 } else {
125 builder
126 }
127}