1gentlemann
1gentlemann

Reputation: 176

Download Azure DevOps work item attachments using node js and azure-devops-node-api

Got stuck while trying to download an attachment of a work item (Azure DevOps).

I'm Using node.js 'azure-devops-node-api' client (https://www.npmjs.com/package/azure-devops-node-api) to interact with ADO API. I get a certain workItem using WorkItemTracking client (wit):

let workItem = await witApi.getWorkItem(1234, undefined, undefined, WorkItemExpand.All);

let attachment = await witApi.getAttachmentContent(attachmentId, fileName, projectId, true);

Documentation states that getAttachmentContent method downloads an attachment (https://github.com/microsoft/azure-devops-node-api/blob/ff820b2dd0c9a09cf09e64e94d3f95818a77249d/api/WorkItemTrackingApi.ts#L392), as a return value I'm getting a ReadableStream which i tried to write to a file using standard fs module:

fs.writeFile('WordDoc.docx', attachment, function (err) {if (err) return console.log(err);});

File is created but is empty. While debugging i also see that attachment variable has type ReadableStream but inside has lot's of properties and values, among them there is a buffer which i actually would like to extract and pass to fs.writeFile but can't reach them

enter image description here

What am i doing wrong ?

Upvotes: 1

Views: 545

Answers (1)

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

I believe you should write using WritableStream. As the getAttachmentContent is returning a ReadableStream. Below is the pseudo code. It might work

let readableStream = await witApi.getAttachmentContent(attachmentId, fileName, projectId, true);
let writableStream = fs.createWriteStream('./WordDoc.docx');
readableStream.pipe(writableStream);

Upvotes: 2

Related Questions