Created
September 14, 2019 11:20
-
-
Save brianpooe/5601a4367b92e8fdab72c02a97af46c7 to your computer and use it in GitHub Desktop.
NestJS upload file to AWS s3 bucket with multer
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
import { Req, Res, Injectable } from '@nestjs/common'; | |
import { Profile } from './profile.entity'; | |
import { Repository } from 'typeorm'; | |
import { InjectRepository } from '@nestjs/typeorm'; | |
import { ProfileDto } from './dto/profile.dto'; | |
import { User } from 'users/user.entity'; | |
import { UserQuery, ProfileQuery } from './structures/helpers'; | |
import * as multer from 'multer'; | |
import * as AWS from 'aws-sdk'; | |
import * as multerS3 from 'multer-s3'; | |
import { extname } from 'path'; | |
const AWS_S3_BUCKET_NAME = 'artapps3bucket'; | |
AWS.config.loadFromPath('./aws/AwsConfig.json'); | |
const s3 = new AWS.S3(); | |
@Injectable() | |
export class ProfileService { | |
constructor( @InjectRepository(Profile) private readonly profileRepository: Repository<Profile>, | |
@InjectRepository(User) private readonly userRepository: Repository<User> ) {} | |
async createProfile( profileDto: ProfileDto): Promise<void> { | |
const profile: Profile = this.createProfileObject( profileDto ); | |
await this.profileRepository.save( profile ); | |
await this.updateUserAndDeleteFormerProfileIfExists( profile, profileDto.username ); | |
} | |
async updateUserAndDeleteFormerProfileIfExists( profile: Profile, username: string ): Promise<void> { | |
const userQuery: UserQuery = new UserQuery( username ); | |
const user: User = await this.userRepository.findOne(userQuery); | |
const formerProfileId: number = user.profileId; | |
user.profile = profile; | |
await this.userRepository.save(user); | |
await this.deleteFormerProfileIfExists( formerProfileId ); | |
} | |
async deleteFormerProfileIfExists( id: number ): Promise<void> { | |
const profileQuery: ProfileQuery = new ProfileQuery( id ); | |
const profile: Profile = await this.profileRepository.findOne( profileQuery ); | |
await this.profileRepository.remove( profile ); | |
} | |
createProfileObject( profileDto: ProfileDto ): Profile { | |
const profile: Profile = new Profile(); | |
profile.bio = profileDto.bio; | |
profile.interests = profileDto.interests; | |
return profile; | |
} | |
async createReference( fileName: string, username: string ): Promise<boolean> { | |
const profile: Profile = await this.checkIfUserExistsAndReturnRelevantProfile( username ); | |
if ( profile ) { | |
profile.profilePhoto = fileName; | |
await this.profileRepository.save( profile ); | |
return true; | |
} | |
return false; | |
} | |
async checkIfUserExistsAndReturnRelevantProfile( username: string ): Promise<Profile> { | |
const userQuery: UserQuery = new UserQuery( username ); | |
const user: User = await this.userRepository.findOne( userQuery ); | |
const profileQuery: ProfileQuery = new ProfileQuery( user.profileId ); | |
return await this.profileRepository.findOne( profileQuery ); | |
} | |
async uploadProfileImage( @Req() req, username: string ): Promise<void> { | |
const randomName = Array(32).fill(null).map(() => (Math.round(Math.random() * 16)).toString(16)).join(''); | |
const fileName = `${randomName}${extname(req.file.originalname)}`; | |
const params = { | |
Body: req.file.buffer, | |
Bucket: AWS_S3_BUCKET_NAME, | |
Key: 'profile-photos/' + fileName, | |
}; | |
const shouldUploadFile = await this.createReference( fileName, username ); | |
if ( shouldUploadFile ){ | |
return await s3 | |
.putObject(params) | |
.promise() | |
.then( | |
data => { | |
return; | |
}, | |
err => { | |
return err; | |
}); | |
} else { | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment