forked from yy0ung/nibobnebob
-
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.
feat : object storage에 대한 서비스 로직 구현 #35
- object storage에 파일 업로드 서비스 구현 - object storage에서 파일 다운로드를 할 수 있는 서명된 URL 응답 구현 Co-authored-by: GeunH <[email protected]>
- Loading branch information
Showing
3 changed files
with
40 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,19 +1,42 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import * as AWS from 'aws-sdk'; | ||
import { awsConfig } from 'objectStorage.config'; | ||
import { Injectable } from "@nestjs/common"; | ||
import * as AWS from "aws-sdk"; | ||
import { awsConfig } from "objectStorage.config"; | ||
import { v4 } from "uuid"; | ||
|
||
@Injectable() | ||
export class AwsService { | ||
private s3: AWS.S3; | ||
private s3: AWS.S3; | ||
|
||
constructor() { | ||
this.s3 = new AWS.S3({ | ||
endpoint: awsConfig.endpoint, | ||
region: awsConfig.region, | ||
credentials: { | ||
accessKeyId: awsConfig.accessKey, | ||
secretAccessKey: awsConfig.secretKey | ||
}, | ||
}); | ||
} | ||
} | ||
constructor() { | ||
this.s3 = new AWS.S3({ | ||
endpoint: awsConfig.endpoint, | ||
region: awsConfig.region, | ||
credentials: { | ||
accessKeyId: awsConfig.accessKey, | ||
secretAccessKey: awsConfig.secretKey, | ||
}, | ||
}); | ||
} | ||
|
||
uploadToS3(path: string, data: Buffer): string { | ||
const uuid = v4(); | ||
|
||
this.s3.putObject({ | ||
Bucket: awsConfig.bucket, | ||
Key: `${path}/${uuid}.png`, | ||
Body: data, | ||
}); | ||
|
||
return uuid; | ||
} | ||
|
||
getImageURL(path: string) { | ||
const signedUrl = this.s3.getSignedUrl("getObject", { | ||
Bucket: awsConfig.bucket, | ||
Key: path, | ||
Expires: 60, | ||
}); | ||
|
||
console.log(signedUrl); | ||
} | ||
} |