Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download Azure Blob with Shared Access Signature using wget or curl

I know I can use the Azure CLI to accomplish this, but I'd like to use common Linux/Unix commands.

I have an Azure Blob Container, which is private. Let's call it https://my-account.blob.core.windows.net/my-blob-container/my-file.my-extension.

If my container is set to "Public read access for blobs only", everything works great.

$ wget https://my-account.blob.core.windows.net/my-blob-container/my-file.my-extension
--2016-04-28 16:11:15-- https://my-account.blob.core.windows.net/my-blob-container/my-file.my-extension
Resolving my-account.blob.core.windows.net (my-account.blob.core.windows.net)... 4.11.2.14
Connecting to my-account.blob.core.windows.net (my-account.blob.core.windows.net)|4.11.2.14|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 21682 (21K) [application/octet-stream]
Saving to: ‘my-file.my-extension’

If my container is set to "No public access", and I create an Access policy called my-policy, with Read/Write/Delete/List for one year, I get the following direct link: https://my-account.blob.core.windows.net/my-blob-container/my-file.my-extension?sv=2015-02-21&si=my-policy&sr=b&sig=y%2B2pvIuR9rGQdvm%2FQKp0xNCWE%2B1G%2F2IqjHkWMhRcPUA%3D

If I copy the link into a browser, the file downloads automatically. If I use wget, I get a 404 error:

$ wget https://my-account.blob.core.windows.net/my-blob-container/my-file.my-extension?sv=2015-02-21&si=my-policy&sr=b&sig=y%2B2pvIuR9rGQdvm%2FQKp0xNCWE%2B1G%2F2IqjHkWMhRcPUA%3D
[4] 14106
[5] 14107
[6] 14108
[2]   Done                    si=my-policy
[3]   Done                    sr=b
$ --2016-04-29 01:25:54--  https://my-account.blob.core.windows.net/my-blob-container/my-file.my-extension?sv=2015-02-21
Resolving my-account.blob.core.windows.net (my-account.blob.core.windows.net)... 4.11.2.14
Connecting to my-account.blob.core.windows.net (my-account.blob.core.windows.net)|4.11.2.14|:443... connected.
HTTP request sent, awaiting response... 404 The specified resource does not exist.
2016-04-29 01:25:54 ERROR 404: The specified resource does not exist..

wget returns Exit Stats 8 ("Server issued an error response").

like image 474
numeratus Avatar asked Apr 28 '16 23:04

numeratus


1 Answers

The query is difficult to debug without the specifics but...

You must make sure that the URL is enclosed in speech marks when making the wget call due to the nature of some of the URL encoding (it's the same if you're doing a curl)

I can create a SAS at the container level that allows me to read a file in storage

wget "https://azurerpkg.blob.core.windows.net/azurer/doggies.jpg?st=2016-06-06T07%3A56%3A00Z&se=2018-06-07T07%3A56%3A00Z&sp=rl&sv=2015-04-05&sr=c&sig=nKQz5BcWoUaASBzSW7Hv0TzfMbTFcf0f%2B5mMtmTwQ2A%3D" -k -O "doggies.jpg"

If I create a specific SAS at the blob level my wget also works

wget "https://azurerpkg.blob.core.windows.net/azurer/doggies.jpg?st=2016-06-06T07%3A56%3A00Z&se=2018-06-07T07%3A56%3A00Z&sp=rl&sv=2015-04-05&sr=b&sig=ntNFXS6fUlHVkiqoqj2rHg1Pw6gSNosnYqj3CQ6GIrg%3D" -k -O doggies.jpg

A valid shared access signature query string requires arguments for the following parameters:

  • st
  • se
  • sp
  • sr
  • sig

Yours appears to be missing the se component which is the expiry date.

like image 58
Steph Locke Avatar answered Oct 16 '22 16:10

Steph Locke