Skip to content

Commit deee23f

Browse files
committed
Added listFiles function
1 parent 2c39ed0 commit deee23f

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

src/ErrorThrower.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IConfig } from './types';
22

3-
export const throwError = (config: IConfig, file: File) => {
3+
export const throwError = (config: IConfig) => {
44
if (config.bucketName === null || config.bucketName === '') {
55
throw new Error(`Your bucketName cannot be empty `);
66
}
@@ -13,7 +13,12 @@ export const throwError = (config: IConfig, file: File) => {
1313
if (config.secretAccessKey === null || config.secretAccessKey === '') {
1414
throw new Error(`Must provide secretAccessKey`);
1515
}
16+
};
17+
18+
export const throwUploadError = (config: IConfig, file: File) => {
19+
throwError(config);
20+
1621
if (!file) {
1722
throw new Error(`File cannot be empty`);
1823
}
19-
};
24+
}

src/react-aws-s3.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import shortId from 'short-uuid';
22
import { dateYMD, xAmzDate } from './Date';
3-
import { IConfig, DeleteResponse, UploadResponse } from './types';
4-
import { throwError } from './ErrorThrower';
3+
import { IConfig, ListFileErrorResponse, ListFileResponse, UploadResponse } from './types';
4+
import { throwUploadError } from './ErrorThrower';
55
import GetUrl from './Url';
66
import Policy from './Policy';
77
import Signature from './Signature';
@@ -13,7 +13,7 @@ class ReactS3Client {
1313
this.config = config;
1414
}
1515
public async uploadFile(file: File, newFileName?: string): Promise<UploadResponse> {
16-
throwError(this.config, file);
16+
throwUploadError(this.config, file);
1717
let fileExtension: string;
1818
const fd = new FormData();
1919

@@ -78,6 +78,53 @@ class ReactS3Client {
7878
},
7979
);
8080
}
81+
82+
public async listFiles() {
83+
const awsConfig = (({ region, accessKeyId, secretAccessKey }) => ({ region, accessKeyId, secretAccessKey }))(
84+
this.config,
85+
);
86+
AWS.config.update(awsConfig);
87+
88+
const s3 = new AWS.S3({
89+
apiVersion: '2006-03-01',
90+
params: {
91+
Bucket: this.config.bucketName,
92+
},
93+
});
94+
95+
try {
96+
const req = await s3.listObjects({
97+
Bucket: this.config.bucketName,
98+
}).promise();
99+
100+
if (req.$response.error) {
101+
return Promise.reject<ListFileErrorResponse>({
102+
err: req.$response.error.name,
103+
errMessage: req.$response.error.message,
104+
data: req.$response.error
105+
});
106+
}
107+
108+
if (!req.$response.data) {
109+
return Promise.reject<ListFileErrorResponse>({
110+
err: 'Something went wrong!',
111+
errMessage: 'Unknown error occured. Please try again',
112+
data: null
113+
});
114+
}
115+
116+
return Promise.resolve<ListFileResponse>({
117+
message: 'Objects listed succesfully',
118+
data: req.$response.data
119+
});
120+
} catch (err) {
121+
return Promise.reject<ListFileErrorResponse>({
122+
err: 'Something went wrong!',
123+
errMessage: 'Unknown error occured. Please try again',
124+
data: err
125+
});
126+
}
127+
}
81128
}
82129

83130
export default ReactS3Client;

src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,14 @@ export type DeleteResponse = {
4646
message: string;
4747
fileName: string;
4848
};
49+
50+
export type ListFileResponse = {
51+
message: string;
52+
data: AWS.S3.ListObjectsOutput
53+
};
54+
55+
export type ListFileErrorResponse = {
56+
err: string;
57+
errMessage: string;
58+
data: any;
59+
};

0 commit comments

Comments
 (0)