Struct s3_sync::S3

source ·
pub struct S3 { /* private fields */ }
Expand description

Wrapper of Rusoto S3 client that adds some high level imperative and declarative operations on S3 buckets and objects.

Implementations§

Creates high level S3 client.

  • region - the AWS region to connect to; when None autodetects the region value (see Region for detail)
  • region_endpoint - use dedicated AWS endpoint within the region
  • settings - use specific client setting
Examples found in repository?
src/lib.rs (line 545)
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
    fn default() -> S3 {
        S3::new(None, None, None)
    }
}

impl S3 {
    /// Creates high level S3 client.
    ///
    /// * `region` - the AWS region to connect to; when `None` autodetects the region value (see [Region] for detail)
    /// * `region_endpoint` - use dedicated AWS endpoint within the region
    /// * `settings` - use specific client setting
    pub fn new(
        region: impl Into<Option<Region>>,
        region_endpoint: impl Into<Option<String>>,
        settings: impl Into<Option<Settings>>
    ) -> S3 {
        let region = match (region.into(), region_endpoint.into()) {
            (Some(region), Some(endpoint)) => Region::Custom { name: region.name().to_owned(), endpoint },
            (None, Some(endpoint)) => Region::Custom { name: Region::default().name().to_owned(), endpoint },
            (Some(region), None) => region,
            _ => Region::default(),
        };
        let settings = settings.into().unwrap_or_default();

        S3 {
            client: S3Client::new(region),
            on_upload_progress: None,
            part_size: settings.part_size,
            timeout: settings.timeout,
            data_timeout: settings.data_timeout,
            max_multipart_upload_parts: settings.max_multipart_upload_parts,
            max_delete_objects: settings.max_delete_objects,
        }
    }

    /// Creates high level S3 client with given region and default settings.
    pub fn with_region(region: Region) -> S3 {
        S3::new(region, None, None)
    }

Creates high level S3 client with given region and default settings.

Gets maximum size of the multipart upload part.

Useful to set up other I/O buffers accordingly.

Returns maximum size of data in bytes that can be uploaded to a single object with current settings.

Set callback on body upload progress.

Examples found in repository?
src/lib.rs (line 613)
608
609
610
611
612
613
614
615
616
617
    pub fn with_on_upload_progress<O>(
        &mut self,
        callback: impl FnMut(&TransferStatus) + 'static,
        f: impl FnOnce(&mut Self) -> O
    ) -> O {
        let old = self.on_upload_progress(callback);
        let ret = f(self);
        old.map(|callback| self.on_upload_progress(callback));
        ret
    }

Calls f with S3 client that has S3::on_upload_progress() set to callback and restores callback to previous state on return.

Checks if given bucket exists.

Checks if given object exists.

  • implementation - select implementation of this function

Note:

Examples found in repository?
src/lib.rs (line 1066)
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
    pub fn object_present<'b, 's: 'b, R, F>(
        &'s self,
        bucket_key: BucketKey<'b>,
        check_impl: CheckObjectImpl,
        body: F
    ) -> impl Ensure<Present<BucketKey<'b>>, EnsureAction = impl Meet<Met = Present<BucketKey<'b>>, Error = S3SyncError> + Captures1<'s> + Captures2<'b>> + Captures1<'s> + Captures2<'b>
    where R: Read + 's, F: FnOnce() -> Result<(R, ObjectBodyMeta), std::io::Error> + 's {
        move || {
            Ok(match self.check_object_exists(bucket_key, check_impl)? {
                Left(present) => Met(present.into()),
                Right(absent) => EnsureAction(move || {
                    let (body, meta) = body()?;
                    self.put_object(absent, body, meta)
                })
            })
        }
    }

    /// Returns [Ensure] value that can be used to ensure that object is absent in the S3 bucket.
    ///
    /// Warning: There can be a race condition between check if object exists and the delete operation.
    pub fn object_absent<'b, 's: 'b>(
        &'s self,
        bucket_key: BucketKey<'b>,
        check_impl: CheckObjectImpl
    ) -> impl Ensure<Absent<BucketKey<'b>>, EnsureAction = impl Meet<Met = Absent<BucketKey<'b>>, Error = S3SyncError> + Captures1<'s> + Captures2<'b>> + Captures1<'s> + Captures2<'b> {
        move || {
            Ok(match self.check_object_exists(bucket_key, check_impl)? {
                Right(absent) => Met(absent),
                Left(present) => EnsureAction(move || {
                    self.delete_object(present)
                })
            })
        }
    }

Checks if given object exists by issuing HeadObject API request.

Note:

