2
votes

Is there a way for a user of an MVC web application to avoid having to upload the file thorough the MVC application and eventually for the application to transfer it to storage?

In other words, could the web client be given the proper SAS token to upload it directly into the proper location in Azure Blob Storage some how?

I have seen examples of a client app copying directly to blob storage, but can't find anything on a web app. THANK YOU!

3
Try picking through the Azure SDK for node.js to find the Javascript you are looking for. github.com/WindowsAzure/azure-sdk-for-nodeSimon Munro

3 Answers

2
votes

At this time it's not possible because Windows Azure Storage does not have CORS support. However during a presentation at \Build conference storage team indicated that it is coming. One way to achieve this is by hosting the HTML page for upload in that storage account only as mentioned in the link by @viperguyz and use SAS for uploading blobs in that storage account. If you want, you could map a custom domain to your blob storage account and use that domain name. The problem with custom domain name is that you won't be able to use SSL.

0
votes

You can upload from the client without touching the MVC site using JavaScript, I have written a blog post with an example on how to do this http://blog.dynabyte.se/2013/10/09/uploading-directly-to-windows-azure-blob-storage-from-javascript/ the code is at GitHub

It is based on Gaurav Mantris example and works by hosting the JavaScript on the Blob Storage itself.

-1
votes

sure - here's an example:

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

    private string UploadFileToBlob(string file)
    {
        // Retrieve storage account from connection string
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the blob client
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve reference to a previously created container
        CloudBlobContainer container = blobClient.GetContainerReference("mydeployments");

        // Retrieve reference to a blob named "myblob"
        var date = DateTime.UtcNow.ToString("yyyyMMdd-hhmmss-");
        var fileinfo = new FileInfo(file);
        if (fileinfo.Exists)
        {
            var fileToUpload = new FileInfo(file).Name;
            var filename = date + fileToUpload;
            try
            {
                CloudBlob blob = container.GetBlobReference(filename);

                // Create or overwrite the "myblob" blob with contents from a local file
                using (var fileStream = System.IO.File.OpenRead(file))
                {
                    blob.UploadFromStream(fileStream);
                }

                return blob.Uri.AbsoluteUri;
            }
            catch (Exception ex)
            {
                LogError("Error uploading file to blog: ", ex.Message);
                return "";
            }
        }

        LogError("Error - specified file does not exist: ", file);
        return "";
    }