Skip to content

Commit

Permalink
feat(nestjs-storage): upload (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsangste authored Oct 15, 2024
1 parent 8cf52e5 commit ce36914
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions libs/nestjs-storage/src/services/storage.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing'
import { Logger } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'

import { S3Client } from '@aws-sdk/client-s3'

Expand All @@ -13,6 +14,7 @@ describe('StorageService', () => {
providers: [
StorageService,
Logger,
ConfigService,
{
provide: S3Client,
useValue: {}
Expand Down
24 changes: 20 additions & 4 deletions libs/nestjs-storage/src/services/storage.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { Inject, Injectable, Logger, LoggerService } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'

import { S3Client } from '@aws-sdk/client-s3'
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'

@Injectable()
export class StorageService {
constructor(@Inject(Logger) private readonly logger: LoggerService, private readonly s3client: S3Client) {}
constructor(@Inject(Logger) private readonly logger: LoggerService, private readonly config: ConfigService, private readonly s3Client: S3Client) {}

upload() {
this.logger.debug('upload files!')
async upload(key: string, data: string | Buffer) {
const bucketName = this.config.get('AWS_S3_BUCKET_NAME', 'bucket')
const prefix = this.config.get('AWS_S3_PREFIX', 'csv')

const result = await this.s3Client.send(
new PutObjectCommand({
Bucket: bucketName,
Key: [prefix, key].join('/'),
Body: data,
Tagging: 'csv',
ContentType: 'text/csv; charset=utf-8',
})
)

this.logger.log({ message: `Uploaded file to S3 bucket(${bucketName}) - ${key}`, bucketName, key })

return result
}
}

0 comments on commit ce36914

Please sign in to comment.