Skip to content

Commit

Permalink
Merge pull request #37 from kmi0817/be-feature/#31-storage
Browse files Browse the repository at this point in the history
merge #37: Nest์™€ ObjectStorage ์—ฐ๋™
  • Loading branch information
yaongmeow authored Nov 16, 2023
2 parents 855220c + 2f83a81 commit 7019451
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
21 changes: 19 additions & 2 deletions BE/src/app.controller.ts
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);
}
}
2 changes: 2 additions & 0 deletions BE/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { TimelinesModule } from './timelines/timelines.module';
import { AuthModule } from './auth/auth.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { DatabaseModule } from './database/database.module';
import { StorageModule } from './storage/storage.module';

@Module({
imports: [
ConfigModule.forRoot({
envFilePath: `.env`,
isGlobal: true,
}),
StorageModule,
UsersModule,
PostingsModule,
TimelinesModule,
Expand Down
8 changes: 8 additions & 0 deletions BE/src/storage/storage.module.ts
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 {}
37 changes: 37 additions & 0 deletions BE/src/storage/storage.service.ts
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,
};
}
}

0 comments on commit 7019451

Please sign in to comment.