2
2
DeleteObjectCommand ,
3
3
GetObjectCommand ,
4
4
HeadObjectCommand ,
5
+ NotFound ,
5
6
PutObjectCommand ,
6
7
S3Client ,
7
8
} from "@aws-sdk/client-s3" ;
@@ -36,12 +37,21 @@ export const exists = async (
36
37
path : string ,
37
38
) : Promise < boolean > => {
38
39
const client = new S3Client ( bucket . connection ) ;
39
- const res = await client . send (
40
- new HeadObjectCommand ( {
41
- Bucket : bucket . name ,
42
- Key : path ,
43
- } ) ,
44
- ) ;
40
+ const res = await client
41
+ . send (
42
+ new HeadObjectCommand ( {
43
+ Bucket : bucket . name ,
44
+ Key : path ,
45
+ } ) ,
46
+ )
47
+ . catch ( ( err ) => {
48
+ if ( err instanceof NotFound ) {
49
+ return {
50
+ ContentLength : undefined ,
51
+ } ;
52
+ }
53
+ throw err ;
54
+ } ) ;
45
55
46
56
return res . ContentLength !== undefined ;
47
57
} ;
@@ -51,24 +61,36 @@ export const get = async (
51
61
path : string ,
52
62
) : Promise < Buffer | null > => {
53
63
const client = new S3Client ( bucket . connection ) ;
54
- const res = await client . send (
55
- new GetObjectCommand ( {
56
- Bucket : bucket . name ,
57
- Key : path ,
58
- } ) ,
59
- ) ;
64
+ const res = await client
65
+ . send (
66
+ new GetObjectCommand ( {
67
+ Bucket : bucket . name ,
68
+ Key : path ,
69
+ } ) ,
70
+ )
71
+ . catch ( ( err ) => {
72
+ if ( err instanceof NotFound ) {
73
+ return null ;
74
+ }
75
+ throw err ;
76
+ } ) ;
60
77
61
- return res . Body ? Buffer . from ( await res . Body . transformToByteArray ( ) ) : null ;
78
+ return res ? .Body ? Buffer . from ( await res . Body . transformToByteArray ( ) ) : null ;
62
79
} ;
63
80
64
81
export const deleteFile = async (
65
82
bucket : Bucket ,
66
83
path : string ,
67
84
) : Promise < void > => {
68
85
const client = new S3Client ( bucket . connection ) ;
69
- await client . send (
70
- new DeleteObjectCommand ( { Bucket : bucket . name , Key : path } ) ,
71
- ) ;
86
+ await client
87
+ . send ( new DeleteObjectCommand ( { Bucket : bucket . name , Key : path } ) )
88
+ . catch ( ( err ) => {
89
+ if ( err instanceof NotFound ) {
90
+ return ;
91
+ }
92
+ throw err ;
93
+ } ) ;
72
94
} ;
73
95
74
96
export const getStreamed = async (
0 commit comments