Skip to content

Commit

Permalink
feat(be): add content type upload s3
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Sep 12, 2023
1 parent 5273132 commit a3cd510
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/files/files.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
import {
PutObjectCommand,
PutObjectCommandInput,
S3Client,
} from '@aws-sdk/client-s3';
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { readFileSync } from 'fs';
Expand Down Expand Up @@ -82,12 +86,16 @@ export class FilesService {
this.configService.get<string>('aws.s3SubFolder') || 'images';
const keyName = `${s3SubFolder}/${key}/${f.originalname}`;

await this.uploadToS3(keyName, f.buffer);
await this.uploadToS3(keyName, f.buffer, f.mimetype);
return new URL(keyName, this.configService.get<string>('aws.queryEndpoint'))
.href;
}

async uploadToS3(keyName: string, filePath: string | Buffer) {
async uploadToS3(
keyName: string,
filePath: string | Buffer,
mimetype?: string,
) {
const file =
typeof filePath === 'string' ? readFileSync(filePath) : filePath;

Expand All @@ -102,12 +110,22 @@ export class FilesService {
const bucketName = this.configService.get<string>('aws.bucketName');
this.logger.debug(`Upload key: ${keyName} to bucket ${bucketName}`);

// Create a promise on S3 service object
const command = new PutObjectCommand({
const input: PutObjectCommandInput = {
Bucket: bucketName,
Key: keyName,
Body: file,
});
Metadata: {
'Content-Type': mimetype,
},
};

if (mimetype)
input.Metadata = {
'Content-Type': mimetype,
};

// Create a promise on S3 service object
const command = new PutObjectCommand(input);
return client.send(command);
}
}

0 comments on commit a3cd510

Please sign in to comment.