esthri 11.2.0

Extremely simple (memory stable) S3 client that supports get, put, head, list, and sync.
Documentation
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
/*
* Copyright (C) 2020 Swift Navigation Inc.
* Contact: Swift Navigation <dev@swiftnav.com>
*
* This source is subject to the license found in the file 'LICENSE' which must
* be be distributed together with this source. All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*/

#![cfg_attr(feature = "aggressive_lint", deny(warnings))]
#![recursion_limit = "256"]

pub mod aws_sdk;
#[cfg(feature = "blocking")]
pub mod blocking;
pub mod errors;

pub(crate) mod compression;
pub(crate) mod tempfile;
pub(crate) mod types;

mod config;
mod ops;
mod presign;

use std::{marker::Unpin, path::Path, time::Duration};

pub use crate::config::Config;
use crate::{aws_sdk::*, types::S3Listing};
use futures::{stream, TryStream, TryStreamExt};
use log::{info, warn};
use log_derive::logfn;
use md5::{Digest, Md5};
use tokio::{
    fs,
    io::{AsyncRead, AsyncReadExt, BufReader},
};

pub use errors::{Error, Result};
pub use ops::{
    copy::copy,
    delete::{delete, delete_streaming},
    download::{download, download_streaming},
    sync::{streaming::sync as sync_streaming, sync, GlobFilter},
    upload::{upload, upload_from_reader, PendingUpload},
};
pub use presign::{
    delete::{delete_file_presigned, presign_delete},
    download::{download_file_presigned, presign_get},
    multipart_upload::{
        abort_presigned_multipart_upload, complete_presigned_multipart_upload,
        setup_presigned_multipart_upload, upload_file_presigned_multipart_upload,
        PresignedMultipartUpload,
    },
    upload::{presign_put, upload_file_presigned},
};

pub use aws_sdk::HeadObjectInfo;
pub use aws_smithy_client::hyper_ext;
pub use types::{S3ListingItem, S3Object, S3PathParam};

pub use esthri_internals::new_https_connector;

pub const FILTER_EMPTY: Option<&[GlobFilter]> = None;

pub enum AwsCredProvider {
    DefaultProvider,
    Environment,
    Profile,
    Ecs,
    Imds,
    WebIdentityToken,
}

/// This function builds a AWS client using the default AWS region
/// and default credentials_provider
pub async fn init_default_s3client() -> Client {
    init_default_s3client_with_region(None::<&str>).await
}

/// This function builds a AWS client using default credentials_provider,
/// allowing you to optionally override the default aws_region
pub async fn init_default_s3client_with_region(region: Option<impl AsRef<str>>) -> Client {
    init_s3client_with_region(AwsCredProvider::DefaultProvider, region).await
}

/// This function builds a AWS client using the default AWS region.
pub async fn init_s3client(provider: AwsCredProvider) -> Client {
    init_s3client_with_region(provider, None::<&str>).await
}

/// This function builds a AWS client, while allowing you to optionally
/// override the default aws region.
pub async fn init_s3client_with_region(
    provider: AwsCredProvider,
    region: Option<impl AsRef<str>>,
) -> Client {
    let retry_config = aws_config::retry::RetryConfig::standard()
        .with_initial_backoff(Duration::from_millis(500))
        .with_max_attempts(5);
    let https_connector = new_https_connector();
    let smithy_connector = hyper_ext::Adapter::builder().build(https_connector);

    let sdk_config = if let Some(region) = region {
        aws_config::from_env()
            .region(Region::new(region.as_ref().to_owned()))
            .load()
            .await
    } else {
        aws_config::load_from_env().await
    };
    let config = match provider {
        AwsCredProvider::DefaultProvider => aws_sdk_s3::config::Builder::from(&sdk_config)
            .retry_config(retry_config)
            .http_connector(smithy_connector)
            .build(),
        AwsCredProvider::Environment => {
            let cred = aws_config::environment::EnvironmentVariableCredentialsProvider::new();
            aws_sdk_s3::config::Builder::from(&sdk_config)
                .retry_config(retry_config)
                .http_connector(smithy_connector)
                .credentials_provider(cred)
                .build()
        }
        AwsCredProvider::Profile => {
            let cred = aws_config::profile::ProfileFileCredentialsProvider::builder().build();
            aws_sdk_s3::config::Builder::from(&sdk_config)
                .retry_config(retry_config)
                .http_connector(smithy_connector)
                .credentials_provider(cred)
                .build()
        }
        AwsCredProvider::Ecs => {
            let cred = aws_config::ecs::EcsCredentialsProvider::builder().build();
            aws_sdk_s3::config::Builder::from(&sdk_config)
                .retry_config(retry_config)
                .http_connector(smithy_connector)
                .credentials_provider(cred)
                .build()
        }
        AwsCredProvider::Imds => {
            let cred = aws_config::imds::credentials::ImdsCredentialsProvider::builder().build();
            aws_sdk_s3::config::Builder::from(&sdk_config)
                .retry_config(retry_config)
                .http_connector(smithy_connector)
                .credentials_provider(cred)
                .build()
        }
        AwsCredProvider::WebIdentityToken => {
            let cred =
                aws_config::web_identity_token::WebIdentityTokenCredentialsProvider::builder()
                    .build();
            aws_sdk_s3::config::Builder::from(&sdk_config)
                .retry_config(retry_config)
                .http_connector(smithy_connector)
                .credentials_provider(cred)
                .build()
        }
    };

    aws_sdk_s3::Client::from_conf(config)
}

