Skip to content

Commit

Permalink
Merge pull request #292 from boostcampwm-2024/feat/#289/ncp-object-st…
Browse files Browse the repository at this point in the history
…orage

[Feat] object storage 파일 업로드 기능 구현
  • Loading branch information
Jieun1ee authored Dec 2, 2024
2 parents b222095 + 2116d41 commit a6617fd
Show file tree
Hide file tree
Showing 6 changed files with 1,307 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/media/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.700.0",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.3.0",
"@nestjs/core": "^10.0.0",
Expand All @@ -29,6 +30,7 @@
"@repo/lint": "workspace:*",
"@repo/tsconfig": "workspace:*",
"@repo/mediasoup": "workspace:*",
"@repo/tsconfig": "workspace:*",
"@repo/types": "workspace:*",
"mediasoup": "^3.15.0",
"reflect-metadata": "^0.2.0",
Expand Down
25 changes: 25 additions & 0 deletions apps/media/src/ncp/ncp.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { S3Client } from '@aws-sdk/client-s3';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class NcpConfig {
s3Client: S3Client;

constructor(private configService: ConfigService) {
const accessKeyId = this.configService.get<string>('NCP_ACCESS_KEY');
const secretAccessKey = this.configService.get<string>('NCP_SECRET_KEY');
const region = this.configService.get<string>('NCP_OBJECT_STORAGE_REGION');
const endpoint = this.configService.get<string>('NCP_OBJECT_STORAGE_ENDPOINT');

this.s3Client = new S3Client({
region: region,
credentials: {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
},
endpoint: endpoint,
forcePathStyle: true,
});
}
}
10 changes: 10 additions & 0 deletions apps/media/src/ncp/ncp.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';

import { NcpConfig } from './ncp.config';
import { NcpService } from './ncp.service';

@Module({
providers: [NcpService, NcpConfig],
exports: [NcpService],
})
export class AppModule {}
44 changes: 44 additions & 0 deletions apps/media/src/ncp/ncp.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as fs from 'fs';

import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ErrorMessage } from '@repo/types';

import { NcpConfig } from './ncp.config';

@Injectable()
export class NcpService {
private s3: S3Client;

constructor(
private ncpConfig: NcpConfig,
private configService: ConfigService
) {
this.s3 = ncpConfig.s3Client;
}

async uploadFile(localFilePath: string, remoteFileName: string): Promise<string> {
const bucketName = this.configService.get<string>('NCP_OBJECT_STORAGE_BUCKET');
const endpoint = this.configService.get<string>('NCP_OBJECT_STORAGE_ENDPOINT');

const fileStream = fs.createReadStream(localFilePath);
const params = {
Bucket: bucketName,
Key: remoteFileName,
Body: fileStream,
};

try {
const uploadResponse = await this.s3.send(new PutObjectCommand(params));
// console.log('File uploaded:', uploadResponse);

const url = `${endpoint}/${bucketName}/${remoteFileName}`;
// console.log('Uploaded file URL:', url);

return remoteFileName;
} catch (error) {
throw new Error(ErrorMessage.FILE_UPLOAD_FAILED);
}
}
}
1 change: 1 addition & 0 deletions packages/types/src/errorMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const ErrorMessage = {
ROOM_NOT_FOUND: '방이 존재하지 않습니다',
TRANSPORT_NOT_FOUND: 'transport가 존재하지 않습니다',
PEER_ALREADY_EXISTS_IN_ROOM: '이미 방에 존재하는 Peer입니다',
FILE_UPLOAD_FAILED: '파일 업로드에 실패했습니다',
} as const;

export type ErrorMessage = (typeof ErrorMessage)[keyof typeof ErrorMessage];
Loading

0 comments on commit a6617fd

Please sign in to comment.