NestJS Efficient File Storage with S3
Nadeera Sampath
Senior Lead Software Engineer | NestJS | MongoDB | ReactJS | AWS
Are you a developer building web applications that require efficient file storage? Look no further! In this tutorial, we'll explore how to integrate Amazon S3, a powerful cloud-based object storage service, with NestJS, a robust Node.js framework, to handle file storage seamlessly. By the end of this tutorial, you'll have a solid understanding of how to leverage S3's scalability and NestJS's modularity to optimize your file storage strategy.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
To start, you'll need an AWS account. Head over to the AWS website (https://aws.amazon.com/) and create a new account or log in if you already have one. Once you're in, navigate to the Amazon S3 service and create a new S3 bucket to store your files.
Initialize Your NestJS Project
If you don't already have a NestJS project, let's create one. Open your terminal and run the following commands:
# Install the NestJS CLI globally (if you haven't already)
npm install -g @nestjs/cli
# Create a new NestJS project
nest new nest-s3-file-storage
Install AWS SDK
In your NestJS project directory, install the AWS SDK using npm:
npm install aws-sdk
Configure AWS Credentials
To interact with your S3 bucket, you need to provide your AWS access key and secret key. There are two recommended ways to do this:
Environment Variables:
Set your AWS access key and secret key as environment variables on your system:
export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key
AWS SDK Configuration:
领英推荐
Alternatively, you can configure the AWS SDK directly in your NestJS project. Open the main.ts file and add the following lines at the beginning of the file:
import { config as AWSConfig } from 'aws-sdk';
AWSConfig.update({
accessKeyId: 'your_access_key',
secretAccessKey: 'your_secret_key',
region: 'your_preferred_aws_region',
});
Implement File Uploads
In this step, we'll create an API endpoint in NestJS to handle file uploads. First, let's create a new module and controller for file storage:
nest generate module storage
nest generate controller storage --no-spec
Next, open the storage.controller.ts file and create an endpoint to handle file uploads:
import { Controller, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { S3 } from 'aws-sdk';
import { v4 as uuidv4 } from 'uuid';
@Controller('storage')
export class StorageController {
private s3 = new S3();
@Post('upload')
@UseInterceptors(FileInterceptor('file'))
async uploadFile(@UploadedFile() file: Express.Multer.File) {
const params = {
Bucket: 'your_bucket_name',
Key: `${uuidv4()}-${file.originalname}`,
Body: file.buffer,
};
const result = await this.s3.upload(params).promise();
return { fileUrl: result.Location };
}
}
Serving Files
Now, let's create an endpoint to serve files from S3. Open the storage.controller.ts file again and add the following method:
import { Controller, Get, Param, Res } from '@nestjs/common';
import { Response } from 'express';
import { S3 } from 'aws-sdk';
@Controller('storage')
export class StorageController {
? private s3 = new S3();
? // ... (previously defined methods)
? @Get(':key')
? async serveFile(@Param('key') key: string, @Res() res: Response) {
? ? const params = {
? ? ? Bucket: 'your_bucket_name',
? ? ? Key: key,
? ? };
? ? const stream = this.s3.getObject(params).createReadStream();
? ? stream.pipe(res);
? }
}
Advanced Features (Optional)
Amazon S3 offers a wide range of advanced features, such as access control, lifecycle policies, and versioning. You can explore these capabilities to further enhance the security and performance of your application.
Conclusion
Congratulations! You've successfully integrated Amazon S3 with NestJS for efficient file storage. You can now handle file uploads and serve files seamlessly using the power of S3 and NestJS. This combination ensures that your web application can scale effortlessly and deliver an excellent user experience.
Remember to follow best practices, such as securing your AWS credentials and implementing error handling, to ensure a robust and reliable file storage solution.
Happy coding! ??
Note: Don't forget to replace 'your_access_key', 'your_secret_key', 'your_preferred_aws_region', and 'your_bucket_name' with your actual AWS credentials and bucket name in the code snippets. Also, install the necessary dependencies (like @nestjs/platform-express and uuid) if you haven't done so already.
LinkedIn Top Voice | SDE Intern @HSV Digital | Ex-Testbook | Fullstack Developer | MERN/MEAN | Next | Nest
6 个月Thanks helped a lot for integration