Skip to content

Commit

Permalink
feat : object storage에 대한 서비스 로직 구현 #35
Browse files Browse the repository at this point in the history
- object storage에 파일 업로드 서비스 구현
- object storage에서 파일 다운로드를 할 수 있는 서명된 URL 응답 구현

Co-authored-by: GeunH <[email protected]>
  • Loading branch information
LeeTH916 and GeunH committed Nov 30, 2023
1 parent 28f09b2 commit 765f29d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions be/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"rxjs": "^7.8.1",
"swagger-ui-express": "^5.0.0",
"typeorm": "^0.3.17",
"uuid": "^9.0.1",
"winston": "^3.11.0"
},
"devDependencies": {
Expand Down
53 changes: 38 additions & 15 deletions be/src/aws/aws.service.ts
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);
}
}

0 comments on commit 765f29d

Please sign in to comment.