DigitalOcean S3 NestJS Example
To implement DigitalOcean Spaces (which is an S3-compatible object storage service) in NestJS for uploading images, you can use the aws-sdk package, which supports DigitalOcean Spaces as well. Here’s a step-by-step guide:
To implement DigitalOcean Spaces (which is an S3-compatible object storage service) in NestJS for uploading images, you can use the aws-sdk package, which supports DigitalOcean Spaces as well. Here’s a step-by-step guide:
First, install the aws-sdk package by running the following command:
npm install aws-sdk
Create a new module in your NestJS application by running the following command:
nest g module digitalocean
Inside the newly created digital ocean module, create a new service file called digitalocean.service.ts with the following content:
import { Injectable } from ‘@nestjs/common’;
import * as AWS from ‘aws-sdk’;
@Injectable()
export class DigitalOceanService {
private s3: AWS.S3;
constructor() {
this.s3 = new AWS.S3({
endpoint: 'https://your-space-name.nyc3.digitaloceanspaces.com',
accessKeyId: 'your-access-key-id',
secretAccessKey: 'your-secret-access-key',
region: 'nyc3',
});
}
async uploadFile(buffer: Buffer, filename: string): Promise<string> {
const params = {
Bucket: 'your-bucket-name',
Key: filename,
Body: buffer,
};
const { Location } = await this.s3.upload(params).promise();
return Location;
}
}
Note that you should replace the placeholders your-space-name, your-access-key-id, your-secret-access-key, and your-bucket-name with your actual DigitalOcean Spaces details.
Export the DigitalOceanService from the digitalocean module by adding the following line to the digitalocean.module.ts file:
import { Module } from '@nestjs/common';
import { DigitalOceanService } from './digitalocean.service';
@Module({
providers: [DigitalOceanService],
exports: [DigitalOceanService],
})
export class DigitalOceanModule {}
Finally, you can use the DigitalOceanService in any other module of your application by injecting it as a dependency, like this:
import { Controller, Post, UseInterceptors, UploadedFile } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { DigitalOceanService } from './digitalocean/digitalocean.service';
@Controller()
export class AppController {
constructor(private readonly digitalOceanService: DigitalOceanService) {}
@Post('upload')
@UseInterceptors(FileInterceptor('file'))
async upload(@UploadedFile() file: Express.Multer.File) {
const buffer = file.buffer;
const filename = file.originalname;
const imageUrl = await this.digitalOceanService.uploadFile(buffer, filename);
return { imageUrl };
}
}
In this example, the AppController injects the DigitalOceanService and uses it to upload a file in a POST /upload endpoint using the @nestjs/platform-express package to handle file uploads. The endpoint returns a JSON object with the uploaded image URL.
That’s it! You should now be able to upload images to DigitalOcean Spaces from your NestJS application.