0
votes

Just wanted to ask a quick question.

I want users to upload blobs directly to blob storage. Heres what I came up with:

public ActionResult Upload(HttpPostedFileBase file)

    CloudBlobContainer blobContainerSas = new CloudBlobContainer(new Uri(blobSasUri));
    CloudBlockBlob blob = blobContainerSas.GetBlockBlobReference(BLOBNAMEHERE);
    blob.UploadFromStream(file.InputStream);

Before this code i set the blobSasUrl, (I left that part out).

I was wondering if blob.UploadFromStream(file.InputStream); is correct? Am i uploading directly now?

Im using SAS URI for the uploading blob, (not container). Is this even possible? The blob isnt even uploaded yet... Should i be using SAS URI for container instead? //Confused

Thanks!

1

1 Answers

2
votes

No, in the example above your users are not directly uploading files in the storage account. They are uploading a file from their browsers to your web server by calling Upload action which in turn uploads the data into storage.

To directly upload the data into storage via client browser, you would need to make use of concepts like CORS and AJAX. First you would need to enable CORS on your storage account. This is a one time operation which you would need to do so that browsers can directly interact with your storage.

When setting up CORS Settings, you could start with the following CORS settings:

Allowed Domains: your website address (you could also specify * which would mean all websites have access to your storage. Could be recommended if you're really starting out but once you have grasped the concepts, you should always specify very specific website addresses)

Allowed Methods: PUT, POST (this indicate the HTTP verbs you would be using in your JavaScript AJAX calls)

Allowed Headers: * (* means all headers sent by browsers are allowed. You should stick to that as different browsers tend to send different headers which makes debugging quite hard)

Exposed Headers: * (* means all headers will be sent by storage service to the browser)

Max Age in Seconds: 3600

Once CORS is enabled, you could write an application where using JavaScript/AJAX your users will directly upload a file in your storage account.

You may want to read this blog post about understanding CORS concepts in Azure: http://msdn.microsoft.com/library/azure/dn535601.aspx.

I have also written a blog post about Azure and CORS where I have demonstrated uploading a file from browser using JavaScript/AJAX which you can read here: http://gauravmantri.com/2013/12/01/windows-azure-storage-and-cors-lets-have-some-fun/

For configuring CORS rules, you may find this free utility developed by my company useful: http://blog.cynapta.com/2013/12/cynapta-azure-cors-helper-free-tool-to-manage-cors-rules-for-windows-azure-blob-storage/