-
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.
Merge branch 'feature/s3-buckets' of https://github.com/COS301-SE-202…
…4/TuneIn into develop
- Loading branch information
Showing
18 changed files
with
1,190 additions
and
162 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
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
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
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
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
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,10 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { ConfigModule } from "@nestjs/config"; | ||
import { S3Service } from "./s3.service"; | ||
|
||
@Module({ | ||
imports: [ConfigModule], | ||
providers: [S3Service], | ||
exports: [S3Service], | ||
}) | ||
export class S3Module {} |
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { S3Service } from './s3.service'; | ||
|
||
describe('S3Service', () => { | ||
let service: S3Service; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [S3Service], | ||
}).compile(); | ||
|
||
service = module.get<S3Service>(S3Service); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,68 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { S3 } from "aws-sdk"; | ||
import { ConfigService } from "@nestjs/config"; | ||
import { Express } from "express"; | ||
|
||
@Injectable() | ||
export class S3Service { | ||
private s3: S3; | ||
private bucketName: string; | ||
private accessKeyId: string; | ||
private secretAccessKey: string; | ||
private region: string; | ||
|
||
constructor(private configService: ConfigService) { | ||
const bucketName = this.configService.get<string>("AWS_S3_BUCKET_NAME"); | ||
if (!bucketName) { | ||
throw new Error("AWS_S3_BUCKET_NAME is not defined"); | ||
} | ||
this.bucketName = bucketName; | ||
|
||
const accessKeyId = this.configService.get<string>("AWS_ACCESS_KEY_ID"); | ||
if (!accessKeyId) { | ||
throw new Error("AWS_ACCESS_KEY_ID is not defined"); | ||
} | ||
this.accessKeyId = accessKeyId; | ||
|
||
const secretAccessKey = this.configService.get<string>( | ||
"AWS_SECRET_ACCESS_KEY", | ||
); | ||
if (!secretAccessKey) { | ||
throw new Error("AWS_SECRET_ACCESS_KEY is not defined"); | ||
} | ||
this.secretAccessKey = secretAccessKey; | ||
|
||
const region = this.configService.get<string>("AWS_S3_REGION"); | ||
if (!region) { | ||
throw new Error("AWS_S3_REGION is not defined"); | ||
} | ||
this.region = region; | ||
|
||
this.s3 = new S3({ | ||
accessKeyId: this.accessKeyId, | ||
secretAccessKey: this.secretAccessKey, | ||
region: this.region, | ||
}); | ||
this.bucketName = bucketName; | ||
} | ||
|
||
async uploadFile( | ||
file: Express.Multer.File, | ||
): Promise<S3.ManagedUpload.SendData> { | ||
console.log("Uploading file to S3"); | ||
console.log(file); | ||
const params = { | ||
Bucket: this.bucketName, | ||
Key: `${Date.now()}-${file.originalname}`, | ||
Body: file.buffer, | ||
ContentType: file.mimetype, | ||
}; | ||
|
||
return await this.s3.upload(params).promise(); | ||
} | ||
|
||
async getFileUrl(key: string): Promise<string> { | ||
const params = { Bucket: this.bucketName, Key: key }; | ||
return this.s3.getSignedUrlPromise("getObject", params); | ||
} | ||
} |
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.