Skip to main content

s3_simple/
types.rs

1use serde::Deserialize;
2use std::str::FromStr;
3
4#[derive(Clone, Debug)]
5pub(crate) struct Multipart<'a> {
6    part_number: u32,
7    upload_id: &'a str,
8}
9
10impl<'a> Multipart<'a> {
11    pub fn query_string(&self) -> String {
12        format!(
13            "?partNumber={}&uploadId={}",
14            self.part_number,
15            percent_encoding::percent_encode(
16                self.upload_id.as_bytes(),
17                percent_encoding::NON_ALPHANUMERIC
18            )
19        )
20    }
21
22    pub fn new(part_number: u32, upload_id: &'a str) -> Self {
23        Multipart {
24            part_number,
25            upload_id,
26        }
27    }
28}
29
30#[derive(Deserialize, Debug, Clone)]
31pub struct Owner {
32    #[serde(rename = "DisplayName")]
33    /// Object owner's name.
34    pub display_name: Option<String>,
35    #[serde(rename = "ID")]
36    /// Object owner's ID.
37    pub id: String,
38}
39
40#[derive(Deserialize, Debug, Clone)]
41pub struct Object {
42    #[serde(rename = "LastModified")]
43    /// Date and time the object was last modified.
44    pub last_modified: String,
45    #[serde(rename = "ETag")]
46    /// The entity tag is an MD5 hash of the object. The ETag only reflects changes to the
47    /// contents of an object, not its metadata.
48    pub e_tag: Option<String>,
49    #[serde(rename = "StorageClass")]
50    /// STANDARD | STANDARD_IA | REDUCED_REDUNDANCY | GLACIER
51    pub storage_class: Option<String>,
52    #[serde(rename = "Key")]
53    /// The object's key
54    pub key: String,
55    #[serde(rename = "Owner")]
56    /// Bucket owner
57    pub owner: Option<Owner>,
58    #[serde(rename = "Size")]
59    /// Size in bytes of the object.
60    pub size: u64,
61}
62
63#[derive(Deserialize, Debug, Clone)]
64pub struct CommonPrefix {
65    #[serde(rename = "Prefix")]
66    /// Keys that begin with the indicated prefix.
67    pub prefix: String,
68}
69
70// Taken from https://github.com/rusoto/rusoto
71#[derive(Deserialize, Debug, Default, Clone)]
72pub struct HeadObjectResult {
73    #[serde(rename = "AcceptRanges")]
74    /// Indicates that a range of bytes was specified.
75    pub accept_ranges: Option<String>,
76    #[serde(rename = "CacheControl")]
77    /// Specifies caching behavior along the request/reply chain.
78    pub cache_control: Option<String>,
79    #[serde(rename = "ContentDisposition")]
80    /// Specifies presentational information for the object.
81    pub content_disposition: Option<String>,
82    #[serde(rename = "ContentEncoding")]
83    /// Specifies what content encodings have been applied to the object and thus what decoding mechanisms must be applied to obtain the media-type referenced by the Content-Type header field.
84    pub content_encoding: Option<String>,
85    #[serde(rename = "ContentLanguage")]
86    /// The language the content is in.
87    pub content_language: Option<String>,
88    #[serde(rename = "ContentLength")]
89    /// Size of the body in bytes.
90    pub content_length: Option<u64>,
91    #[serde(rename = "ContentType")]
92    /// A standard MIME type describing the format of the object data.
93    pub content_type: Option<String>,
94    #[serde(rename = "DeleteMarker")]
95    /// Specifies whether the object retrieved was (true) or was not (false) a Delete Marker.
96    pub delete_marker: Option<bool>,
97    #[serde(rename = "ETag")]
98    /// An ETag is an opaque identifier assigned by a web server to a specific version of a resource found at a URL.
99    pub e_tag: Option<String>,
100    #[serde(rename = "Expiration")]
101    /// If the object expiration is configured, the response includes this header. It includes the expiry-date and rule-id key-value pairs providing object expiration information.
102    /// The value of the rule-id is URL encoded.
103    pub expiration: Option<String>,
104    #[serde(rename = "Expires")]
105    /// The date and time at which the object is no longer cacheable.
106    pub expires: Option<String>,
107    #[serde(rename = "LastModified")]
108    /// Last modified date of the object
109    pub last_modified: Option<String>,
110    #[serde(rename = "Metadata", default)]
111    /// A map of metadata to store with the object in S3.
112    pub metadata: Option<::std::collections::HashMap<String, String>>,
113    #[serde(rename = "MissingMeta")]
114    /// This is set to the number of metadata entries not returned in x-amz-meta headers. This can happen if you create metadata using an API like SOAP that supports more flexible metadata than
115    /// the REST API. For example, using SOAP, you can create metadata whose values are not legal HTTP headers.
116    pub missing_meta: Option<i64>,
117    #[serde(rename = "ObjectLockLegalHoldStatus")]
118    /// Specifies whether a legal hold is in effect for this object. This header is only returned if the requester has the s3:GetObjectLegalHold permission.
119    /// This header is not returned if the specified version of this object has never had a legal hold applied.
120    pub object_lock_legal_hold_status: Option<String>,
121    #[serde(rename = "ObjectLockMode")]
122    /// The Object Lock mode, if any, that's in effect for this object.
123    pub object_lock_mode: Option<String>,
124    #[serde(rename = "ObjectLockRetainUntilDate")]
125    /// The date and time when the Object Lock retention period expires.
126    /// This header is only returned if the requester has the s3:GetObjectRetention permission.
127    pub object_lock_retain_until_date: Option<String>,
128    #[serde(rename = "PartsCount")]
129    /// The count of parts this object has.
130    pub parts_count: Option<i64>,
131    #[serde(rename = "ReplicationStatus")]
132    /// If your request involves a bucket that is either a source or destination in a replication rule.
133    pub replication_status: Option<String>,
134    #[serde(rename = "RequestCharged")]
135    pub request_charged: Option<String>,
136    #[serde(rename = "Restore")]
137    /// If the object is an archived object (an object whose storage class is GLACIER), the response includes this header if either the archive restoration is in progress or an archive copy is already restored.
138    /// If an archive copy is already restored, the header value indicates when Amazon S3 is scheduled to delete the object copy.
139    pub restore: Option<String>,
140    #[serde(rename = "SseCustomerAlgorithm")]
141    /// If server-side encryption with a customer-provided encryption key was requested, the response will include this header confirming the encryption algorithm used.
142    pub sse_customer_algorithm: Option<String>,
143    #[serde(rename = "SseCustomerKeyMd5")]
144    /// If server-side encryption with a customer-provided encryption key was requested, the response will include this header to provide round-trip message integrity verification of the customer-provided encryption key.
145    pub sse_customer_key_md5: Option<String>,
146    #[serde(rename = "SsekmsKeyId")]
147    /// If present, specifies the ID of the AWS Key Management Service (AWS KMS) symmetric customer managed customer master key (CMK) that was used for the object.
148    pub ssekms_key_id: Option<String>,
149    #[serde(rename = "ServerSideEncryption")]
150    /// If the object is stored using server-side encryption either with an AWS KMS customer master key (CMK) or an Amazon S3-managed encryption key,
151    /// The response includes this header with the value of the server-side encryption algorithm used when storing this object in Amazon S3 (for example, AES256, aws:kms).
152    pub server_side_encryption: Option<String>,
153    #[serde(rename = "StorageClass")]
154    /// Provides storage class information of the object. Amazon S3 returns this header for all objects except for S3 Standard storage class objects.
155    pub storage_class: Option<String>,
156    #[serde(rename = "VersionId")]
157    /// Version of the object.
158    pub version_id: Option<String>,
159    #[serde(rename = "WebsiteRedirectLocation")]
160    /// If the bucket is configured as a website, redirects requests for this object to another object in the same bucket or to an external URL. Amazon S3 stores the value of this header in the object metadata.
161    pub website_redirect_location: Option<String>,
162}
163
164trait GetAndConvertHeaders {
165    fn get_and_convert<T: FromStr>(&self, header: &str) -> Option<T>;
166    fn get_string(&self, header: &str) -> Option<String>;
167}
168
169impl GetAndConvertHeaders for http::header::HeaderMap {
170    fn get_and_convert<T: FromStr>(&self, header: &str) -> Option<T> {
171        self.get(header)?.to_str().ok()?.parse::<T>().ok()
172    }
173    fn get_string(&self, header: &str) -> Option<String> {
174        Some(self.get(header)?.to_str().ok()?.to_owned())
175    }
176}
177
178impl From<&http::HeaderMap> for HeadObjectResult {
179    fn from(headers: &http::HeaderMap) -> Self {
180        let mut result = HeadObjectResult {
181            accept_ranges: headers.get_string("accept-ranges"),
182            cache_control: headers.get_string("Cache-Control"),
183            content_disposition: headers.get_string("Content-Disposition"),
184            content_encoding: headers.get_string("Content-Encoding"),
185            content_language: headers.get_string("Content-Language"),
186            content_length: headers.get_and_convert("Content-Length"),
187            content_type: headers.get_string("Content-Type"),
188            delete_marker: headers.get_and_convert("x-amz-delete-marker"),
189            e_tag: headers.get_string("ETag"),
190            expiration: headers.get_string("x-amz-expiration"),
191            expires: headers.get_string("Expires"),
192            last_modified: headers.get_string("Last-Modified"),
193            ..Default::default()
194        };
195        let mut values = ::std::collections::HashMap::new();
196        for (key, value) in headers.iter() {
197            if key.as_str().starts_with("x-amz-meta-")
198                && let Ok(value) = value.to_str()
199            {
200                values.insert(
201                    key.as_str()["x-amz-meta-".len()..].to_owned(),
202                    value.to_owned(),
203                );
204            }
205        }
206        result.metadata = Some(values);
207        result.missing_meta = headers.get_and_convert("x-amz-missing-meta");
208        result.object_lock_legal_hold_status = headers.get_string("x-amz-object-lock-legal-hold");
209        result.object_lock_mode = headers.get_string("x-amz-object-lock-mode");
210        result.object_lock_retain_until_date =
211            headers.get_string("x-amz-object-lock-retain-until-date");
212        result.parts_count = headers.get_and_convert("x-amz-mp-parts-count");
213        result.replication_status = headers.get_string("x-amz-replication-status");
214        result.request_charged = headers.get_string("x-amz-request-charged");
215        result.restore = headers.get_string("x-amz-restore");
216        result.sse_customer_algorithm =
217            headers.get_string("x-amz-server-side-encryption-customer-algorithm");
218        result.sse_customer_key_md5 =
219            headers.get_string("x-amz-server-side-encryption-customer-key-MD5");
220        result.ssekms_key_id = headers.get_string("x-amz-server-side-encryption-aws-kms-key-id");
221        result.server_side_encryption = headers.get_string("x-amz-server-side-encryption");
222        result.storage_class = headers.get_string("x-amz-storage-class");
223        result.version_id = headers.get_string("x-amz-version-id");
224        result.website_redirect_location = headers.get_string("x-amz-website-redirect-location");
225        result
226    }
227}
228
229#[derive(Deserialize, Debug, Clone)]
230pub struct ListBucketResult {
231    #[serde(rename = "Name")]
232    /// Name of the bucket.
233    pub name: String,
234    #[serde(rename = "Delimiter")]
235    /// A delimiter is a character you use to group keys.
236    pub delimiter: Option<String>,
237    #[serde(rename = "MaxKeys")]
238    /// Sets the maximum number of keys returned in the response body.
239    pub max_keys: Option<i32>,
240    #[serde(rename = "Prefix")]
241    /// Limits the response to keys that begin with the specified prefix.
242    pub prefix: Option<String>,
243    #[serde(rename = "ContinuationToken")] // for ListObjectsV2 request
244    #[serde(alias = "Marker")] // for ListObjects request
245    /// Indicates where in the bucket listing begins. It is included in the response if
246    /// it was sent with the request.
247    pub continuation_token: Option<String>,
248    #[serde(rename = "EncodingType")]
249    /// Specifies the encoding method to used
250    pub encoding_type: Option<String>,
251    #[serde(default, rename = "IsTruncated")]
252    ///  Specifies whether (true) or not (false) all of the results were returned.
253    ///  If the number of results exceeds that specified by MaxKeys, all of the results
254    ///  might not be returned.
255
256    /// When the response is truncated (that is, the IsTruncated element value in the response
257    /// is true), you can use the key name in in 'next_continuation_token' as a marker in the
258    /// subsequent request to get next set of objects. Amazon S3 lists objects in UTF-8 character
259    /// encoding in lexicographical order.
260    pub is_truncated: bool,
261    #[serde(rename = "NextContinuationToken", default)] // for ListObjectsV2 request
262    #[serde(alias = "NextMarker")] // for ListObjects request
263    pub next_continuation_token: Option<String>,
264    #[serde(rename = "Contents", default)]
265    /// Metadata about each object returned.
266    pub contents: Vec<Object>,
267    #[serde(rename = "CommonPrefixes", default)]
268    /// All of the keys rolled up into a common prefix count as a single return when
269    /// calculating the number of returns.
270    pub common_prefixes: Option<Vec<CommonPrefix>>,
271}
272
273#[derive(Deserialize, Debug)]
274pub(crate) struct InitiateMultipartUploadResponse {
275    #[serde(rename = "Bucket")]
276    _bucket: String,
277    // Only used for logging/diagnostics; the upload itself targets the
278    // requested key (see `put_stream_with`).
279    #[allow(dead_code)]
280    #[serde(rename = "Key")]
281    pub key: String,
282    #[serde(rename = "UploadId")]
283    pub upload_id: String,
284}
285
286#[derive(Debug)]
287pub struct PutStreamResponse {
288    pub status_code: u16,
289    pub uploaded_bytes: usize,
290}