0

I'm creating a website that needs to be able to retrieve files from an Azure Storage Container that I have and I'm having difficulty actually retrieving any files.

So the container's Access Policy is set to the Private (no anonymous access) value since I don't want just anyone to be able to access it. I then went into CORS settings and created a new CORS rule. For now I setup the options as follows:

ALLOWED ORIGINS == *
ALLOWED METHODS == GET
ALLOWED HEADERS == *
EXPOSED HEADERS == *
MAX AGE == 10000

I think I'm supposed to put my website into the ALLOWED ORIGINS field to restrict access to only my website, but for now I'm leaving it just with an * to get this started.

So I think this should allow me to get blobs from my container...

Here is how I'm attempting to retrieve the files in PHP

$response = file_get_contents("https://<accountname>.blob.core.windows.net/<containername>/SomeSimpleFile.txt");
print_r($response );

But I don't get the contents of the file back, instead I get the following error messages:

[01-Aug-2018 12:58:35 America/Los_Angeles] PHP Warning:  file_get_contents(https://<accountname>.blob.core.windows.net/<containername>/SomeSimpleFile.txt): failed to open stream: HTTP request failed! HTTP/1.1 404 The specified resource does not exist.
in D:\home\site\wwwroot\index.php on line 30

If I go back into the Azure Portal and change the container's access policy to Blob (anonymous read access for blobs only) then I am able to get the file using the above PHP code. So I know the url I'm using for the file is correct. It seems I have set something wrong in the CORS settings. Or maybe it's something else.

Any help I can get on this is appreciated.

3

1 Answer 1

1

The problem is caused by visiting a private blob using its plain URL.

You could use shared access signature. With a SAS, you can grant clients access to resources in your storage account.

Go to portal, your storage account, locate the blob in container. Right click on the blob, select Generate SAS. You may see content below.

enter image description here

You can set the permission, expiration, IP allowed, etc. Then Generate blob SAS token and URL, replace your blob url with Blob SAS URL and things should work.

Sign up to request clarification or add additional context in comments.

1 Comment

wow that's so much easier than figuring out all of the header stuff. Instead of creating a SAS for a single file I ended up creating a SAS for the entire container and I'm able to use that with all of the blobs. I can even use it for listing operations too. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.