-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
use std::time::Duration; | ||
|
||
use time::OffsetDateTime; | ||
use url::Url; | ||
|
||
use super::S3Action; | ||
use crate::actions::Method; | ||
use crate::signing::sign; | ||
use crate::{Bucket, Credentials, Map}; | ||
|
||
/// Create a copy of an object that is already stored in S3, using a `PUT` request. | ||
/// | ||
/// Find out more about CopyObject from the [AWS API Reference][api] | ||
/// | ||
/// [api]: https://docs.aws.amazon.com/AmazonS3/latest/API/API_CopyObject.html | ||
#[derive(Debug, Clone)] | ||
pub struct CopyObject<'a> { | ||
bucket: &'a Bucket, | ||
credentials: Option<&'a Credentials>, | ||
src_object: &'a str, | ||
dst_object: &'a str, | ||
|
||
query: Map<'a>, | ||
headers: Map<'a>, | ||
} | ||
|
||
impl<'a> CopyObject<'a> { | ||
#[inline] | ||
pub fn new( | ||
bucket: &'a Bucket, | ||
credentials: Option<&'a Credentials>, | ||
src_object: &'a str, | ||
dst_object: &'a str, | ||
) -> Self { | ||
let mut query = Map::new(); | ||
query.insert( | ||
"x-amz-copy-source", | ||
format!("{}/{}", bucket.name(), src_object), | ||
); | ||
|
||
Self { | ||
bucket, | ||
credentials, | ||
src_object, | ||
dst_object, | ||
|
||
query, | ||
headers: Map::new(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> S3Action<'a> for CopyObject<'a> { | ||
const METHOD: Method = Method::Put; | ||
|
||
fn sign(&self, expires_in: Duration) -> Url { | ||
let now = OffsetDateTime::now_utc(); | ||
self.sign_with_time(expires_in, &now) | ||
} | ||
|
||
fn query_mut(&mut self) -> &mut Map<'a> { | ||
&mut self.query | ||
} | ||
|
||
fn headers_mut(&mut self) -> &mut Map<'a> { | ||
&mut self.headers | ||
} | ||
|
||
fn sign_with_time(&self, expires_in: Duration, time: &OffsetDateTime) -> Url { | ||
let mut url = self.bucket.object_url(self.dst_object).unwrap(); | ||
|
||
match self.credentials { | ||
Some(credentials) => sign( | ||
time, | ||
Method::Put, | ||
url, | ||
credentials.key(), | ||
credentials.secret(), | ||
credentials.token(), | ||
self.bucket.region(), | ||
expires_in.as_secs(), | ||
self.query.iter(), | ||
self.headers.iter(), | ||
), | ||
None => { | ||
url.query_pairs_mut().append_pair( | ||
"x-amz-copy-source", | ||
format!("{}/{}", self.bucket.name(), self.src_object).as_str(), | ||
); | ||
url | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use time::OffsetDateTime; | ||
|
||
use pretty_assertions::assert_eq; | ||
|
||
use super::*; | ||
use crate::{Bucket, Credentials, UrlStyle}; | ||
|
||
#[test] | ||
fn aws_example() { | ||
// Fri, 24 May 2013 00:00:00 GMT | ||
let date = OffsetDateTime::from_unix_timestamp(1369353600).unwrap(); | ||
let expires_in = Duration::from_secs(86400); | ||
|
||
let endpoint = "https://s3.amazonaws.com".parse().unwrap(); | ||
let bucket = Bucket::new( | ||
endpoint, | ||
UrlStyle::VirtualHost, | ||
"examplebucket", | ||
"us-east-1", | ||
) | ||
.unwrap(); | ||
let credentials = Credentials::new( | ||
"AKIAIOSFODNN7EXAMPLE", | ||
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", | ||
); | ||
|
||
let action = CopyObject::new(&bucket, Some(&credentials), "test.txt", "test_copy.txt"); | ||
|
||
let url = action.sign_with_time(expires_in, &date); | ||
let expected = "https://examplebucket.s3.amazonaws.com/test_copy.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIOSFODNN7EXAMPLE%2F20130524%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20130524T000000Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&x-amz-copy-source=examplebucket%2Ftest.txt&X-Amz-Signature=760326dbb90c424f6b5dcfa5f8473754f44cb4c05c173416feb1b9306dc64d35"; | ||
|
||
assert_eq!(expected, url.as_str()); | ||
} | ||
|
||
#[test] | ||
fn anonymous_custom_query() { | ||
let expires_in = Duration::from_secs(86400); | ||
|
||
let endpoint = "https://s3.amazonaws.com".parse().unwrap(); | ||
let bucket = Bucket::new( | ||
endpoint, | ||
UrlStyle::VirtualHost, | ||
"examplebucket", | ||
"us-east-1", | ||
) | ||
.unwrap(); | ||
|
||
let action = CopyObject::new(&bucket, None, "test.txt", "test_copy.txt"); | ||
let url = action.sign(expires_in); | ||
let expected = "https://examplebucket.s3.amazonaws.com/test_copy.txt?x-amz-copy-source=examplebucket%2Ftest.txt"; | ||
|
||
assert_eq!(expected, url.as_str()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::time::Duration; | ||
|
||
use rusty_s3::actions::{ListObjectsV2, S3Action}; | ||
|
||
mod common; | ||
|
||
#[tokio::test] | ||
async fn test_copy() { | ||
let (bucket, credentials, client) = common::bucket().await; | ||
|
||
let action = bucket.list_objects_v2(Some(&credentials)); | ||
let url = action.sign(Duration::from_secs(60)); | ||
let resp = client | ||
.get(url) | ||
.send() | ||
.await | ||
.expect("send ListObjectsV2") | ||
.error_for_status() | ||
.expect("ListObjectsV2 unexpected status code"); | ||
let text = resp.text().await.expect("ListObjectsV2 read respose body"); | ||
let list = ListObjectsV2::parse_response(&text).expect("ListObjectsV2 parse response"); | ||
|
||
assert!(list.contents.is_empty()); | ||
|
||
assert_eq!(list.max_keys, 4500); | ||
assert!(list.common_prefixes.is_empty()); | ||
assert!(list.next_continuation_token.is_none()); | ||
assert!(list.start_after.is_none()); | ||
|
||
let body = vec![b'r'; 1024]; | ||
|
||
let action = bucket.put_object(Some(&credentials), "test.txt"); | ||
let url = action.sign(Duration::from_secs(60)); | ||
client | ||
.put(url) | ||
.body(body.clone()) | ||
.send() | ||
.await | ||
.expect("send PutObject") | ||
.error_for_status() | ||
.expect("PutObject unexpected status code"); | ||
|
||
let action = bucket.copy_object(Some(&credentials), "test.txt", "test_copy.txt"); | ||
let url = action.sign(Duration::from_secs(60)); | ||
client | ||
.put(url) | ||
.send() | ||
.await | ||
.expect("send CopyObject") | ||
.error_for_status() | ||
.expect("CopyObject unexpected status code"); | ||
|
||
let action = bucket.list_objects_v2(Some(&credentials)); | ||
let url = action.sign(Duration::from_secs(60)); | ||
let resp = client | ||
.get(url) | ||
.send() | ||
.await | ||
.expect("send ListObjectsV2") | ||
.error_for_status() | ||
.expect("ListObjectsV2 unexpected status code"); | ||
let text = resp.text().await.expect("ListObjectsV2 read respose body"); | ||
let list = ListObjectsV2::parse_response(&text).expect("ListObjectsV2 parse response"); | ||
assert_eq!(list.contents.len(), 2); | ||
} |