From 0efc3134eb348e68e7bf7519e770237e15811900 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 10 Aug 2020 23:13:56 +0300 Subject: feat: create Files service for s3 uploads --- services/files/files.class.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 services/files/files.class.ts (limited to 'services/files/files.class.ts') diff --git a/services/files/files.class.ts b/services/files/files.class.ts new file mode 100644 index 0000000..8a104b7 --- /dev/null +++ b/services/files/files.class.ts @@ -0,0 +1,33 @@ +import { Application } from '@feathersjs/express'; +import { Params } from '@feathersjs/feathers'; +import { S3 } from 'aws-sdk'; +import { v4 as uuidv4 } from 'uuid'; + + +export default class Files { + app!: Application; + s3!: S3; + bucket!: string; + + async find(params: Params): Promise { + // Return signed upload URL + return this.s3.getSignedUrl('putObject', { + Bucket: this.bucket, + Key: `${params.user?.username}/${uuidv4()}.png`, + ContentType: 'image/*', + Expires: 300, + }); + } + + setup(app: Application): void { + this.app = app; + this.s3 = new S3({ + accessKeyId: process.env.ACCESSKEYID, + secretAccessKey: process.env.SECRETACCESSKEY, + signatureVersion: 'v4', + region: 'eu-central-1' + }); + this.bucket = process.env.BUCKET || ''; + } +} + -- cgit v1.2.3