C# Azure Storage download issue

Viewed 54

I'm working on moving our files from the database to Azure Storage for the files themselves. We are keeping the folder structure and file information in our SQL DB.

The control we are using is an ASPxFileManager from Dev Express which does not allow you do to async functionality because it isn't built in.

The code I'm using to pull the blob from Azure and return a stream is below:

            using (var ms = new MemoryStream())
            {
                var result = blobItem.DownloadStreaming().GetRawResponse();

                result.ContentStream.CopyTo(ms);

                return ms.ToArray();
            }

It appears on large files, it is downloading it to the server to get the stream AND then sending that stream to the client. So I think it is processing twice.

            BlobContainerClient container = GetBlobContainer(containerInfo);

            BlobClient blobItem = container.GetBlobClient(fileSystemFileDataId);

            using (var ms = new MemoryStream())
            {
                blobItem.DownloadTo(ms);

                return ms.ToArray();
            }

I looked at using their CloudFileSystemProviderBase to use GetDownloadURL, but it only allows you to download single files. This works fine for single files as we can return a url with a SAS token etc.

We still need to support downloading multiple files though.

Is there a way in azure to NOT download to the file system and just obtain ONE stream to send back for the Dev Express ASPxFileManager to process?

I liked the GetDownloadUrl call from their CloudFileSystemProviderBase, because it doesn't block the main UI thread and allows the user to continue to work in the app while large files are downloading.

Main question: Is there a way to return a stream from azure where it does not have to download to the server first?

(Note: I've already been talking to DevExpress about this issue)

UPDATE 2:

The code below obtains it to a stream, but does it download it on the server and then sends it to the client? Or just does the obtain the stream once? I think the code above I was using does it twice?

Also, this code uses the WindowsAzure.Storage, which is depecated.

So what would be the nuget/C# code for the correct way now days?

  if (!String.IsNullOrWhiteSpace(contentDisposition.FileName))
                {
                    string connectionString = ConfigurationManager.AppSettings["my-connection-string"];
                    string containerName = ConfigurationManager.AppSettings["my-container"];
                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);

                    CloudBlockBlob blob = blobContainer.GetBlockBlobReference(contentDisposition.FileName);
                    stream = blob.OpenWrite();
                }
0 Answers
Related