From 11ada8c6637ef75fe86019c0786d90a1d86f1bed Mon Sep 17 00:00:00 2001 From: Joao Eurico Date: Tue, 10 Oct 2023 10:11:32 -0300 Subject: [PATCH 1/2] reading the file via stream --- examples/client/node.js/storeBlob.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/examples/client/node.js/storeBlob.js b/examples/client/node.js/storeBlob.js index c32eb4b2e8..8a66a21c0f 100644 --- a/examples/client/node.js/storeBlob.js +++ b/examples/client/node.js/storeBlob.js @@ -6,10 +6,18 @@ const token = 'API_KEY' // your API key from https://nft.storage/manage async function main() { const storage = new NFTStorage({ endpoint, token }) - const data = await fs.promises.readFile('pinpie.jpg') - const cid = await storage.storeBlob(new Blob([data])) - console.log({ cid }) - const status = await storage.status(cid) - console.log(status) + let data = "" + const readStream = fs.createReadStream('pinpie.jpg') + // on data will receive data from the stream and concatenate it to the variable data + readStream.on('data', (chunk) => { + data += chunk + }) + // on end will finish the stream + readStream.on('end', async () => { + const cid = await storage.storeBlob(new Blob([data])) + console.log({ cid }) + const status = await storage.status(cid) + console.log(status) + }) } main() From 099b1eaa30d3ca45ae54ee1a1099c43eab9be071 Mon Sep 17 00:00:00 2001 From: Joao Eurico Date: Tue, 10 Oct 2023 10:12:33 -0300 Subject: [PATCH 2/2] added error handling --- examples/client/node.js/storeBlob.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/client/node.js/storeBlob.js b/examples/client/node.js/storeBlob.js index 8a66a21c0f..be5ee80a6e 100644 --- a/examples/client/node.js/storeBlob.js +++ b/examples/client/node.js/storeBlob.js @@ -19,5 +19,10 @@ async function main() { const status = await storage.status(cid) console.log(status) }) + // on error will show what happened if something goes wrong + readStream.on('error', (error) => { + console.log({ error }) + }) + } main()