Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

downloadBlockBlobResponse.readableStreamBody in node js not downloading the whole file #5260

Closed
3 tasks
kriti218 opened this issue Sep 26, 2019 · 11 comments
Closed
3 tasks
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files)

Comments

@kriti218
Copy link

  • Package Name: @azure/storage-blob
  • Package Version: 10.5
  • Operating system: Windows 10
  • nodejs
    • version: 10
  • typescript
    • version:
  • Is the bug related to documentation in
    • README.md

Describe the bug
i am following what is mentioned in "getting started" and code sample. My requirement is to download a zip file from blob to a predefined location and upload it in release pipeline of Azure devops task.
The issue is using this command
var data = downloadBlockBlobResponse.readableStreamBody;
fileSystem.writeFile(fileName, data);

is copying only 1kb data and exiting. Can you please let me know what is wrong here.

@loarabia loarabia added Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files) labels Sep 26, 2019
@ghost
Copy link

ghost commented Sep 26, 2019

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @xgithubtriage

1 similar comment
@ghost
Copy link

ghost commented Sep 26, 2019

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @xgithubtriage

@kriti218
Copy link
Author

If anyone can help ASAP that will be great as I am blocked and see no other way out reading the limited documentation provided on your site

@chradek
Copy link
Contributor

chradek commented Sep 27, 2019

@kriti218
I believe since you're using the readableStreamBody, you'll want to first create a writeable stream to the filesystem using fs.createWriteStream then pipe the readableStreamBody into that:

// error handlers should also be added
const fileStream = fs.createWriteStream("/path/to/destination");
downloadBlockBlobResponse.readableStreamBody.pipe(fileStream);

There is also the downloadToFile method to make downloading a blob to a file easier as long as the file doesn't already exist on your system.

@zhusijia26 zhusijia26 added the question The issue doesn't require a change to the product in order to be resolved. Most issues start as that label Sep 27, 2019
@kriti218
Copy link
Author

kriti218 commented Oct 3, 2019

Thanks @chradek for unblocking me. Can i give "sas url to the blob file" in place of blob file path in "downloadToFile" method?

@jeremymeng
Copy link
Member

@kriti218 you can create an instance of BlobClient directly using the constructor that takes a url (the doc for overloads doesn't show until you click on them though) , then call the downloadToFile function
image

@kriti218
Copy link
Author

kriti218 commented Oct 3, 2019

i did that but i am getting error "BlobClient is not a constructor".

import * as Azure from "@azure/storage-blob";
BlobClient seems to be absent from "Azure' imported from package. Any inputs please?

var blobClient = new BlobClient('sas url of blob file');

@chradek
Copy link
Contributor

chradek commented Oct 3, 2019

@kriti218
Apologies, downloadToFile is available on the preview version of the @azure/storage-blob SDK (version 12), not version 10.x.

To accomplish what you want to do with version 10.5 of the SDK, you can do something like the following:

const { createWriteStream} = require('fs');
const { join } = require('path');
const { BlobURL, Aborter, StorageURL, AnonymousCredential } = require('@azure/storage-blob');

async function run() {
  const blobUrl = new BlobURL(
    'MY-SAS-CONNECTION-STRING',
    StorageURL.newPipeline(new AnonymousCredential())
  );
  
  const blob = await blobUrl.download(Aborter.none, 0);
  // does not check if file already exists on system
  await downloadToFile(join(__dirname, 'test.txt'), blob);
}

function downloadToFile(path, blob) {
  return new Promise((resolve, reject) => {
    const fileStream = createWriteStream(path);
    blob.readableStreamBody.on('error', reject);
    fileStream.on('error', reject);
    fileStream.on('close', resolve);
    blob.readableStreamBody.pipe(fileStream);
  });
}

run();

If you're interested in trying out the preview version of the SDK, you can install it by running npm install @azure/storage-blob@next. There may be breaking changes between preview versions, but we'd love to get feedback if you have time to try it!

Here's what the equivalent code looks like in the preview version of the SDK:

const { join } = require('path');
const { BlobClient } = require('@azure/storage-blob');

async function run() {
  const client = new BlobClient(
    'MY-SAS-CONNECTION-STRING'
  );

  // Checks if file exists on system and throws error if it does
  await client.downloadToFile(join(__dirname, 'test.txt'));
}

run();

@kriti218
Copy link
Author

kriti218 commented Oct 4, 2019

The preview version looks far better but i have to deploy these changes in production so would not be willing to use "preview" version as that might break because of continuous changes. Thanks!

One feedback, please improve the documentation by giving some examples as it is easy to understand.

@kartikshah9
Copy link

@XiaoningLiu can we consider closing this issue as we have provided guidance on the approach to take with V12 SDK for js

@XiaoningLiu
Copy link
Member

Hi, in latest commits, we provided detialed samples and references for storage SDKs. Will close this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files)
Projects
None yet
Development

No branches or pull requests

7 participants