Skip to main content

s3sync/types/
preprocess_callback.rs

1use anyhow::Error as AnyhowError;
2use anyhow::Result;
3use async_trait::async_trait;
4use aws_sdk_s3::operation::get_object::GetObjectOutput;
5use aws_sdk_s3::types::{ObjectCannedAcl, RequestPayer, StorageClass};
6use aws_smithy_types::DateTime;
7use std::collections::HashMap;
8use thiserror::Error;
9
10#[derive(Default, Debug, Clone)]
11pub struct UploadMetadata {
12    pub acl: Option<ObjectCannedAcl>,
13    pub cache_control: Option<String>,
14    pub content_disposition: Option<String>,
15    pub content_encoding: Option<String>,
16    pub content_language: Option<String>,
17    pub content_type: Option<String>,
18    pub expires: Option<DateTime>,
19    pub metadata: Option<HashMap<String, String>>,
20    pub request_payer: Option<RequestPayer>,
21    pub storage_class: Option<StorageClass>,
22    pub website_redirect_location: Option<String>,
23    pub tagging: Option<String>,
24}
25
26#[derive(Debug, Error)]
27pub enum PreprocessError {
28    #[error("Preprocess failed")]
29    Other(String),
30    #[error("Callback cancelled")]
31    Cancelled,
32}
33
34pub fn is_callback_cancelled(err: &AnyhowError) -> bool {
35    err.downcast_ref::<PreprocessError>()
36        .is_some_and(|e| matches!(e, PreprocessError::Cancelled))
37}
38
39#[async_trait]
40pub trait PreprocessCallback {
41    /// Preprocess the object before uploading to S3.
42    /// This method is called before the upload process starts.
43    /// It can modify the `UploadMetadata` to set properties like content-type, user-defined metadata, etc.
44    ///
45    /// If the error returned is `PreprocessError::Cancelled`, the upload will be canceled.
46    /// If the error is any other type, it will be treated as a not-retryable error.
47    async fn preprocess_before_upload(
48        &mut self,
49        key: &str,
50        source_object: &GetObjectOutput,
51        metadata: &mut UploadMetadata,
52    ) -> Result<()>;
53}