pub async fn compute_etag(path: impl AsRef<Path>) -> Result<String> {
    let f = match fs::File::open(path).await {
        Ok(f) => f,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            return Err(Error::ETagNotPresent);
        }
        Err(e) => {
            return Err(e.into());
        }
    };
    let file_size = f.metadata().await?.len();
    compute_etag_from_reader(f, file_size).await
}

pub async fn compute_etag_from_reader<T>(reader: T, length: u64) -> Result<String>
where
    T: AsyncRead + Unpin + Send + 'static,
{
    let mut reader = BufReader::new(reader);
    let mut hash = Md5::new();
    let mut digests: Vec<[u8; 16]> = vec![];
    let mut remaining = length;
    let upload_part_size = Config::global().upload_part_size();
    while remaining != 0 {
        let upload_part_size: usize = (if remaining >= upload_part_size {
            upload_part_size
        } else {
            remaining
        }) as usize;
        let mut blob = vec![0u8; upload_part_size];
        reader.read_exact(&mut blob).await?;
        hash.update(&blob);
        let hash_bytes: [u8; 16] = hash.finalize_reset().into();
        digests.push(hash_bytes);
        remaining -= upload_part_size as u64;
    }
    if digests.is_empty() {
        let hash_bytes = hash.finalize();
        let hex_digest = hex::encode(hash_bytes);
        Ok(format!("\"{}\"", hex_digest))
    } else if digests.len() == 1 && length < upload_part_size {
        let hex_digest = hex::encode(digests[0]);
        Ok(format!("\"{}\"", hex_digest))
    } else {
        let count = digests.len();
        let mut etag_hash = Md5::new();
        for digest_bytes in digests {
            etag_hash.update(digest_bytes);
        }
        let final_hash = etag_hash.finalize();
        let hex_digest = hex::encode(final_hash);
        Ok(format!("\"{}-{}\"", hex_digest, count))
    }
}

#[logfn(err = "ERROR")]
pub async fn head_object(
    s3: &Client,
    bucket: impl AsRef<str>,
    key: impl AsRef<str>,
) -> Result<Option<HeadObjectInfo>> {
    let (bucket, key) = (bucket.as_ref(), key.as_ref());
    info!("head-object: bucket={}, key={}", bucket, key);
    head_object_request(s3, bucket, key, None).await
}

#[logfn(err = "ERROR")]
pub async fn list_objects(
    s3: &Client,
    bucket: impl AsRef<str>,
    key: impl AsRef<str>,
) -> Result<Vec<String>> {
    let none: Option<&str> = None;
    list_objects_with_delim(s3, bucket, key, none).await
}

#[logfn(err = "ERROR")]
pub async fn list_directory(
    s3: &Client,
    bucket: impl AsRef<str>,
    dir_path: impl AsRef<str>,
) -> Result<Vec<String>> {
    list_objects_with_delim(s3, bucket, dir_path, Some("/")).await
}

async fn list_objects_with_delim<S0, S1, S2>(
    s3: &Client,
    bucket: S0,
    key: S1,
    delim: Option<S2>,
) -> Result<Vec<String>>
where
    S0: AsRef<str>,
    S1: AsRef<str>,
    S2: AsRef<str>,
{
    let (bucket, key, delim) = (bucket.as_ref(), key.as_ref(), delim.as_ref());

    let batches: Vec<_> = list_objects_stream_with_delim(s3, bucket, key, delim)
        .try_collect()
        .await?;

    let keys: Vec<_> = batches
        .into_iter()
        .flat_map(|batch| {
            batch.into_iter().map(|entry| {
                match &entry {
                    S3ListingItem::S3Object(obj) => info!("key={}, etag={}", obj.key, obj.e_tag),
                    S3ListingItem::S3CommonPrefix(cp) => info!("common_prefix={}", cp),
                }
                entry.prefix()
            })
        })
        .collect();

    Ok(keys)
}

pub fn list_objects_stream<'a>(
    s3: &'a Client,
    bucket: impl AsRef<str> + 'a,
    key: impl AsRef<str> + 'a,
) -> impl TryStream<Ok = Vec<S3ListingItem>, Error = Error> + Unpin + 'a {
    let no_delim: Option<&str> = None;
    list_objects_stream_with_delim(s3, bucket, key, no_delim)
}

pub fn list_directory_stream<'a>(
    s3: &'a Client,
    bucket: &'a str,
    key: &'a str,
) -> impl TryStream<Ok = Vec<S3ListingItem>, Error = Error> + Unpin + 'a {
    let slash_delim = Some("/");
    list_objects_stream_with_delim(s3, bucket, key, slash_delim)
}

