backblaze icon indicating copy to clipboard operation
backblaze copied to clipboard

Download via Stream

Open Nikey646 opened this issue 6 years ago • 3 comments

It shouldn't be a requirement to download a file to a local path. There should be the capability of downloading the file to any writable stream.

The best (in my opion) way to handle this would be creating a download method that returns the response stream that correlates to the file. EG, if using a Http Client, returning response.Content.ReadAsStreamAsync() might be an option.

Nikey646 avatar Dec 06 '19 06:12 Nikey646

The direct stream along with the streams progress (IProgress) can be access using the "DownloadAsync" method in root of the client object. Here is an example:

var files = new string[] { @"c:\my\directory\file1.txt", "file2.bat" };

foreach (var fileName in files)
{
  using (var stream = File.Create(fileName))
  {
      var results = await Client.DownloadAsync("[BucketName]", fileName, stream);
  }
}

microcompiler avatar Dec 08 '19 16:12 microcompiler

This doesn't quite achieve my objective in the end. UItimately, I wanted to start the download request but not actually download any part of the file, because it was just to check if the file existed. In the end, i just ended up implementing a manual HTTP Head request to check the status code (looking for a successful status code to indicate the file existed).

Nikey646 avatar Dec 08 '19 21:12 Nikey646

Got it. I have a better understanding of what you are trying to do. You can download the status/results of a file by passing "null" in the stream. This will automatically handle passing a head request HttpCompletionOption.ResponseHeadersRead and will not download any content. Here is an example:

var results = await Client.DownloadAsync("[BucketName]", "file.txt", null) 

However, to download the actual content you will have to execute the request again without a "null" stream. Agreed not the cleanest way to handle it.

microcompiler avatar Dec 09 '19 03:12 microcompiler