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
use crate::*;
use Rng;
use Path;
use Arc;
use TempDir;
use TimeoutState;
use AsyncReadExt;
use Mutex;
/*
/// Timeout implementation used for testing
struct TimeoutState;
impl Timeout for TimeoutState {
fn get_timeout(&self, _bytes: usize, _attempts: usize) -> Duration {
Duration::from_secs(4)
}
fn update(&mut self, _: &RequestReport) {}
fn get_estimate(&self) -> f64 {
0.0
}
}
*/
pub
async
async
/// Returns the common prefix of all files in S3
async
// TODO uncomment after rewriting move_all function ETC
/*
#[tokio::test]
async fn test_move_files() {
const N_FILES: usize = 100;
let s3 = testing_sdk_client().await;
let algo = S3Algo::new(s3.clone());
let tmp_dir = TempDir::new("s3-testing").unwrap();
let prefix = upload_test_files(algo.clone(), tmp_dir.path(), N_FILES)
.await
.unwrap();
let new_prefix = PathBuf::from("haha/lala");
println!(
"Move prefix {} to {}",
prefix.display(),
new_prefix.display()
);
// TODO try also the following more manual way of doing the same
/*
algo.list_prefix("test-bucket".into(), format!("{}", prefix.display()))
.boxed() // hope we can remove boxed() soon (it's for reducing type size)
.move_all(
move |key| {
let key = PathBuf::from(key);
let name = key.file_name().unwrap();
format!("{}/{}", new_prefix2.display(), name.to_str().unwrap())
},
None,
)
.await
.unwrap();
*/
algo.list_prefix("test-bucket".into(), prefix.to_str().map(|x| x.to_owned()))
.boxed() // hope we can remove boxed() soon (it's for reducing type size)
.move_to_prefix(
None,
new_prefix.to_str().unwrap().to_owned(),
Default::default,
)
.boxed()
.await
.unwrap();
// Check that all files are under `new_prefix` and not under `prefix`
for i in 0..N_FILES {
let key = new_prefix.join(format!("img_{}.tif", i));
let response = s3.get_object(GetObjectRequest {
bucket: "test-bucket".to_string(),
key: key.to_str().unwrap().to_string(),
..Default::default()
});
let _ = response.await.unwrap();
let key = prefix.join(format!("img_{}.tif", i));
let response = s3.get_object(GetObjectRequest {
bucket: "test-bucket".to_string(),
key: key.to_str().unwrap().to_string(),
..Default::default()
});
let _ = response.await.unwrap_err();
}
}
*/
// TODO: uncomment after rewriting copy_all function
/*
#[tokio::test]
async fn test_copy_files() {
const N_FILES: usize = 100;
let s3 = testing_s3_client();
let algo = S3Algo::new(s3.clone());
let tmp_dir = TempDir::new("s3-testing").unwrap();
let prefix = upload_test_files(algo.clone(), tmp_dir.path(), N_FILES)
.await
.unwrap();
let n = Arc::new(std::sync::Mutex::new(0_usize));
let m = n.clone();
algo.list_prefix("test-bucket".into(), prefix.to_str().unwrap().to_owned())
.boxed() // hope we can remove boxed() soon (it's for reducing type size)
.copy_all(
Some("test-bucket2".into()),
move |key| {
*m.lock().unwrap() += 1;
format!("test_copy_files/{}", key)
},
Default::default,
)
.boxed()
.await
.unwrap();
assert_eq!(*n.lock().unwrap(), N_FILES);
// Check that all objects are present in both buckets
for i in 0..N_FILES {
let key = format!("test_copy_files/{}/img_{}.tif", prefix.display(), i);
let response = s3.get_object(GetObjectRequest {
bucket: "test-bucket2".to_string(),
key,
..Default::default()
});
let _ = response.await.unwrap();
let key = prefix.join(format!("img_{}.tif", i));
let response = s3.get_object(GetObjectRequest {
bucket: "test-bucket".to_string(),
key: key.to_str().unwrap().to_string(),
..Default::default()
});
let _ = response.await.unwrap();
}
}
*/