Azure CLI下載Azure Storage Container內的所有檔案

衡子發表於2016-02-14

在某些場景下,客戶需要把Azure Storage的某一個container內的內容都下載到本地。當然採用PowerShell可以定時的進行下載的動作,但有時客戶的環境是Linux或MacOS,這時需要採用Azure xplate CLI實現這個功能。本文將介紹如何採用Azure CLI實現這個功能。

安裝Azure CLI,可以參考:

http://www.cnblogs.com/hengwei/p/5183493.html

http://www.cnblogs.com/hengwei/p/4781332.html

安裝完成後,通過Azure的命令顯示Storage Account的資訊:

azure storage account list

info: Executing command storage account list

+ Getting storage accounts

data: Name Type Label Location Resource Group

data: ------------------------ ------------ ----- ----------- --------------------------

data: hwtest Standard_LRS China East Default-Storage-ChinaEast

info: storage account list command OK

獲得Storage Account的Key:

azure storage account keys list hwtest

info: Executing command storage account keys list

+ Getting storage account keys

data: Primary: pBHrx8d+LDAkyHm2ffljPYygsiSBlbdQh8O45iV12BlFvdjI8kXbqtE17PlpCG0pfTU3yaBQUEEuWuM0tIsobw==

data: Secondary: ss2PunnyTve2sT8R3vaNFJTIcYz0ehmJreKKvmcMSEwSuymbLNqnwDqKznW9Kh03EtZl6fIGAufcT3g+c1UWzw==

info: storage account keys list command OK

先將這個Container的許可權設定成Public的模式:

azure storage container set --container hwc -p Container -a hwtest –k pBHrx8d+LDAkyHm2ffljPYygsiSBlbdQh8O45iV12BlFvdjI8kXbqtE17PlpCG0pfTU3yaBQUEEuWuM0tIsobw==

 

info: Executing command storage container set

+ Set container

+ Getting Storage container information

data: {

data: name: 'hwc',

data: metadata: {},

data: etag: '"0x8D3351F16611905"',

data: lastModified: 'Sun, 14 Feb 2016 09:13:22 GMT',

data: leaseStatus: 'unlocked',

data: leaseState: 'available',

data: requestId: '6f3b08c9-0001-0048-7707-675863000000',

data: publicAccessLevel: 'Container'

data: }

info: storage container set command OK

獲得這些資訊後,可以通過指令碼的方式實現批量檔案的下載:

#!/bin/bash

container=hwc
btype=block
storageaccount=hwtest
storagekey=pBHrx8d+LDAkyHm2ffljPYygsiSBlbdQh8O45iV12BlFvdjI8kXbqtE17PlpCG0pfTU3yaBQUEEuWuM0tIsobw==

files=`azure storage blob list -a $storageaccount -k $storagekey $container |awk '/BlockBlob/ {print $2}'`

for file in $files;
do
{
wget https://$storageaccount.blob.core.chinacloudapi.cn/$container/$file
};
done

 

 

但這樣有一個問題,是需要把整個資料夾變成Public的模式。存在一定的安全隱患。

解決這個問題,可以採用SAS Token的方式實現。

可以通過下面這個指令碼進行改進:

#!/bin/bash
container=hwc
btype=block
storageaccount=hwtest
storagekey=pBHrx8d+LDAkyHm2ffljPYygsiSBlbdQh8O45iV12BlFvdjI8kXbqtE17PlpCG0pfTU3yaBQUEEuWuM0tIsobw==
starttime=2016-02-13
endtime=2016-02-15

files=`azure storage blob list -a $storageaccount -k $storagekey $container |awk '/BlockBlob/ {print $2}'`

for file in $files;
do
{
sastokey=`azure storage blob sas create --container $container --blob $file --permissions rwd --start $starttime --expiry $endtime -a $storageaccount -k $storagekey | awk '/URL/ {print $5}'`
wget $sastokey -O $file
};
done

 

相關文章