Skip to content

Commit b1bb404

Browse files
committed
feat(S3): list all objects uploaded to S3
1 parent 0aa1709 commit b1bb404

File tree

6 files changed

+39
-13
lines changed

6 files changed

+39
-13
lines changed

src/api/modules/v1/components/AWS/s3.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
CreateBucketRequest,
55
PutObjectOutput,
66
GetObjectOutput,
7-
ListObjectsOutput,
7+
_Object,
88
S3,
99
} from "$deps";
1010
import {
@@ -72,19 +72,19 @@ export class SimpleCloudStorage {
7272
}
7373
}
7474

75-
public async listObjects(): Promise<ListObjectsOutput | undefined> {
75+
public async listObjects() {
7676
return this.handleListObjects();
7777
}
7878

79-
private async handleListObjects() {
79+
private async handleListObjects(): Promise<Array<_Object> | undefined> {
8080
const s3 = this.makeNewBucket();
8181

8282
try {
83-
const listObjects = await s3.listObjects({
83+
const listObjects = await s3.listObjectsV2({
8484
Bucket: this.bucketName,
8585
});
8686

87-
return listObjects;
87+
return listObjects.Contents;
8888
} catch (er) {
8989
log.error(er.message);
9090
}

src/api/modules/v1/controllers/files/FileController.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ class Files implements IFileController {
5252
log.error(er.message);
5353
}
5454
}
55+
56+
public async listAllObjectsBuckets(ctx: RouterContext<string>) {
57+
try {
58+
ctx.response.status = Status.OK;
59+
return ctx.response.body = await FileService.listAllObjectsFromBucket()
60+
} catch (er) {
61+
ctx.response.status = Status.BadRequest;
62+
log.error(er.message);
63+
}
64+
}
5565
}
5666

5767
export default new Files();

src/api/modules/v1/repository/File/FileRespository.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class FileRepository extends File {
4646
return this.mysql.buildQuery(
4747
`
4848
SELECT
49+
id,
4950
name,
5051
status
5152
FROM ${this.table}
@@ -54,17 +55,17 @@ class FileRepository extends File {
5455
);
5556
}
5657

57-
public updatedAfterListenAll() {
58-
return this.updateStatusAfterListenAll();
58+
public updatedAfterListenAll(id: string) {
59+
return this.updateStatusAfterListenAll(id);
5960
}
6061

61-
private updateStatusAfterListenAll() {
62+
private updateStatusAfterListenAll(id: string) {
6263
return this.mysql.buildQuery(
6364
`
6465
UPDATE ${this.table}
6566
SET status = ?
66-
WHERE status = ?;
67-
`, [FilenameEnum.LAUNCHED, FilenameEnum.PENDING]
67+
WHERE id = ?;
68+
`, [FilenameEnum.LAUNCHED, id]
6869
);
6970
}
7071
}

src/api/modules/v1/routes/file.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ const file = new Router();
66
file.post("uploadFile/", FileController.uploadCsv);
77
file.get("listAllFiles/", FileController.listAllFiles);
88
file.get("listenAllFiles/", FileController.listenFiles)
9+
file.get("listAllObjectsBuckets/", FileController.listAllObjectsBuckets)
910

1011
export { file };

src/api/modules/v1/services/file/FileService.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FormDataFile } from "$deps";
1+
import { FormDataFile, _Object } from "$deps";
22
import { S3 } from "$components";
33
import {
44
FilenameEnum,
@@ -69,15 +69,28 @@ export class FileService implements IFileService {
6969
);
7070

7171
for (const pending of pendings) {
72-
if (pending.status === 0) {
72+
if (pending.status === FilenameEnum.PENDING) {
7373
this.personService.listenAndCreatePerson(pending.name);
74-
await this.fileRepository.updatedAfterListenAll();
74+
await this.fileRepository.updatedAfterListenAll(pending.id)
7575
continue;
7676
};
7777
}
7878

7979
return files;
8080
}
81+
82+
public async listAllObjectsFromBucket() {
83+
const objects = await this.s3.listObjects() as Array<_Object>;
84+
85+
const objectsFiltered = objects.map(object => {
86+
return {
87+
name: object.Key,
88+
size: object.Size,
89+
}
90+
});
91+
92+
return objectsFiltered;
93+
}
8194
}
8295

8396
export default new FileService()

src/deps/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export {
2626
type CreateBucketRequest,
2727
type PutObjectOutput,
2828
type ListObjectsOutput,
29+
type _Object,
2930
} from "https://deno.land/x/[email protected]/services/s3/structs.ts";
3031

3132
export {

0 commit comments

Comments
 (0)