34 lines
1001 B
TypeScript
34 lines
1001 B
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { S3Client, S3ClientConfig } from '@aws-sdk/client-s3';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { Upload } from '@aws-sdk/lib-storage';
|
|
|
|
@Injectable()
|
|
export class UploadService {
|
|
private s3Client: S3Client;
|
|
|
|
constructor(private configService: ConfigService) {
|
|
this.s3Client = new S3Client({
|
|
region: this.configService.get('AWS_REGION'),
|
|
credentials: {
|
|
accessKeyId: this.configService.get('AWS_ACCESS_KEY_ID'),
|
|
secretAccessKey: this.configService.get('AWS_SECRET_ACCESS_KEY'),
|
|
},
|
|
endpoint: this.configService.get('S3_ENDPOINT'),
|
|
});
|
|
}
|
|
|
|
async uploadFile(file: Express.Multer.File, key: string): Promise<string> {
|
|
const upload = new Upload({
|
|
client: this.s3Client,
|
|
params: {
|
|
Bucket: this.configService.get('S3_BUCKET'),
|
|
Key: key,
|
|
Body: file.buffer,
|
|
},
|
|
});
|
|
const result = await upload.done();
|
|
return result.Location;
|
|
}
|
|
}
|