Azure CLI下载Azure Storage Container内的所有文件

在某些场景下,客户需要把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

 

你可能感兴趣的:(Azure CLI下载Azure Storage Container内的所有文件)