nestjs-azure-storage-blob
Version:
Azure Blob Storage module for Nest.js
55 lines • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppController = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const fs = require("fs");
const path = require("path");
const lib_1 = require("../lib");
let AppController = class AppController {
constructor(storageBlobService) {
this.storageBlobService = storageBlobService;
}
async getSas(containerName = process.env.NEST_STORAGE_BLOB_CONTAINER, fileName = 'image.jpg') {
const expiresOn = new Date(Date.now() + 3600 * 1000);
const accountSas = await this.storageBlobService.getAccountSasUrl();
const containerSas = await this.storageBlobService.getContainerSasUrl(containerName, { add: true, read: true }, { expiresOn });
const blobSas = await this.storageBlobService.getBlockBlobSasUrl(containerName, fileName, { add: true, create: true, read: true, delete: true, write: true }, { expiresOn });
return { accountSas, containerSas, blobSas };
}
async upload(containerName = process.env.NEST_STORAGE_BLOB_CONTAINER, fileName = 'image.jpg') {
const expiresOn = new Date(Date.now() + 3600 * 1000);
const blobSas = await this.storageBlobService.getBlockBlobSasUrl(containerName, fileName, { add: true, create: true, read: true, delete: true, write: true }, { expiresOn });
const buffer = fs.readFileSync(path.join(process.cwd(), 'assets', fileName));
if (buffer) {
throw new common_1.NotFoundException('file not found');
}
const response = await axios_1.default.put(blobSas.sasUrl, buffer, {
headers: { ...blobSas.headers },
});
return { blobSas, status: response.status };
}
};
exports.AppController = AppController;
tslib_1.__decorate([
(0, common_1.Get)('/'),
tslib_1.__param(0, (0, common_1.Query)('containerName')),
tslib_1.__param(1, (0, common_1.Query)('fileName')),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [Object, Object]),
tslib_1.__metadata("design:returntype", Promise)
], AppController.prototype, "getSas", null);
tslib_1.__decorate([
(0, common_1.Get)('/upload'),
tslib_1.__param(0, (0, common_1.Query)('containerName')),
tslib_1.__param(1, (0, common_1.Query)('fileName')),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [Object, Object]),
tslib_1.__metadata("design:returntype", Promise)
], AppController.prototype, "upload", null);
exports.AppController = AppController = tslib_1.__decorate([
(0, common_1.Controller)(),
tslib_1.__metadata("design:paramtypes", [lib_1.AzureStorageBlobService])
], AppController);
//# sourceMappingURL=app.controller.stub.js.map