-
Notifications
You must be signed in to change notification settings - Fork 243
/
s3-multipart-upload.rs
169 lines (150 loc) · 5.66 KB
/
s3-multipart-upload.rs
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#![allow(clippy::result_large_err)]
// snippet-start:[rust.example_code.s3.large_files.scenario]
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use aws_config::meta::region::RegionProviderChain;
use aws_sdk_s3::error::DisplayErrorContext;
use aws_sdk_s3::operation::{
create_multipart_upload::CreateMultipartUploadOutput, get_object::GetObjectOutput,
};
use aws_sdk_s3::types::{CompletedMultipartUpload, CompletedPart};
use aws_sdk_s3::{config::Region, Client as S3Client};
use aws_smithy_types::byte_stream::{ByteStream, Length};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use s3_service::error::Error;
use std::process;
use uuid::Uuid;
//In bytes, minimum chunk size of 5MB. Increase CHUNK_SIZE to send larger chunks.
const CHUNK_SIZE: u64 = 1024 * 1024 * 5;
const MAX_CHUNKS: u64 = 10000;
#[tokio::main]
pub async fn main() {
if let Err(err) = run_example().await {
eprintln!("Error: {}", DisplayErrorContext(err));
process::exit(1);
}
}
async fn run_example() -> Result<(), Error> {
let shared_config = aws_config::load_from_env().await;
let client = S3Client::new(&shared_config);
let bucket_name = format!("doc-example-bucket-{}", Uuid::new_v4());
let region_provider = RegionProviderChain::first_try(Region::new("us-west-2"));
let region = region_provider.region().await.unwrap();
s3_service::create_bucket(&client, &bucket_name, region.as_ref()).await?;
let key = "sample.txt".to_string();
// snippet-start:[rust.example_code.s3.create_multipart_upload]
let multipart_upload_res: CreateMultipartUploadOutput = client
.create_multipart_upload()
.bucket(&bucket_name)
.key(&key)
.send()
.await
.unwrap();
// snippet-end:[rust.example_code.s3.create_multipart_upload]
let upload_id = multipart_upload_res.upload_id().unwrap();
//Create a file of random characters for the upload.
let mut file = File::create(&key).expect("Could not create sample file.");
// Loop until the file is 5 chunks.
while file.metadata().unwrap().len() <= CHUNK_SIZE * 4 {
let rand_string: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(256)
.map(char::from)
.collect();
let return_string: String = "\n".to_string();
file.write_all(rand_string.as_ref())
.expect("Error writing to file.");
file.write_all(return_string.as_ref())
.expect("Error writing to file.");
}
let path = Path::new(&key);
let file_size = tokio::fs::metadata(path)
.await
.expect("it exists I swear")
.len();
let mut chunk_count = (file_size / CHUNK_SIZE) + 1;
let mut size_of_last_chunk = file_size % CHUNK_SIZE;
if size_of_last_chunk == 0 {
size_of_last_chunk = CHUNK_SIZE;
chunk_count -= 1;
}
if file_size == 0 {
panic!("Bad file size.");
}
if chunk_count > MAX_CHUNKS {
panic!("Too many chunks! Try increasing your chunk size.")
}
let mut upload_parts: Vec<CompletedPart> = Vec::new();
for chunk_index in 0..chunk_count {
let this_chunk = if chunk_count - 1 == chunk_index {
size_of_last_chunk
} else {
CHUNK_SIZE
};
let stream = ByteStream::read_from()
.path(path)
.offset(chunk_index * CHUNK_SIZE)
.length(Length::Exact(this_chunk))
.build()
.await
.unwrap();
//Chunk index needs to start at 0, but part numbers start at 1.
let part_number = (chunk_index as i32) + 1;
// snippet-start:[rust.example_code.s3.upload_part]
let upload_part_res = client
.upload_part()
.key(&key)
.bucket(&bucket_name)
.upload_id(upload_id)
.body(stream)
.part_number(part_number)
.send()
.await?;
upload_parts.push(
CompletedPart::builder()
.e_tag(upload_part_res.e_tag.unwrap_or_default())
.part_number(part_number)
.build(),
);
// snippet-end:[rust.example_code.s3.upload_part]
}
// snippet-start:[rust.example_code.s3.upload_part.CompletedMultipartUpload]
let completed_multipart_upload: CompletedMultipartUpload = CompletedMultipartUpload::builder()
.set_parts(Some(upload_parts))
.build();
// snippet-end:[rust.example_code.s3.upload_part.CompletedMultipartUpload]
// snippet-start:[rust.example_code.s3.complete_multipart_upload]
let _complete_multipart_upload_res = client
.complete_multipart_upload()
.bucket(&bucket_name)
.key(&key)
.multipart_upload(completed_multipart_upload)
.upload_id(upload_id)
.send()
.await
.unwrap();
// snippet-end:[rust.example_code.s3.complete_multipart_upload]
let data: GetObjectOutput = s3_service::download_object(&client, &bucket_name, &key).await?;
let data_length: u64 = data
.content_length()
.unwrap_or_default()
.try_into()
.unwrap();
if file.metadata().unwrap().len() == data_length {
println!("Data lengths match.");
} else {
println!("The data was not the same size!");
}
s3_service::delete_objects(&client, &bucket_name)
.await
.expect("Error emptying bucket.");
s3_service::delete_bucket(&client, &bucket_name)
.await
.expect("Error deleting bucket.");
Ok(())
}
// snippet-end:[rust.example_code.s3.large_files.scenario]