Skip to content

Instantly share code, notes, and snippets.

@bbachi
Created October 24, 2022 16:32
Show Gist options
  • Select an option

  • Save bbachi/bf191d2b62bb35c5ee064d3a411694cf to your computer and use it in GitHub Desktop.

Select an option

Save bbachi/bf191d2b62bb35c5ee064d3a411694cf to your computer and use it in GitHub Desktop.
Blob Trigger
const { BlobServiceClient } = require("@azure/storage-blob");
export class StorageService {
constructor() {}
public async downloadContent(containerName: string, blobName: string): Promise<any> {
let content = "";
try {
const blobServiceClient = BlobServiceClient.fromConnectionString(process.env.BLOB_CONNECTION_STRING);
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);
const downloadBlockBlobResponse = await blobClient.download();
content = (await this.streamToBuffer(downloadBlockBlobResponse.readableStreamBody)).toString();
} catch(ex) {
console.log("Exception Occured while downloadContent() ", ex);
}
return content;
}
// [Node.js only] A helper method used to read a Node.js readable stream into a Buffer
private async streamToBuffer(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data instanceof Buffer ? data : Buffer.from(data));
});
readableStream.on("end", () => {
resolve(Buffer.concat(chunks));
});
readableStream.on("error", reject);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment