0
votes

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.

1
Are you setting the request Headers correctly? Basically if it is a private blob then you need Authorizarion Header I guess.Daredevil
It would appear that I am not... I found this on the header stuff looks like I have some more steps to take docs.microsoft.com/en-us/rest/api/storageservices/…Stanton

1 Answers

1
votes

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.