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
use std::collections::HashMap; use std::mem; use serde_xml; use credentials::Credentials; use command::Command; use region::Region; use request::{Request, Headers, Query}; use serde_types::ListBucketResult; use error::S3Result; /// # Example /// ``` /// # // Fake credentials so we don't access user's real credentials in tests /// # use std::env; /// # env::set_var("AWS_ACCESS_KEY_ID", "AKIAIOSFODNN7EXAMPLE"); /// # env::set_var("AWS_SECRET_ACCESS_KEY", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); /// use s3::bucket::Bucket; /// use s3::credentials::Credentials; /// /// let bucket_name = "rust-s3-test"; /// let region = "us-east-1".parse().unwrap(); /// let credentials = Credentials::default(); /// /// let bucket = Bucket::new(bucket_name, region, credentials); /// ``` #[derive(Clone, Debug, Eq, PartialEq)] pub struct Bucket { pub name: String, pub region: Region, pub credentials: Credentials, pub extra_headers: Headers, pub extra_query: Query, } impl Bucket { /// Instantiate a new `Bucket`. /// /// # Example /// ``` /// # // Fake credentials so we don't access user's real credentials in tests /// # use std::env; /// # env::set_var("AWS_ACCESS_KEY_ID", "AKIAIOSFODNN7EXAMPLE"); /// # env::set_var("AWS_SECRET_ACCESS_KEY", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"); /// use s3::bucket::Bucket; /// use s3::credentials::Credentials; /// /// let bucket_name = "rust-s3-test"; /// let region = "us-east-1".parse().unwrap(); /// let credentials = Credentials::default(); /// /// let bucket = Bucket::new(bucket_name, region, credentials); /// ``` pub fn new(name: &str, region: Region, credentials: Credentials) -> Bucket { Bucket { name: name.into(), region, credentials, extra_headers: HashMap::new(), extra_query: HashMap::new(), } } /// Gets file from an S3 path. /// /// # Example: /// /// ```rust,no_run /// use s3::bucket::Bucket; /// use s3::credentials::Credentials; /// /// let bucket_name = "rust-s3-test"; /// let region = "us-east-1".parse().unwrap(); /// let credentials = Credentials::default(); /// let bucket = Bucket::new(bucket_name, region, credentials); /// /// let (data, code) = bucket.get("/test.file").unwrap(); /// println!("Code: {}\nData: {:?}", code, data); /// ``` pub fn get(&self, path: &str) -> S3Result<(Vec<u8>, u32)> { let command = Command::Get; let request = Request::new(self, path, command); request.execute() } /// Delete file from an S3 path. /// /// # Example: /// /// ```rust,no_run /// use s3::bucket::Bucket; /// use s3::credentials::Credentials; /// /// let bucket_name = &"rust-s3-test"; /// let region = "us-east-1".parse().unwrap(); /// let credentials = Credentials::default(); /// let bucket = Bucket::new(bucket_name, region, credentials); /// /// let (_, code) = bucket.delete("/test.file").unwrap(); /// assert_eq!(204, code); /// ``` pub fn delete(&self, path: &str) -> S3Result<(Vec<u8>, u32)> { let command = Command::Delete; let request = Request::new(self, path, command); request.execute() } /// Put into an S3 bucket. /// /// # Example /// /// ```rust,no_run /// use s3::bucket::Bucket; /// use s3::credentials::Credentials; /// /// let bucket_name = &"rust-s3-test"; /// let aws_access = &"access_key"; /// let aws_secret = &"secret_key"; /// /// let bucket_name = &"rust-s3-test"; /// let region = "us-east-1".parse().unwrap(); /// let credentials = Credentials::default(); /// let bucket = Bucket::new(bucket_name, region, credentials); /// /// let content = "I want to go to S3".as_bytes(); /// let (_, code) = bucket.put("/test.file", content, "text/plain").unwrap(); /// assert_eq!(201, code); /// ``` pub fn put(&self, path: &str, content: &[u8], content_type: &str) -> S3Result<(Vec<u8>, u32)> { let command = Command::Put { content, content_type, }; let request = Request::new(self, path, command); request.execute() } fn _list(&self, prefix: &str, delimiter: Option<&str>, continuation_token: Option<&str>) -> S3Result<(ListBucketResult, u32)> { let command = Command::List { prefix, delimiter, continuation_token, }; let request = Request::new(self, "/", command); let result = request.execute()?; let result_string = String::from_utf8_lossy(&result.0); let deserialized: ListBucketResult = serde_xml::deserialize(result_string.as_bytes())?; Ok((deserialized, result.1)) } /// List the contents of an S3 bucket. /// /// # Example /// /// ```rust,no_run /// use std::str; /// use s3::bucket::Bucket; /// use s3::credentials::Credentials; /// let bucket_name = &"rust-s3-test"; /// let aws_access = &"access_key"; /// let aws_secret = &"secret_key"; /// /// let bucket_name = &"rust-s3-test"; /// let region = "us-east-1".parse().unwrap(); /// let credentials = Credentials::default(); /// let bucket = Bucket::new(bucket_name, region, credentials); /// /// let results = bucket.list("/", Some("/")).unwrap(); /// for (list, code) in results { /// assert_eq!(200, code); /// println!("{:?}", list); /// } /// ``` pub fn list(&self, prefix: &str, delimiter: Option<&str>) -> S3Result<Vec<(ListBucketResult, u32)>> { let mut results = Vec::new(); let mut result = self._list(prefix, delimiter, None)?; loop { results.push(result.clone()); match result.0.next_continuation_token { Some(token) => result = self._list(prefix, delimiter, Some(&token))?, None => break, } } Ok(results) } /// Get a reference to the name of the S3 bucket. pub fn name(&self) -> &str { &self.name } /// Get a reference to the hostname of the S3 API endpoint. pub fn host(&self) -> &str { self.region.host() } pub fn scheme(&self) -> &str { self.region.scheme() } /// Get the region this object will connect to. pub fn region(&self) -> Region { self.region.clone() } /// Get a reference to the AWS access key. pub fn access_key(&self) -> &str { &self.credentials.access_key } /// Get a reference to the AWS secret key. pub fn secret_key(&self) -> &str { &self.credentials.secret_key } /// Get a reference to the AWS token. pub fn token(&self) -> Option<&str> { self.credentials.token.as_ref().map(|s| s.as_str()) } /// Get a reference to the full [`Credentials`](struct.Credentials.html) /// object used by this `Bucket`. pub fn credentials(&self) -> &Credentials { &self.credentials } /// Change the credentials used by the Bucket, returning the existing /// credentials. pub fn set_credentials(&mut self, credentials: Credentials) -> Credentials { mem::replace(&mut self.credentials, credentials) } /// Add an extra header to send with requests to S3. /// /// Add an extra header to send with requests. Note that the library /// already sets a number of headers - headers set with this method will be /// overridden by the library headers: /// * Host /// * Content-Type /// * Date /// * Content-Length /// * Authorization /// * X-Amz-Content-Sha256 /// * X-Amz-Date pub fn add_header(&mut self, key: &str, value: &str) { self.extra_headers.insert(key.into(), value.into()); } /// Get a reference to the extra headers to be passed to the S3 API. pub fn extra_headers(&self) -> &Headers { &self.extra_headers } /// Get a mutable reference to the extra headers to be passed to the S3 /// API. pub fn extra_headers_mut(&mut self) -> &mut Headers { &mut self.extra_headers } /// Add an extra query pair to the URL used for S3 API access. pub fn add_query(&mut self, key: &str, value: &str) { self.extra_query.insert(key.into(), value.into()); } /// Get a reference to the extra query pairs to be passed to the S3 API. pub fn extra_query(&self) -> &Query { &self.extra_query } /// Get a mutable reference to the extra query pairs to be passed to the S3 /// API. pub fn extra_query_mut(&mut self) -> &mut Query { &mut self.extra_query } }