forked from datenlord/s3-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs.rs
1081 lines (939 loc) · 39.3 KB
/
fs.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! fs implementation
use crate::async_trait;
use crate::data_structures::BytesStream;
use crate::dto::{
Bucket, CompleteMultipartUploadError, CompleteMultipartUploadOutput,
CompleteMultipartUploadRequest, CopyObjectError, CopyObjectOutput, CopyObjectRequest,
CopyObjectResult, CreateBucketError, CreateBucketOutput, CreateBucketRequest,
CreateMultipartUploadError, CreateMultipartUploadOutput, CreateMultipartUploadRequest,
DeleteBucketError, DeleteBucketOutput, DeleteBucketRequest, DeleteObjectError,
DeleteObjectOutput, DeleteObjectRequest, DeleteObjectsError, DeleteObjectsOutput,
DeleteObjectsRequest, DeletedObject, GetBucketLocationError, GetBucketLocationOutput,
GetBucketLocationRequest, GetObjectError, GetObjectOutput, GetObjectRequest, HeadBucketError,
HeadBucketOutput, HeadBucketRequest, HeadObjectError, HeadObjectOutput, HeadObjectRequest,
ListBucketsError, ListBucketsOutput, ListBucketsRequest, ListObjectsError, ListObjectsOutput,
ListObjectsRequest, ListObjectsV2Error, ListObjectsV2Output, ListObjectsV2Request, Object,
PutObjectError, PutObjectOutput, PutObjectRequest, UploadPartError, UploadPartOutput,
UploadPartRequest,
};
use crate::errors::{S3StorageError, S3StorageResult};
use crate::headers::{AmzCopySource, Range};
use crate::path::S3Path;
use crate::storage::S3Storage;
use crate::utils::{crypto, time, Apply};
use std::collections::{HashMap, VecDeque};
use std::convert::TryInto;
use std::io::{self, SeekFrom};
use std::path::{Path, PathBuf};
use futures::stream::{Stream, StreamExt, TryStreamExt};
use hyper::body::Bytes;
use md5::{Digest, Md5};
use path_absolutize::Absolutize;
use rusoto_s3::CommonPrefix;
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWrite, AsyncWriteExt, BufWriter};
use tracing::{debug, error};
use uuid::Uuid;
use tokio::fs;
use tokio::fs::File;
/// A S3 storage implementation based on file system
#[derive(Debug)]
pub struct FileSystem {
/// root path
root: PathBuf,
}
/// Helper function to recursively collect all files and directories
async fn collect_files_recursive(dir: &Path, files: &mut Vec<(PathBuf, bool)>) -> io::Result<()> {
let mut stack = vec![dir.to_owned()];
while let Some(current_dir) = stack.pop() {
let mut entries = fs::read_dir(¤t_dir).await?;
while let Some(entry) = entries.next_entry().await? {
let path = entry.path();
let file_type = entry.file_type().await?;
if file_type.is_dir() {
// Add the directory itself
files.push((path.clone(), true));
// Add to stack for processing
stack.push(path);
} else {
// Add regular file
files.push((path, false));
}
}
}
Ok(())
}
impl FileSystem {
/// Constructs a file system storage located at `root`
/// # Errors
/// Returns an `Err` if current working directory is invalid or `root` doesn't exist
pub fn new(root: impl AsRef<Path>) -> Result<Self, io::Error> {
let root_path = root.as_ref();
// Check if the path exists
if !root_path.exists() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("Root path does not exist: {}", root_path.display()),
));
}
// Try to canonicalize, but if it fails on WASM, use the path as-is
let root = match root_path.canonicalize() {
Ok(canonical) => canonical,
Err(e) => {
if cfg!(target_family = "wasm") || e.kind() == io::ErrorKind::Unsupported {
// In WASM or if canonicalization is unsupported, use the path as-is
root_path.to_path_buf()
} else {
// For other errors, propagate them
return Err(e);
}
}
};
debug!("File system root = {:?}", root);
Ok(Self { root })
}
/// resolve object path under the virtual root
fn get_object_path(&self, bucket: &str, key: &str) -> io::Result<PathBuf> {
let dir = Path::new(&bucket);
let file_path = Path::new(&key);
let ans = dir.join(file_path).absolutize_virtually(&self.root)?.into();
Ok(ans)
}
/// resolve bucket path under the virtual root
fn get_bucket_path(&self, bucket: &str) -> io::Result<PathBuf> {
let dir = Path::new(&bucket);
let ans = dir.absolutize_virtually(&self.root)?.into();
Ok(ans)
}
/// resolve metadata path under the virtual root (custom format)
fn get_metadata_path(&self, bucket: &str, key: &str) -> io::Result<PathBuf> {
let encode = |s: &str| base64_simd::URL_SAFE_NO_PAD.encode_to_string(s);
let file_path_str = format!(
".bucket-{}.object-{}.metadata.json",
encode(bucket),
encode(key),
);
let file_path = Path::new(&file_path_str);
let ans = file_path.absolutize_virtually(&self.root)?.into();
Ok(ans)
}
/// load metadata from fs
async fn load_metadata(
&self,
bucket: &str,
key: &str,
) -> io::Result<Option<HashMap<String, String>>> {
let path = self.get_metadata_path(bucket, key)?;
if path.exists() {
let content = fs::read(&path).await?;
let map = serde_json::from_slice(&content)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
Ok(Some(map))
} else {
Ok(None)
}
}
/// save metadata
async fn save_metadata(
&self,
bucket: &str,
key: &str,
metadata: &HashMap<String, String>,
) -> io::Result<()> {
let path = self.get_metadata_path(bucket, key)?;
let content = serde_json::to_vec(metadata)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
fs::write(&path, &content).await
}
/// get md5 sum
async fn get_md5_sum(&self, bucket: &str, key: &str) -> io::Result<String> {
let object_path = self.get_object_path(bucket, key)?;
let mut file = File::open(&object_path).await?;
let mut buf = vec![0; 4_usize.wrapping_mul(1024).wrapping_mul(1024)];
let mut md5_hash = Md5::new();
loop {
let nread = file.read(&mut buf).await?;
if nread == 0 {
break;
}
md5_hash.update(buf.get(..nread).unwrap_or_else(|| {
panic!(
"nread is larger than buffer size: nread = {}, size = {}",
nread,
buf.len()
)
}));
}
md5_hash.finalize().apply(crypto::to_hex_string).apply(Ok)
}
}
/// copy bytes from a stream to a writer
async fn copy_bytes<S, W>(mut stream: S, writer: &mut W) -> io::Result<usize>
where
S: Stream<Item = io::Result<Bytes>> + Send + Unpin,
W: AsyncWrite + Send + Unpin,
{
let mut nwrite: usize = 0;
while let Some(bytes) = stream.next().await {
let bytes = bytes?;
let amt_u64 = tokio::io::copy_buf(&mut bytes.as_ref(), writer).await?;
let amt: usize = amt_u64.try_into().unwrap_or_else(|err| {
panic!(
"number overflow: u64 to usize, n = {}, err = {}",
amt_u64, err
)
});
assert_eq!(
bytes.len(),
amt,
"amt mismatch: bytes.len() = {}, amt = {}, nwrite = {}",
bytes.len(),
amt,
nwrite
);
nwrite = nwrite
.checked_add(amt)
.unwrap_or_else(|| panic!("nwrite overflow: amt = {}, nwrite = {}", amt, nwrite));
}
writer.flush().await?;
Ok(nwrite)
}
/// wrap operation error
const fn operation_error<E>(e: E) -> S3StorageError<E> {
S3StorageError::Operation(e)
}
#[async_trait]
impl S3Storage for FileSystem {
#[tracing::instrument]
async fn create_bucket(
&self,
input: CreateBucketRequest,
) -> S3StorageResult<CreateBucketOutput, CreateBucketError> {
let path = trace_try!(self.get_bucket_path(&input.bucket));
if path.exists() {
let err = CreateBucketError::BucketAlreadyExists(String::from(
"The requested bucket name is not available. \
The bucket namespace is shared by all users of the system. \
Please select a different name and try again.",
));
return Err(operation_error(err));
}
trace_try!(fs::create_dir(&path).await);
let output = CreateBucketOutput::default(); // TODO: handle other fields
Ok(output)
}
#[tracing::instrument]
async fn copy_object(
&self,
input: CopyObjectRequest,
) -> S3StorageResult<CopyObjectOutput, CopyObjectError> {
let copy_source = AmzCopySource::from_header_str(&input.copy_source)
.map_err(|err| invalid_request!("Invalid header: x-amz-copy-source", err))?;
let (bucket, key) = match copy_source {
AmzCopySource::AccessPoint { .. } => {
return Err(not_supported!("Access point is not supported yet.").into())
}
AmzCopySource::Bucket { bucket, key } => (bucket, key),
};
let src_path = trace_try!(self.get_object_path(bucket, key));
let dst_path = trace_try!(self.get_object_path(&input.bucket, &input.key));
let file_metadata = trace_try!(fs::metadata(&src_path).await);
let last_modified = time::to_rfc3339(trace_try!(file_metadata.modified()));
let _ = trace_try!(fs::copy(&src_path, &dst_path).await);
debug!(
from = %src_path.display(),
to = %dst_path.display(),
"CopyObject: copy file",
);
let src_metadata_path = trace_try!(self.get_metadata_path(bucket, key));
if src_metadata_path.exists() {
let dst_metadata_path = trace_try!(self.get_metadata_path(&input.bucket, &input.key));
let _ = trace_try!(fs::copy(src_metadata_path, dst_metadata_path).await);
}
let md5_sum = trace_try!(self.get_md5_sum(bucket, key).await);
let output = CopyObjectOutput {
copy_object_result: CopyObjectResult {
e_tag: Some(format!("\"{}\"", md5_sum)),
last_modified: Some(last_modified),
}
.apply(Some),
..CopyObjectOutput::default()
};
Ok(output)
}
#[tracing::instrument]
async fn delete_bucket(
&self,
input: DeleteBucketRequest,
) -> S3StorageResult<DeleteBucketOutput, DeleteBucketError> {
let path = trace_try!(self.get_bucket_path(&input.bucket));
trace_try!(fs::remove_dir_all(path).await);
Ok(DeleteBucketOutput)
}
#[tracing::instrument]
async fn delete_object(
&self,
input: DeleteObjectRequest,
) -> S3StorageResult<DeleteObjectOutput, DeleteObjectError> {
let path = trace_try!(self.get_object_path(&input.bucket, &input.key));
if input.key.ends_with('/') {
let mut dir = trace_try!(fs::read_dir(&path).await);
let is_empty = dir.next_entry().await.ok().flatten().is_none();
if is_empty {
trace_try!(fs::remove_dir(&path).await);
}
} else {
trace_try!(fs::remove_file(path).await);
}
let output = DeleteObjectOutput::default(); // TODO: handle other fields
Ok(output)
}
#[tracing::instrument]
async fn delete_objects(
&self,
input: DeleteObjectsRequest,
) -> S3StorageResult<DeleteObjectsOutput, DeleteObjectsError> {
let mut objects: Vec<(PathBuf, String)> = Vec::new();
for object in input.delete.objects {
let path = trace_try!(self.get_object_path(&input.bucket, &object.key));
if path.exists() {
objects.push((path, object.key));
}
}
let mut deleted: Vec<DeletedObject> = Vec::new();
for (path, key) in objects {
trace_try!(fs::remove_file(path).await);
deleted.push(DeletedObject {
key: Some(key),
..DeletedObject::default()
});
}
let output = DeleteObjectsOutput {
deleted: Some(deleted),
..DeleteObjectsOutput::default()
};
Ok(output)
}
#[tracing::instrument]
async fn get_bucket_location(
&self,
input: GetBucketLocationRequest,
) -> S3StorageResult<GetBucketLocationOutput, GetBucketLocationError> {
let path = trace_try!(self.get_bucket_path(&input.bucket));
if !path.exists() {
let err = code_error!(NoSuchBucket, "NotFound");
return Err(err.into());
}
let output = GetBucketLocationOutput {
location_constraint: None, // TODO: handle region
};
Ok(output)
}
#[tracing::instrument]
async fn get_object(
&self,
input: GetObjectRequest,
) -> S3StorageResult<GetObjectOutput, GetObjectError> {
let object_path = trace_try!(self.get_object_path(&input.bucket, &input.key));
let parse_range = |s: &str| {
Range::from_header_str(s).map_err(|err| invalid_request!("Invalid header: range", err))
};
let range: Option<Range> = input.range.as_deref().map(parse_range).transpose()?;
let mut file = match File::open(&object_path).await {
Ok(file) => file,
Err(e) => {
error!(error = %e, "GetObject: open file");
let err = code_error!(NoSuchKey, "The specified key does not exist.");
return Err(err.into());
}
};
let file_metadata = trace_try!(file.metadata().await);
let last_modified = time::to_rfc3339(trace_try!(file_metadata.modified()));
let content_length = {
let file_len = file_metadata.len();
let content_len = match range {
None => file_len,
Some(Range::Normal { first, last }) => {
if first >= file_len {
let err =
code_error!(InvalidRange, "The requested range cannot be satisfied.");
return Err(err.into());
}
let _ = trace_try!(file.seek(SeekFrom::Start(first)).await);
// HTTP byte range is inclusive
// len = last + 1 - first
// or len = file_len - first
last.and_then(|x| x.checked_add(1))
.unwrap_or(file_len)
.wrapping_sub(first)
}
Some(Range::Suffix { last }) => {
let offset = Some(last)
.filter(|&x| x <= file_len)
.and_then(|x| i64::try_from(x).ok())
.and_then(i64::checked_neg);
if let Some(x) = offset {
let _ = trace_try!(file.seek(SeekFrom::End(x)).await);
} else {
let err =
code_error!(InvalidRange, "The requested range cannot be satisfied.");
return Err(err.into());
}
last
}
};
trace_try!(usize::try_from(content_len))
};
let stream = BytesStream::new(file, 4096, Some(content_length));
let object_metadata = trace_try!(self.load_metadata(&input.bucket, &input.key).await);
let (md5_sum, duration) = {
let (ret, duration) =
time::count_duration(self.get_md5_sum(&input.bucket, &input.key)).await;
let md5_sum = trace_try!(ret);
(md5_sum, duration)
};
debug!(
sum = ?md5_sum,
path = %object_path.display(),
size = ?content_length,
?duration,
"GetObject: calculate md5 sum",
);
let output: GetObjectOutput = GetObjectOutput {
body: Some(crate::dto::ByteStream::new(stream)),
content_length: Some(trace_try!(content_length.try_into())),
last_modified: Some(last_modified),
metadata: object_metadata,
e_tag: Some(format!("\"{}\"", md5_sum)),
..GetObjectOutput::default() // TODO: handle other fields
};
Ok(output)
}
#[tracing::instrument]
async fn head_bucket(
&self,
input: HeadBucketRequest,
) -> S3StorageResult<HeadBucketOutput, HeadBucketError> {
let path = trace_try!(self.get_bucket_path(&input.bucket));
if !path.exists() {
let err = code_error!(NoSuchBucket, "The specified bucket does not exist.");
return Err(err.into());
}
Ok(HeadBucketOutput)
}
#[tracing::instrument]
async fn head_object(
&self,
input: HeadObjectRequest,
) -> S3StorageResult<HeadObjectOutput, HeadObjectError> {
let path = trace_try!(self.get_object_path(&input.bucket, &input.key));
if !path.exists() {
let err = code_error!(NoSuchKey, "The specified key does not exist.");
return Err(err.into());
}
let file_metadata = trace_try!(fs::metadata(path).await);
let last_modified = time::to_rfc3339(trace_try!(file_metadata.modified()));
let size = file_metadata.len();
let object_metadata = trace_try!(self.load_metadata(&input.bucket, &input.key).await);
let output: HeadObjectOutput = HeadObjectOutput {
content_length: Some(trace_try!(size.try_into())),
content_type: Some(mime::APPLICATION_OCTET_STREAM.as_ref().to_owned()), // TODO: handle content type
last_modified: Some(last_modified),
metadata: object_metadata,
..HeadObjectOutput::default()
};
Ok(output)
}
#[tracing::instrument]
async fn list_buckets(
&self,
_: ListBucketsRequest,
) -> S3StorageResult<ListBucketsOutput, ListBucketsError> {
let mut buckets = Vec::new();
let mut iter = trace_try!(fs::read_dir(&self.root).await);
loop {
let entry = trace_try!(iter.next_entry().await);
if let Some(entry) = entry {
let file_type = trace_try!(entry.file_type().await);
if file_type.is_dir() {
let file_name = entry.file_name();
let name = file_name.to_string_lossy();
if S3Path::check_bucket_name(&name) {
let file_meta = trace_try!(entry.metadata().await);
let creation_date = trace_try!(file_meta.created());
buckets.push(Bucket {
creation_date: Some(time::to_rfc3339(creation_date)),
name: Some(name.into()),
});
}
}
} else {
break;
}
}
let output = ListBucketsOutput {
buckets: Some(buckets),
owner: None, // TODO: handle owner
};
Ok(output)
}
#[tracing::instrument]
async fn list_objects(
&self,
input: ListObjectsRequest,
) -> S3StorageResult<ListObjectsOutput, ListObjectsError> {
let path = trace_try!(self.get_bucket_path(&input.bucket));
let mut objects = Vec::new();
let mut common_prefixes = Vec::new();
let delimiter_is_dir = input.delimiter.as_deref() == Some("/");
let request_prefix = input.prefix.as_deref().unwrap_or("");
// This will be true if we're looking at contents of a specific directory
let is_dir_listing = request_prefix.ends_with('/');
// First, collect all files recursively
let mut all_files: Vec<(PathBuf, bool)> = Vec::new(); // (path, is_dir)
collect_files_recursive(&path, &mut all_files)
.await
.map_err(|e| {
S3StorageError::Operation(ListObjectsError::NoSuchBucket(e.to_string()))
})?;
// For each file, see if it should be included in the listing
for (file_path, is_dir) in all_files {
let relative_path = trace_try!(file_path.strip_prefix(&path));
let key_str = relative_path.to_string_lossy();
// Skip if it doesn't match the prefix
if !key_str.starts_with(request_prefix) {
continue;
}
if is_dir {
let dir_key = format!("{}/", key_str);
if delimiter_is_dir {
// Special handling for delimiter with directories
// Check if this is the directory we're listing
let is_requested_dir = is_dir_listing && dir_key == request_prefix;
// Check if this is a direct child of the directory we're listing
let is_direct_child = if is_dir_listing {
let prefix_parts =
request_prefix.split('/').filter(|p| !p.is_empty()).count();
let dir_parts = dir_key.split('/').filter(|p| !p.is_empty()).count();
dir_key.starts_with(request_prefix) && dir_parts == prefix_parts + 1
} else {
let parts = dir_key.split('/').filter(|p| !p.is_empty()).count();
parts == 1
};
if !is_requested_dir && is_direct_child {
// Add direct child directories as common prefixes
common_prefixes.push(CommonPrefix {
prefix: Some(dir_key),
});
}
}
} else {
let metadata = trace_try!(file_path.metadata());
let last_modified = time::to_rfc3339(trace_try!(metadata.modified()));
let size = metadata.len();
if delimiter_is_dir {
// With delimiter, we need to handle file paths carefully
// Check if file is in a subdirectory
if key_str.contains('/') {
if is_dir_listing {
// In a directory listing, include only direct children
let remaining_path = key_str
.strip_prefix(request_prefix)
.unwrap_or(key_str.as_ref());
if !remaining_path.contains('/') {
// Direct child of the requested directory
objects.push(Object {
e_tag: None,
key: Some(key_str.into_owned()),
last_modified: Some(last_modified),
owner: None,
size: Some(trace_try!(size.try_into())),
storage_class: None,
});
}
} else {
// Root listing, group files by their top-level directory
let top_dir = key_str.split('/').next().unwrap_or("");
let prefix = format!("{}/", top_dir);
if !common_prefixes
.iter()
.any(|cp| cp.prefix.as_deref() == Some(&prefix))
{
common_prefixes.push(CommonPrefix {
prefix: Some(prefix),
});
}
}
} else {
// File in the root level
objects.push(Object {
e_tag: None,
key: Some(key_str.into_owned()),
last_modified: Some(last_modified),
owner: None,
size: Some(trace_try!(size.try_into())),
storage_class: None,
});
}
} else {
// Without delimiter, include all files
objects.push(Object {
e_tag: None,
key: Some(key_str.into_owned()),
last_modified: Some(last_modified),
owner: None,
size: Some(trace_try!(size.try_into())),
storage_class: None,
});
}
}
}
objects.sort_by(|lhs, rhs| {
let lhs_key = lhs.key.as_deref().unwrap_or("");
let rhs_key = rhs.key.as_deref().unwrap_or("");
lhs_key.cmp(rhs_key)
});
// TODO: handle other fields
let output = ListObjectsOutput {
contents: Some(objects),
delimiter: input.delimiter,
encoding_type: input.encoding_type,
name: Some(input.bucket),
common_prefixes: if common_prefixes.is_empty() {
None
} else {
Some(common_prefixes)
},
is_truncated: None,
marker: None,
max_keys: None,
next_marker: None,
prefix: None,
};
Ok(output)
}
#[tracing::instrument]
async fn list_objects_v2(
&self,
input: ListObjectsV2Request,
) -> S3StorageResult<ListObjectsV2Output, ListObjectsV2Error> {
let path = trace_try!(self.get_bucket_path(&input.bucket));
let mut objects = Vec::new();
let mut common_prefixes = Vec::new();
let delimiter_is_dir = input.delimiter.as_deref() == Some("/");
let request_prefix = input.prefix.as_deref().unwrap_or("");
// This will be true if we're looking at contents of a specific directory
let is_dir_listing = request_prefix.ends_with('/');
// First, collect all files recursively
let mut all_files: Vec<(PathBuf, bool)> = Vec::new(); // (path, is_dir)
collect_files_recursive(&path, &mut all_files)
.await
.map_err(|e| {
S3StorageError::Operation(ListObjectsV2Error::NoSuchBucket(e.to_string()))
})?;
// For each file, see if it should be included in the listing
for (file_path, is_dir) in all_files {
let relative_path = trace_try!(file_path.strip_prefix(&path));
let key_str = relative_path.to_string_lossy();
// Skip if it doesn't match the prefix
if !key_str.starts_with(request_prefix) {
continue;
}
if is_dir {
let dir_key = format!("{}/", key_str);
if delimiter_is_dir {
// Special handling for delimiter with directories
// Check if this is the directory we're listing
let is_requested_dir = is_dir_listing && dir_key == request_prefix;
// Check if this is a direct child of the directory we're listing
let is_direct_child = if is_dir_listing {
let prefix_parts =
request_prefix.split('/').filter(|p| !p.is_empty()).count();
let dir_parts = dir_key.split('/').filter(|p| !p.is_empty()).count();
dir_key.starts_with(request_prefix) && dir_parts == prefix_parts + 1
} else {
let parts = dir_key.split('/').filter(|p| !p.is_empty()).count();
parts == 1
};
if !is_requested_dir && is_direct_child {
// Add direct child directories as common prefixes
common_prefixes.push(CommonPrefix {
prefix: Some(dir_key),
});
}
}
} else {
let metadata = trace_try!(file_path.metadata());
let last_modified = time::to_rfc3339(trace_try!(metadata.modified()));
let size = metadata.len();
if delimiter_is_dir {
// With delimiter, we need to handle file paths carefully
// Check if file is in a subdirectory
if key_str.contains('/') {
if is_dir_listing {
// In a directory listing, include only direct children
let remaining_path = key_str
.strip_prefix(request_prefix)
.unwrap_or(key_str.as_ref());
if !remaining_path.contains('/') {
// Direct child of the requested directory
objects.push(Object {
e_tag: None,
key: Some(key_str.into_owned()),
last_modified: Some(last_modified),
owner: None,
size: Some(trace_try!(size.try_into())),
storage_class: None,
});
}
} else {
// Root listing, group files by their top-level directory
let top_dir = key_str.split('/').next().unwrap_or("");
let prefix = format!("{}/", top_dir);
if !common_prefixes
.iter()
.any(|cp| cp.prefix.as_deref() == Some(&prefix))
{
common_prefixes.push(CommonPrefix {
prefix: Some(prefix),
});
}
}
} else {
// File in the root level
objects.push(Object {
e_tag: None,
key: Some(key_str.into_owned()),
last_modified: Some(last_modified),
owner: None,
size: Some(trace_try!(size.try_into())),
storage_class: None,
});
}
} else {
// Without delimiter, include all files
objects.push(Object {
e_tag: None,
key: Some(key_str.into_owned()),
last_modified: Some(last_modified),
owner: None,
size: Some(trace_try!(size.try_into())),
storage_class: None,
});
}
}
}
objects.sort_by(|lhs, rhs| {
let lhs_key = lhs.key.as_deref().unwrap_or("");
let rhs_key = rhs.key.as_deref().unwrap_or("");
lhs_key.cmp(rhs_key)
});
// TODO: handle other fields
let output = ListObjectsV2Output {
key_count: Some(trace_try!(objects.len().try_into())),
contents: Some(objects),
delimiter: input.delimiter,
encoding_type: input.encoding_type,
name: Some(input.bucket),
common_prefixes: if common_prefixes.is_empty() {
None
} else {
Some(common_prefixes)
},
is_truncated: None,
max_keys: None,
prefix: None,
continuation_token: None,
next_continuation_token: None,
start_after: None,
};
Ok(output)
}
#[tracing::instrument]
async fn put_object(
&self,
input: PutObjectRequest,
) -> S3StorageResult<PutObjectOutput, PutObjectError> {
if let Some(ref storage_class) = input.storage_class {
let is_valid = ["STANDARD", "REDUCED_REDUNDANCY"].contains(&storage_class.as_str());
if !is_valid {
let err = code_error!(
InvalidStorageClass,
"The storage class you specified is not valid."
);
return Err(err.into());
}
}
let PutObjectRequest {
body,
bucket,
key,
metadata,
content_length,
..
} = input;
let body = body.ok_or_else(||{
code_error!(IncompleteBody,"You did not provide the number of bytes specified by the Content-Length HTTP header.")
})?;
if key.ends_with('/') {
if content_length == Some(0) {
let object_path = trace_try!(self.get_object_path(&bucket, &key));
trace_try!(fs::create_dir_all(&object_path).await);
let output = PutObjectOutput::default();
return Ok(output);
}
let err = code_error!(
UnexpectedContent,
"Unexpected request body when creating a directory object."
);
return Err(err.into());
}
let object_path = trace_try!(self.get_object_path(&bucket, &key));
if let Some(dir_path) = object_path.parent() {
trace_try!(fs::create_dir_all(&dir_path).await);
}
let mut md5_hash = Md5::new();
let stream = body.inspect_ok(|bytes| md5_hash.update(bytes.as_ref()));
let file = trace_try!(File::create(&object_path).await);
let mut writer = BufWriter::new(file);
let (ret, duration) = time::count_duration(copy_bytes(stream, &mut writer)).await;
let size = trace_try!(ret);
let md5_sum = md5_hash.finalize().apply(crypto::to_hex_string);
debug!(
path = %object_path.display(),
?size,
?duration,
%md5_sum,
"PutObject: write file",
);
if let Some(ref metadata) = metadata {
trace_try!(self.save_metadata(&bucket, &key, metadata).await);
}
let output = PutObjectOutput {
e_tag: Some(format!("\"{}\"", md5_sum)),
..PutObjectOutput::default()
}; // TODO: handle other fields
Ok(output)
}
#[tracing::instrument]
async fn create_multipart_upload(
&self,
input: CreateMultipartUploadRequest,
) -> S3StorageResult<CreateMultipartUploadOutput, CreateMultipartUploadError> {
let upload_id = Uuid::new_v4().to_string();
let output = CreateMultipartUploadOutput {
bucket: Some(input.bucket),
key: Some(input.key),
upload_id: Some(upload_id),
..CreateMultipartUploadOutput::default()
};
Ok(output)
}
#[tracing::instrument]
async fn upload_part(
&self,
input: UploadPartRequest,
) -> S3StorageResult<UploadPartOutput, UploadPartError> {
let UploadPartRequest {
body,
upload_id,
part_number,
..
} = input;
let body = body.ok_or_else(||{
code_error!(IncompleteBody, "You did not provide the number of bytes specified by the Content-Length HTTP header.")
})?;
let file_path_str = format!(".upload_id-{}.part-{}", upload_id, part_number);
let file_path = trace_try!(Path::new(&file_path_str).absolutize_virtually(&self.root));
let mut md5_hash = Md5::new();
let stream = body.inspect_ok(|bytes| md5_hash.update(bytes.as_ref()));
let file = trace_try!(File::create(&file_path).await);
let mut writer = BufWriter::new(file);
let (ret, duration) = time::count_duration(copy_bytes(stream, &mut writer)).await;
let size = trace_try!(ret);
let md5_sum = md5_hash.finalize().apply(crypto::to_hex_string);
debug!(
path = %file_path.display(),
?size,
?duration,
%md5_sum,
"UploadPart: write file",
);
let e_tag = format!("\"{}\"", md5_sum);
let output = UploadPartOutput {
e_tag: Some(e_tag),
..UploadPartOutput::default()
};
Ok(output)
}