Skip to content

Commit

Permalink
Get blob
Browse files Browse the repository at this point in the history
  • Loading branch information
pregress committed Jan 8, 2023
1 parent 0e76fdc commit 1276751
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
9 changes: 8 additions & 1 deletion nodes/AzureBlobStorage/AzureBlobDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export const blobOperations: INodeProperties[] = [
description: 'List blobs inside a container',
action: 'Get many blobs',
},
{
name: 'Get',
value: 'get',
description: 'Get the binary data from a blob',
action: "Get a blob"
}
],

default: 'getMany',
Expand All @@ -83,6 +89,7 @@ export const blobOperations: INodeProperties[] = [
name: 'container',
type: 'string',
noDataExpression: true,
required:true,

displayOptions: {
show: {
Expand All @@ -101,7 +108,7 @@ export const blobOperations: INodeProperties[] = [
displayOptions: {
show: {
resource: ['blob'],
operation: ['upload'],
operation: ['upload', 'get'],
},
},
description:
Expand Down
44 changes: 44 additions & 0 deletions nodes/AzureBlobStorage/AzureBlobStorage.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ export class AzureBlobStorage implements INodeType {
arr.push(blob);
}
returnData.push.apply(returnData, arr as IDataObject[]);
} else if (operation === 'get') {
const blobName = this.getNodeParameter('blobName', i) as string;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const downloadBlockBlobResponse = await blockBlobClient.download();

const newItemBinary: IBinaryKeyData = {};
const buffer = await streamToBuffer(downloadBlockBlobResponse.readableStreamBody) as Buffer;
newItemBinary.data = await this.helpers.prepareBinaryData(buffer);
newItemBinary.data.mimeType = downloadBlockBlobResponse.contentType;

returnData.push({
json: {
"blobName": blobName,
"blobType": downloadBlockBlobResponse.blobType,
"contentEncoding" : downloadBlockBlobResponse.contentEncoding,
"contentLanguage" : downloadBlockBlobResponse.contentLanguage,
"contentLength" : downloadBlockBlobResponse.contentLength,
"contentType" : downloadBlockBlobResponse.contentType,
"lastAccessed" : downloadBlockBlobResponse.lastAccessed,
"lastModified" : downloadBlockBlobResponse.lastModified,
"leaseDuration" : downloadBlockBlobResponse.leaseDuration,
"leaseState" : downloadBlockBlobResponse.leaseState,
"leaseStatus" : downloadBlockBlobResponse.leaseStatus,
},
pairedItem: {
item: i,
},
binary: Object.keys(newItemBinary).length === 0 ? undefined : newItemBinary,
});

} else if (operation === 'upload') {
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;

Expand Down Expand Up @@ -131,3 +161,17 @@ export class AzureBlobStorage implements INodeType {
return [this.helpers.returnJsonArray(returnData)];
}
}


async function streamToBuffer(readableStream: NodeJS.ReadableStream) {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
readableStream.on("data", (data: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>) => {
chunks.push(data instanceof Buffer ? data : Buffer.from(data));
});
readableStream.on("end", () => {
resolve(Buffer.concat(chunks));
});
readableStream.on("error", reject);
});
}

0 comments on commit 1276751

Please sign in to comment.