hfs-utilities
Version:
Health Fund Solution's internal utilities library for Typescript projects
203 lines • 9.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlockBlobService = void 0;
const storage_blob_1 = require("@azure/storage-blob");
/**
* A BlockBlobService instance is a convenience wrapper around the Azure Storage BlobServiceClient.
*/
class BlockBlobService {
/**
* Creates an instance of the BlockBlobService class.
* @param connectionString - Optional. The Azure Storage Account's Primary Connection String
* @param containerName - Optional. The name of the container.
*/
constructor(connectionString, containerName) {
if (connectionString !== undefined) {
this.blobService = storage_blob_1.BlobServiceClient.fromConnectionString(connectionString);
}
this.containerName = containerName;
}
/**
* @description - Connects to blob storage
* @param {string} connectionString - The Azure Storage Account's Primary Connection String
* @returns {Promise<void>} - A promise that resolves when the connection is established.
*/
async connect(connectionString) {
this.blobService = storage_blob_1.BlobServiceClient.fromConnectionString(connectionString);
}
/**
* @description - Writes a string, array, or JSON object to a blob as a string.
* @param {string} blobName - The name of the blob.
* @param {string | any[] | Object | Buffer } content - The content of the blob.
* @param {string} containerName - The name of the container.
*/
async write(blobName, content, containerName) {
var _a;
containerName = (_a = this.containerName) !== null && _a !== void 0 ? _a : containerName;
this.throwIfContainerIsUndefined(containerName);
const containerExists = await this.checkIfContainerExists(containerName);
if (!containerExists) {
await this.createContainer(containerName);
}
const containerClient = this.blobService.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
if (content instanceof Buffer) {
await blockBlobClient.uploadData(content);
}
else {
const contentToUpload = typeof content === 'object' ? JSON.stringify(content) : content;
await blockBlobClient.upload(contentToUpload, contentToUpload.length);
}
}
/**
* @description - Returns the string content of a blob.
* @param {string} blobName - The name of the blob.
* @param {string} containerName - The name of the container.
* @returns {Promise<string | any[] | Object>} - The content of the blob as a string, array, or object.
*/
async read(blobName, containerName) {
var _a;
containerName = (_a = this.containerName) !== null && _a !== void 0 ? _a : containerName;
this.throwIfContainerIsUndefined(containerName);
const containerClient = this.blobService.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const content = await blockBlobClient.downloadToBuffer();
if ((content.toString('utf8').startsWith('{') && content.toString('utf8').endsWith('}')) ||
(content.toString('utf8').startsWith('[') && content.toString('utf8').endsWith(']'))) {
return JSON.parse(content.toString('utf8'));
}
return content.toString('utf8');
}
/**
* @description - Deletes a blob.
* @param {string} blobName - The name of the blob.
* @param {string} containerName - The name of the container.
* @returns {Promise<void>} - A promise that resolves when the blob is deleted.
*/
async delete(blobName, containerName) {
var _a;
containerName = (_a = this.containerName) !== null && _a !== void 0 ? _a : containerName;
this.throwIfContainerIsUndefined(containerName);
const blobExists = await this.checkIfBlobExists(blobName, containerName);
if (blobExists) {
const containerClient = this.blobService.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.delete();
}
else {
throw new Error(`Blob ${blobName} does not exist in container ${containerName}`);
}
}
/**
* @description - Returns a boolean indicating whether the blob container exists.
* @param {string} containerName - The name of the container.
* @returns {boolean} - A promise that resolves with a boolean indicating whether the container exists.
*/
async checkIfContainerExists(containerName) {
var _a;
containerName = (_a = this.containerName) !== null && _a !== void 0 ? _a : containerName;
this.throwIfContainerIsUndefined(containerName);
const containerClient = this.blobService.getContainerClient(containerName);
return await containerClient.exists();
}
/**
* @description - Returns a boolean indicating whether the blob exists.
* @param {string} blobName - The name of the blob.
* @param {string} containerName - The name of the container.
* @returns {Promise<boolean>} - A promise that resolves with a boolean indicating whether the blob exists.
*/
async checkIfBlobExists(blobName, containerName) {
var _a;
containerName = (_a = this.containerName) !== null && _a !== void 0 ? _a : containerName;
this.throwIfContainerIsUndefined(containerName);
const containerClient = this.blobService.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
return await blockBlobClient.exists();
}
/**
* @description - Creates a blob container if one does not exist.
* @param {string} containerName - The name of the container.
* @returns {Promise<void>} - A promise that resolves when the container is created.
*/
async createContainer(containerName) {
var _a;
containerName = (_a = this.containerName) !== null && _a !== void 0 ? _a : containerName;
this.throwIfContainerIsUndefined(containerName);
const exists = await this.checkIfContainerExists(containerName);
if (!exists) {
const containerClient = this.blobService.getContainerClient(containerName);
await containerClient.create();
}
}
/**
* @description - Deletes a blob container.
* @param {string} containerName - The name of the container.
* @returns {Promise<void>} - A promise that resolves when the container is deleted.
*/
async deleteContainer(containerName) {
var _a;
containerName = (_a = this.containerName) !== null && _a !== void 0 ? _a : containerName;
this.throwIfContainerIsUndefined(containerName);
const exists = await this.checkIfContainerExists(containerName);
if (exists) {
const containerClient = this.blobService.getContainerClient(containerName);
await containerClient.delete();
}
}
/**
* @description - Returns a list of container names.
* @returns {Promise<string[]>} - A promise that resolves with a list of container names.
*/
async listBlobContainers() {
const containerNames = [];
for await (const containerItem of this.blobService.listContainers()) {
containerNames.push(containerItem.name);
}
return containerNames;
}
/**
* @description - Returns a list of blob names in a container.
* @param {string} containerName - The name of the container.
* @returns {Promise<string[]>} - A promise that resolves with a list of blob names.
*/
async listBlobs(containerName) {
var _a;
containerName = (_a = this.containerName) !== null && _a !== void 0 ? _a : containerName;
this.throwIfContainerIsUndefined(containerName);
const blobNames = [];
const containerClient = this.blobService.getContainerClient(containerName);
for await (const blobItem of containerClient.listBlobsFlat()) {
blobNames.push(blobItem.name);
}
return blobNames;
}
/**
* @description - Moves a file from one container to another.
* @param {string} fileName - The name of the source blob.
* @param {string} toContainer - The name of the container where the blob will be moved to.
* @param {string} fromContainer - The name of the container where the blob will be moved from.
*/
async moveBlob(fileName, toContainer, fromContainer) {
var _a;
fromContainer = (_a = this.containerName) !== null && _a !== void 0 ? _a : fromContainer;
this.throwIfContainerIsUndefined(fromContainer);
const toContainerExists = await this.checkIfContainerExists(toContainer);
if (!toContainerExists) {
await this.createContainer(toContainer);
}
const fileData = await this.read(fileName, fromContainer);
await this.write(fileName, fileData, toContainer);
await this.delete(fileName, fromContainer);
}
/**
* @description - throws an error if the container is undefined.
* @param {any} val - The value to check.
*/
throwIfContainerIsUndefined(val) {
if (val === undefined) {
throw new Error('ContainerName cannot be undefined');
}
}
}
exports.BlockBlobService = BlockBlobService;
//# sourceMappingURL=azureBlob.js.map