Examples found in repository?
src/lib.rs (line 652)
648
649
650
651
652
653
654
    pub fn check_object_exists<'s, 'b>(&'s self, bucket_key: BucketKey<'b>, implementation: CheckObjectImpl)
        -> Result<Either<Object<'b>, Absent<BucketKey<'b>>>, S3SyncError> {
        match implementation {
            CheckObjectImpl::List => self.check_object_exists_list(bucket_key),
            CheckObjectImpl::Head => self.check_object_exists_head(bucket_key),
        }
    }

Checks if given object exists by listing objects with ListObjcetsV2 API request.

Note:

Examples found in repository?
src/lib.rs (line 651)
648
649
650
651
652
653
654
    pub fn check_object_exists<'s, 'b>(&'s self, bucket_key: BucketKey<'b>, implementation: CheckObjectImpl)
        -> Result<Either<Object<'b>, Absent<BucketKey<'b>>>, S3SyncError> {
        match implementation {
            CheckObjectImpl::List => self.check_object_exists_list(bucket_key),
            CheckObjectImpl::Head => self.check_object_exists_head(bucket_key),
        }
    }

Provides iterator of objects in existing bucket that have key of given prefix.

Note:

Gets object body.

Gets object body boxed.

Note: This is provided as a workaround for “impl Trait return type capcuring all input lifetimes” if needed.

Gets object body retrying the operation in case of an error.

  • retires - retry get_body call up to that many times
  • on_error - called when get_body call fails and there are still retries left; if gets number of retries left and the error and if it returns false the retry loop is aborted

Note:

  • The on_error closure may need to pause the execution of the thread to delay next retry attempt.
  • Once this function returns, the subsequent read operation failures are not retried.

Creates the S3 object with given body using multipart upload API.

Warning: Existing object will be overwritten (subject to bucket versioning settings).

The size of the body is limited to value returned by S3::max_upload_size(). Increase Settings::part_size to be able to upload more data (max_upload_size = part_size * 10_000 on AWS; with default settings the limit is 100_000 MiB).

Examples found in repository?
src/lib.rs (line 1070)
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
    pub fn object_present<'b, 's: 'b, R, F>(
        &'s self,
        bucket_key: BucketKey<'b>,
        check_impl: CheckObjectImpl,
        body: F
    ) -> impl Ensure<Present<BucketKey<'b>>, EnsureAction = impl Meet<Met = Present<BucketKey<'b>>, Error = S3SyncError> + Captures1<'s> + Captures2<'b>> + Captures1<'s> + Captures2<'b>
    where R: Read + 's, F: FnOnce() -> Result<(R, ObjectBodyMeta), std::io::Error> + 's {
        move || {
            Ok(match self.check_object_exists(bucket_key, check_impl)? {
                Left(present) => Met(present.into()),
                Right(absent) => EnsureAction(move || {
                    let (body, meta) = body()?;
                    self.put_object(absent, body, meta)
                })
            })
        }
    }

Deletes single object.

Note: Delete call does not fail if object does not exist.

To delete many objects use S3::delete_objects() witch uses bulk delete API.

Examples found in repository?
src/lib.rs (line 1088)
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
    pub fn object_absent<'b, 's: 'b>(
        &'s self,
        bucket_key: BucketKey<'b>,
        check_impl: CheckObjectImpl
    ) -> impl Ensure<Absent<BucketKey<'b>>, EnsureAction = impl Meet<Met = Absent<BucketKey<'b>>, Error = S3SyncError> + Captures1<'s> + Captures2<'b>> + Captures1<'s> + Captures2<'b> {
        move || {
            Ok(match self.check_object_exists(bucket_key, check_impl)? {
                Right(absent) => Met(absent),
                Left(present) => EnsureAction(move || {
                    self.delete_object(present)
                })
            })
        }
    }

Deletes list of objects in streaming fashion using bulk delete API.

Warning: If returned iterator is not completely consumed not all items from the list may be deleted.

It is not an error to delete non-existing S3 object.

Objects can live in different buckets but for best performance it is recommended to order the list by bucket so that biggest batches can be crated.

Each returned item represent batch delete call to S3 API.

Successful batch call will return Result::Ok variant containing vector of results for each individual object delete operation as provided by S3.

Returns Ensure value that can be used to ensure that object is present in the S3 bucket.

If S3 object does not exist this method will call body function to obtain Read and metadata values, then data read will be uploaded to the new S3 object with given metadata set on it.

Warning: There can be a race condition between check if object exists and the upload creating it.

Returns Ensure value that can be used to ensure that object is absent in the S3 bucket.

Warning: There can be a race condition between check if object exists and the delete operation.

Trait Implementations§

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.