-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
merge #37: Nest์ ObjectStorage ์ฐ๋
- Loading branch information
Showing
4 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,29 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { | ||
Controller, | ||
Get, | ||
Post, | ||
UploadedFile, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { AppService } from './app.service'; | ||
import { StorageService } from './storage/storage.service'; | ||
import { FileInterceptor } from '@nestjs/platform-express'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
constructor( | ||
private readonly appService: AppService, | ||
private readonly storageService: StorageService | ||
) {} | ||
|
||
@Get() | ||
getHello(): string { | ||
return this.appService.getHello(); | ||
} | ||
|
||
@Post('/upload') | ||
@UseInterceptors(FileInterceptor('file')) | ||
async upload(@UploadedFile() file: Express.Multer.File) { | ||
return this.storageService.upload(file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { StorageService } from './storage.service'; | ||
|
||
@Module({ | ||
providers: [StorageService], | ||
exports: [StorageService], | ||
}) | ||
export class StorageModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import * as AWS from 'aws-sdk'; | ||
|
||
@Injectable() | ||
export class StorageService { | ||
private readonly s3: AWS.S3 = new AWS.S3({ | ||
endpoint: 'https://kr.object.ncloudstorage.com', | ||
region: process.env.AWS_REGION, | ||
credentials: { | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID, | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | ||
}, | ||
}); | ||
private readonly bucketName = 'traveline'; | ||
|
||
private generateFilename(originalname: string) { | ||
const extension = originalname.split('.').pop(); | ||
const uniqueId = uuidv4(); | ||
return `${uniqueId}.${extension}`; | ||
} | ||
|
||
async upload(file: Express.Multer.File) { | ||
const uploadParams = { | ||
Bucket: this.bucketName, | ||
Key: this.generateFilename(file.originalname), | ||
Body: file.buffer, | ||
ACL: 'public-read', | ||
}; | ||
|
||
const result = await this.s3.upload(uploadParams).promise(); | ||
|
||
return { | ||
imageUrl: result.Location, | ||
}; | ||
} | ||
} |