fn list_objects_stream_with_delim(
    s3: &'_ Client,
    bucket: impl AsRef<str>,
    key: impl AsRef<str>,
    delimiter: Option<impl AsRef<str>>,
) -> impl TryStream<Ok = Vec<S3ListingItem>, Error = Error> + Unpin + '_ {
    let (bucket, key) = (bucket.as_ref().to_owned(), key.as_ref().to_owned());

    info!("stream-objects: bucket={}, key={}", bucket, key);

    let continuation: Option<String> = None;
    let delimiter = delimiter.map(|s| s.as_ref().to_owned());

    let state = (s3, bucket, key, continuation, delimiter, false);

    Box::pin(stream::try_unfold(
        state,
        |(s3, bucket, key, prev_continuation, delimiter, done)| async move {
            // You can't yield a value and stop unfold at the same time, so do this
            if done {
                return Ok(None);
            }

            let listing =
                list_objects_request(s3, &bucket, &key, prev_continuation, delimiter.clone())
                    .await?;
            let continuation = listing.continuation.clone();

            info!("found count: {}", listing.count());

            if listing.continuation.is_some() {
                Ok(Some((
                    listing.combined(),
                    (s3, bucket, key, continuation, delimiter, false),
                )))
            } else if !listing.is_empty() {
                // Yield the last values, and exit on the next loop
                Ok(Some((
                    listing.combined(),
                    (s3, bucket, key, continuation, delimiter, true),
                )))
            } else {
                // Nothing to yield and we're done
                Ok(None)
            }
        },
    ))
}

async fn list_objects_request(
    s3: &Client,
    bucket: &str,
    key: &str,
    continuation: Option<String>,
    delimiter: Option<String>,
) -> Result<S3Listing> {
    let lov2o = s3
        .list_objects_v2()
        .bucket(bucket)
        .prefix(key)
        .set_continuation_token(continuation)
        .set_delimiter(delimiter)
        .send()
        .await
        .map_err(|e| Error::ListObjectsFailed {
            prefix: key.to_string(),
            source: Box::new(e.into_service_error()),
        })?;

    let mut listing = S3Listing {
        continuation: lov2o.next_continuation_token,
        ..Default::default()
    };

    if let Some(contents) = lov2o.contents {
        for object in contents {
            match (object.key, object.e_tag) {
                (Some(key), Some(e_tag)) => {
                    listing.contents.push(S3Object { key, e_tag });
                }
                (key, etag) => {
                    if key.is_none() {
                        warn!("unexpected: object key was null");
                    }
                    if etag.is_none() {
                        warn!("unexpected: object ETag was null");
                    }
                    continue;
                }
            }
        }
    }

    if let Some(common_prefixes) = lov2o.common_prefixes {
        for common_prefix in common_prefixes {
            match common_prefix.prefix {
                Some(prefix) => listing.common_prefixes.push(prefix),
                None => {
                    warn!("unexpected: prefix was null");
                    continue;
                }
            }
        }
    }

    Ok(listing)
}

pub mod opts {
    use aws_sdk_s3::types::StorageClass;
    use derive_builder::Builder;
    use glob::Pattern;

    #[derive(Debug, Clone, Builder)]
    pub struct AwsCopyOptParams {
        #[builder(default = "Some(StorageClass::Standard)")]
        pub storage_class: Option<StorageClass>,
        #[builder(default)]
        pub transparent_compression: bool,
    }

    #[derive(Debug, Clone, Builder)]
    pub struct EsthriPutOptParams {
        #[builder(default = "Some(StorageClass::Standard)")]
        pub storage_class: Option<StorageClass>,
        #[builder(default)]
        pub transparent_compression: bool,
    }

    impl From<SharedSyncOptParams> for EsthriPutOptParams {
        fn from(opt: SharedSyncOptParams) -> Self {
            Self {
                storage_class: Some(StorageClass::Standard),
                transparent_compression: opt.transparent_compression,
            }
        }
    }

    impl From<AwsCopyOptParams> for EsthriPutOptParams {
        fn from(opt: AwsCopyOptParams) -> Self {
            Self {
                storage_class: opt.storage_class,
                transparent_compression: opt.transparent_compression,
            }
        }
    }

    #[derive(Debug, Copy, Clone, Builder)]
    pub struct EsthriGetOptParams {
        #[builder(default)]
        pub transparent_compression: bool,
    }

    impl From<AwsCopyOptParams> for EsthriGetOptParams {
        fn from(opt: AwsCopyOptParams) -> Self {
            Self {
                transparent_compression: opt.transparent_compression,
            }
        }
    }

    impl From<SharedSyncOptParams> for EsthriGetOptParams {
        fn from(opt: SharedSyncOptParams) -> Self {
            Self {
                transparent_compression: opt.transparent_compression,
            }
        }
    }

    #[derive(Debug, Clone, Builder)]
    pub struct SharedSyncOptParams {
        #[builder(default)]
        pub include: Option<Vec<Pattern>>,
        #[builder(default)]
        pub exclude: Option<Vec<Pattern>>,
        #[builder(default)]
        pub transparent_compression: bool,
        #[builder(default)]
        pub delete: bool,
    }
}