Hi There,
I am trying to upload images to Azure blob storage, but am getting the error "No 'Access-Control-Allow-Origin' header is present on the requested resource." It is driving me crazy.
I am currently using local host to access the server, so it may have to do with cross domain policies. The steps I have taken so far are:
(1) I use a table (via azure mobile services) for generating a unique ID for the filename (i.e. inserting into the table, I can use the ID field). While I do this, I also generate a SAS for the azure storage location. This code seems fine.
(2) In uploading the image file from html, I use the following code:
var uriWithAccess = item.imageUri + "?" + item.sasQueryString;
var xhr = new XMLHttpRequest();
xhr.onerror = fail;
xhr.onloadend = uploadCompleted;
xhr.open("PUT", uriWithAccess);
xhr.setRequestHeader('x-ms-blob-type', 'BlockBlob')
xhr.setRequestHeader('x-ms-blob-content-type', 'image/png');
xhr.send(document.getElementById('fileToUpload').files[0]);
The SAS string looks file, but returns the following error...
XMLHttpRequest cannot load http://mystorageaccount.blob.core.windows.net/mycontainer/aabbccdd.png?se=2015-0%3A02%3A23Z&sr=b&sp=w&sig=sMgAoczzA%2BQFq6eIw4EFyfK7Qwdet7b135Jx8HH3xgJo%3D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8383' is therefore not allowed access. The response had HTTP status code 403.
I am thinking that this may be because the CORS for the blob storage has not been sent accordingly. I tried the following (from the node.js server) to set the permissions...
var accessKey = 'w79zXXXXXXXRg=='; var storageAccount = 'mystorageaccount'; var blobService = azureStorage.createBlobService(storageAccount, accessKey); console.log("service created"); var serviceProperties = { Cors: { CorsRule: [{ AllowedOrigins: ['*'], AllowedMethods: ['GET'], AllowedHeaders: [], ExposedHeaders: [], MaxAgeInSeconds: 60 }] } }; var callback = function () { console.log("changed the cors rule"); request.execute(); // this is included as it was part of a table insert call } console.log("about to set properties"); blobService.setServiceProperties(serviceProperties, callback); console.log("properites set");
I am assuming the above should have granted external permissions to the site.
This is really doing my head in.
Any suggestions greatly appreciated.
Cheers,
Scott