3

(For Azure for SDK 10) I'm able to download a file to memory, but I'd like to just download it to a blob or other local object.

  • There appears to be a download function for BlockBlobURL, but this returns a Single<> object: is there a more direct way to just get the blob contents?

  • This link describes downloading to file.

  • I am looking for the Java equivalent of this.

svenka
  • 39
  • 1
  • 5

2 Answers2

3

You could get blob content directly(not to a local file) with below sample code,please try it.

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.blob.CloudBlob;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.io.InputStreamReader;

public class GetBlobContent {

    public static final String storageConnectionString =
            "DefaultEndpointsProtocol=http;" +
                    "AccountName=***;" +
                    "AccountKey=***";

    public static void main(String[] args) {
        try {
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
            CloudBlobContainer container = blobClient.getContainerReference("jay");
            CloudBlob blob = container.getBlockBlobReference("test.txt");
            InputStream input =  blob.openInputStream();
            InputStreamReader inr = new InputStreamReader(input, "UTF-8");
            String utf8str = IOUtils.toString(inr);
            System.out.println(utf8str);

            System.out.println("download success");

        } catch (Exception e) {
            // Output the stack trace.
            e.printStackTrace();
        }
    }
}
Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • Unfortunately this is the legacy version of Azure, it seems like. I was wondering if there was a way to do this with the stable release. – svenka Nov 28 '18 at 17:16
  • @svenka Based on my researching, java sdk does't provide `get_blob_to_text` function so far like python. If you observe the download to file function which is provided on the official doc:https://github.com/twright-msft/azure-content/blob/master/articles/storage/storage-java-how-to-use-blob-storage.md#download-a-blob. It's still implemented by `FileOutputStream`. – Jay Gong Nov 30 '18 at 01:49
  • 1
    Hi @jay-gong, thank you for checking in! I ended up using the the lower level download API available in the BlobURL class. It was finicky since it returns Single. I shall add an answer with my workaround :) – svenka Dec 03 '18 at 18:13
0

BlobURL had a low level interface that I could extract the byte stream from. This was my workaround:

ByteBuffer byteBuffer = blobURL.download(null, null, false, null)
                               .blockingGet()   // DownloadResponse
                               .body(null)      // Flowable<ByteBuffer>
                               .firstOrError()  
                               .blockingGet();  
svenka
  • 39
  • 1
  • 5