-
Notifications
You must be signed in to change notification settings - Fork 2
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
…orage [Feat] object storage 파일 업로드 기능 구현
- Loading branch information
Showing
6 changed files
with
1,307 additions
and
0 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
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,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, | ||
}); | ||
} | ||
} |
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,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 {} |
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,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); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.