Search code examples
c#asp.netwebformsazure-storagefilestream

Filestream a blob from Azure storage


I have images in Azure that I need to add in a pdf using pdfJet.

This is the code that I use if I read an image on disk, however I have a lot of images and it does not make sense to download them from Azure.

Image image = new Image(objects, new BufferedStream(new FileStream(LocalPath + "image.PNG", FileMode.Open, FileAccess.Read)), ImageType.PNG);

PS: This is done in asp.net webforms.

Thank you for your help.

I am now using the following function to read a PDF:

    public MemoryStream DownloadToMemoryStream(DTO.BlobUpload b)
    {
        CloudStorageAccount storageAccount = Conn.SNString(b.OrgID);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(b.Container);
        CloudBlockBlob blob = container.GetBlockBlobReference(b.FileName);
        var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
        {
            Permissions = SharedAccessBlobPermissions.Read,
            SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
        }, new SharedAccessBlobHeaders()
        {
            ContentDisposition = "attachment; filename=file-name"
        });
        using (MemoryStream ms = new MemoryStream())
        {
            blob.DownloadToStream(ms);
            return ms;
        }

    }

And in the aspx page I use:

MemoryStream pdfScript = B.DownloadToMemoryStream(b);

to read the stream:

SortedDictionary<Int32, PDFobj> objects = pdfFinalScript.Read(pdfScript);

However I get the error message: Cannot access a closed Stream

I have looked at how to open the stream but haven't managed to do it.

Could you please help, thank you


Solution

  • According to your description, you would download blobs from Azure. Here are several ways you could refer to.

    1.Download with blob url.

    Create a Shared Access Signature with Read permission and Content-Disposition header set and create blob URL based on that and use that URL. In this case, the blob contents will be directly streamed from storage to the client browser.

    2.Get the blob and DownloadFileFromBlob.

    3.Download file to exactly path in local.

    Webform:

    You could use Response.Redirect(blobUrl); to redirect blob url and download it.

    In .aspx:

    <asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" /> 
    

    In aspx.cs:

    protected void Button1_Click(object sender, EventArgs e)
            {
                CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("accountname", "accountkey"), true);
                var blobClient = account.CreateCloudBlobClient();
                var container = blobClient.GetContainerReference("container");
                var blob = container.GetBlockBlobReference("text.PNG");
                var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
                {
                    Permissions = SharedAccessBlobPermissions.Read,
                    SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
                }, new SharedAccessBlobHeaders()
                {
                    ContentDisposition = "attachment; filename=file-name"
                });
                using (MemoryStream ms = new MemoryStream())
                {
                    blob.DownloadToStream(ms);
                    Image image = new Image(objects, ms, ImageType.PNG);
                }
            }