Skip to content

Commit 0efc313

Browse files
committed
feat: create Files service for s3 uploads
1 parent 9ac1a7e commit 0efc313

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

services/files/files.class.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Application } from '@feathersjs/express';
2+
import { Params } from '@feathersjs/feathers';
3+
import { S3 } from 'aws-sdk';
4+
import { v4 as uuidv4 } from 'uuid';
5+
6+
7+
export default class Files {
8+
app!: Application;
9+
s3!: S3;
10+
bucket!: string;
11+
12+
async find(params: Params): Promise<string> {
13+
// Return signed upload URL
14+
return this.s3.getSignedUrl('putObject', {
15+
Bucket: this.bucket,
16+
Key: `${params.user?.username}/${uuidv4()}.png`,
17+
ContentType: 'image/*',
18+
Expires: 300,
19+
});
20+
}
21+
22+
setup(app: Application): void {
23+
this.app = app;
24+
this.s3 = new S3({
25+
accessKeyId: process.env.ACCESSKEYID,
26+
secretAccessKey: process.env.SECRETACCESSKEY,
27+
signatureVersion: 'v4',
28+
region: 'eu-central-1'
29+
});
30+
this.bucket = process.env.BUCKET || '';
31+
}
32+
}
33+

services/files/files.service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Application } from '@feathersjs/express';
2+
import Files from './files.class';
3+
4+
export default (app: Application): void => {
5+
app.use('/files', new Files());
6+
};
7+

services/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Votes from './votes/votes.service';
66
import Auth from './auth/auth.service';
77
import Feed from './feed/feed.service';
88
import Feedback from './feedback/feedback.service';
9+
import Files from './files/files.service';
910

1011
import tryAuthenticate from '../hooks/tryAuthenticate';
1112
import logging from '../hooks/logging';
@@ -19,6 +20,7 @@ export default (app: Application): void => {
1920
app.configure(Votes);
2021
app.configure(Feed);
2122
app.configure(Feedback);
23+
app.configure(Files);
2224

2325
app.hooks({
2426
before: {

0 commit comments

Comments
 (0)