3

For our testing purpose, we would like to access Azure storage queue directly with JavaScript instead of preparing a new web service.

Is this possible? What should we do to achieve this, since I cannot find the official documentation for JavaScript API of Azure storage.

Jerry Bian
  • 3,998
  • 6
  • 29
  • 54

1 Answers1

6

Yes, it is certainly possible. In fact, I am currently developing a service which does exactly this.

Step 1: Enable CORS for Queue Service

To accomplish this, first you need to enable CORS settings on your Queue Service. You may find this blog post useful for CORS settings: http://blogs.msdn.com/b/windowsazurestorage/archive/2014/02/03/windows-azure-storage-introducing-cors.aspx. You would have to make following settings:

Allowed Origin: Your domain name

Allowed Verbs: I would start with all the possible verbs but do take a look at REST API documentation for messages and see which operations you wish to perform and allow only those verbs.

Allowed Headers: *

Exposed Headers: *

Step 2: Get a Shared Access Signature for Queue

Next, you would need to create a Shared Access Signature (SAS) on a queue and set appropriate permissions. For setting up SAS on Queues, you could make use of Azure Storage Client library. You may find this blog post useful for learning more about SAS on Queues: http://blogs.msdn.com/b/windowsazurestorage/archive/2012/06/12/introducing-table-sas-shared-access-signature-queue-sas-and-update-to-blob-sas.aspx.

Step 3: Access your Queue

Once SAS URL is created, you can take that URL and start using it via jQuery/AJAX in your web application.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • I'd like to do all above stuffs in JavaScript. So first thing is how can I get a SAS in js? – Jerry Bian Jul 11 '14 at 07:58
  • For SAS, you would need account key. So what you could do is have a simple service which will return you SAS. You'll then call this service via JS. – Gaurav Mantri Jul 11 '14 at 08:12
  • Too bad, our boss just want everything via JavaScript, we could put everything using web service if we can. – Jerry Bian Jul 11 '14 at 08:18
  • I mean you could still do it but you would have to write the code to create SAS token in JavaScript. Not to mention you're exposing your account key on the user's browser. Do take a look at these links: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx & http://msdn.microsoft.com/en-us/library/azure/dn140256.aspx. So you would need to create a `stringToSign` string and then sign it with your account key using HMAC-SHA256. – Gaurav Mantri Jul 11 '14 at 09:17
  • Hi, I've tried all what I can find from the internet. I just follow http://convective.wordpress.com/2010/08/18/examples-of-the-windows-azure-storage-services-rest-api/ and it always said 403 authorization failed, can you send me a complete demo to show how to create azure queue with REST api if possible? Use .NET is ok, thanks very much, and by the way, please forward to my personal address (JerryBian@outlook.com) , since I cannot use company email during the weekends. – Jerry Bian Jul 11 '14 at 13:54
  • Hi, pls have a look at https://gist.github.com/JerryBian/d4368a980870b7c286d4 , very appreciate. – Jerry Bian Jul 12 '14 at 03:42
  • The reason your code is failing is because `httpClient.PutAsync` is putting content-type as `text/plain; charset=utf-8` however in your `stringToSign` you're passing empty content type. Try the following for stringToSign and that should work: `var stringToSign = string.Format( "{0}\n\n\n{1}\n\ntext/plain; charset=utf-8\n\n\n\n\n\n\n{2}\n{3}", requestMethod, 0, canonicalizedHeaders, canonicalizedResource);` – Gaurav Mantri Jul 12 '14 at 12:06
  • But one quick question ... if you're writing .Net code, why consume REST API? Why not just use Storage Client Library? – Gaurav Mantri Jul 12 '14 at 12:07
  • Thanks very very much, the reason I use REST api for .NET is I would like to make sure I invoked correctly, so that I can move everything to JavaScript, but I think I have solved the hardest part with the help of you. – Jerry Bian Jul 12 '14 at 16:18
  • can you also help me look at why https://gist.github.com/JerryBian/064dbe77502c4d81b8d4 cannot authorize successfully, I've spent an hour to figure out the issue, but failed. Driven me crazy again. Thanks. – Jerry Bian Jul 12 '14 at 17:25
  • You forgot to add "/" after storage account name in `canonicalizedResource` :). It should be `var canonicalizedResource = string.Format("/{0}/\ncomp:properties\nrestype:service", StorageAccount);`. Since we are digressing from the main question, may I suggest you post them as separate question? HTH. – Gaurav Mantri Jul 12 '14 at 17:48