@slickteam/nestjs-cellar
Version:
Module for Cellar with Nestjs
103 lines • 4.78 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CellarService = void 0;
const client_s3_1 = require("@aws-sdk/client-s3");
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const logger = new common_1.Logger('CellarService');
let CellarService = class CellarService {
configService;
timeoutSignedUrl;
s3EndPoint;
s3Client;
constructor(configService) {
this.configService = configService;
this.s3EndPoint = `https://${this.configService.getOrThrow('CELLAR_ADDON_HOST')}`;
const regionBucket = this.configService.get('CELLAR_REGION') ?? 'fr';
this.timeoutSignedUrl = this.configService.get('CELLAR_TIMEOUT_SIGNED_URL') ?? 3600;
this.s3Client = new client_s3_1.S3Client({
endpoint: this.s3EndPoint,
region: regionBucket,
credentials: {
accessKeyId: this.configService.getOrThrow('CELLAR_ADDON_KEY_ID'),
secretAccessKey: this.configService.getOrThrow('CELLAR_ADDON_KEY_SECRET'),
},
});
}
async listObjectsByBucketName(name) {
const result = await this.s3Client.send(new client_s3_1.ListObjectsV2Command({
Bucket: name,
}));
return (result.Contents?.map((c) => ({
name: c.Key ?? '',
lastModified: c.LastModified ?? '',
eTag: c.ETag ?? '',
size: c.Size ?? 0,
storageClass: c.StorageClass ?? '',
})) ?? []);
}
async createPresignedUrlWithClient(bucketName, keyObject) {
const command = new client_s3_1.GetObjectCommand({ Bucket: bucketName, Key: keyObject });
return (0, s3_request_presigner_1.getSignedUrl)(this.s3Client, command, { expiresIn: this.timeoutSignedUrl });
}
async uploadFile(bucketName, file) {
return this.s3Client.send(new client_s3_1.PutObjectCommand({
Bucket: bucketName,
Key: file.originalname,
Body: file.buffer,
ACL: 'bucket-owner-full-control',
ContentType: file.mimetype,
}));
}
async deleteFile(bucketName, fileName) {
return this.s3Client.send(new client_s3_1.DeleteObjectCommand({
Bucket: bucketName,
Key: fileName,
}));
}
async getObjectMetadata(bucketName, fileName) {
return this.s3Client.send(new client_s3_1.HeadObjectCommand({ Bucket: bucketName, Key: fileName }));
}
async isFileExist(bucketName, fileName) {
const result = await this.getObjectMetadata(bucketName, fileName);
const code = result?.$metadata?.httpStatusCode ?? 500;
return code >= 200 && code < 400;
}
async getSignedUrl(bucketName, fileName) {
const command = new client_s3_1.GetObjectCommand({ Bucket: bucketName, Key: fileName });
return (0, s3_request_presigner_1.getSignedUrl)(this.s3Client, command, { expiresIn: this.timeoutSignedUrl });
}
async uploadPdfToS3(bucketName, fileName, pdfBuffer) {
await this.uploadFile(bucketName, {
buffer: pdfBuffer,
originalname: fileName,
mimetype: 'application/pdf',
});
}
async downloadPdfFromS3(bucketName, fileName) {
const resultCommand = await this.s3Client.send(new client_s3_1.GetObjectCommand({ Bucket: bucketName, Key: fileName }));
const rawFileBytes = await resultCommand.Body?.transformToByteArray();
if (rawFileBytes) {
return Buffer.from(rawFileBytes);
}
else {
throw new Error(`Error when downloading PDF from S3 with fileName[${fileName}]`);
}
}
};
exports.CellarService = CellarService;
exports.CellarService = CellarService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [config_1.ConfigService])
], CellarService);
//# sourceMappingURL=cellar.service.js.map