在某些场景下,客户需要把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=--
endtime=-- 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内的所有文件的更多相关文章

  1. 使用 Azure CLI 在 Azure China Cloud 云平台上手动部署一套 Cloud Foundry

    这篇文章将介绍如何使用 Azure CLI 在 Azure China Cloud 云平台上手动部署一套 Cloud Foundry.本文的目的在于: 了解作为 PaaS 的 Cloud Foundr ...

  2. 使用 Azure CLI 管理 Azure 虚拟网络和 Linux 虚拟机

    Azure 虚拟机使用 Azure 网络进行内部和外部网络通信. 本教程将指导读者部署两个虚拟机,并为这些 VM 配置 Azure 网络. 本教程中的示例假设 VM 将要托管包含数据库后端的 Web ...

  3. 使用 Azure CLI 管理 Azure 磁盘

    Azure 虚拟机使用磁盘来存储 VM 操作系统.应用程序和数据. 创建 VM 时,请务必选择适用于所需工作负荷的磁盘大小和配置. 本教程介绍如何部署和管理 VM 磁盘. 学习内容: OS 磁盘和临时 ...

  4. 使用 Azure CLI 2.0 从自定义磁盘创建 Linux VM

    本文说明如何在 Azure 中上传自定义的虚拟硬盘 (VHD) 或复制现有 VHD,并从自定义磁盘创建 Linux 虚拟机 (VM). 可以根据要求安装并配置 Linux 分发版,并使用该 VHD 快 ...

  5. Azure CLI的版本问题

    Azure支持多种管理方法.命令行方法有: PowerShell,PowerShell只能运行在Windows上 Azure CLI,而Azure CLI可以运行在Windows.MAC以及Linux ...

  6. 使用Azure CLI创建Linux虚拟机

    Azure提供了三种方式创建虚拟机,分别是Azure CLI.Azure PowerShell和Azure门户.本文介绍使用Azure CLI来创建Linux虚拟机. 使用Azure CLI创建Lin ...

  7. Linux上使用Azure CLI来管理Azure

    在Windows上我们有强大的Powershell提供各种命令来管理Azure的服务,在Linux上微软提供了基于Node.JS的跨平台的Azure Command Line来帮助Linux用户来管理 ...

  8. [Azure Storage]使用Java上传文件到Storage并生成SAS签名

    Azure官网提供了比较详细的文档,您可以参考:https://azure.microsoft.com/en-us/documentation/articles/storage-java-how-to ...

  9. Azure 基础:Blob Storage

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在前文中介绍了 Table Storage 的基本 ...

随机推荐

  1. DatagramSocket总是发送UDP数据后无法接收数据

    ref:http://blog.chinaunix.net/uid-20771867-id-3416509.html         cmd:telnet localhost 5554        ...

  2. mysql存储引擎(待补充)

    数据库中的表也应该有不同的类型,表的类型不同,会对应mysql不同的存取机制,表类型又称为存储引擎 存储引擎说白了就是如何存取数据.如何为存储的数据建立索引和如何更新.查询数据等技术的实现方法.因为在 ...

  3. Python基础(2)_数字和字符串类型

    一.数据类型 1.数字 整型 Python的整型相当于C中的long型,Python中的整数可以用十进制,八进制,十六进制表示. >>> --------->默认十进制 > ...

  4. 【Flask模板】include标签

    # include标签:1. 这个标签相当于是直接将指定的模版中的代码复制粘贴到当前位置.2. `include`标签,如果想要使用父模版中的变量,直接用就可以了,不需要使用`with context ...

  5. Python核心编程 课后练习 第二章

    2.4 使用raw_input()函数得到用户输入. (a) 创建一段脚本使用raw_input()函数从用户输入得到一个字符串, 然后显示这个用户杠杠输入的字符串. #coding = utf-8 ...

  6. 面试问题(HTML和CSS方面)

    1 IE/Win的 HasLayout 2 浮动 float 的定义.float后元素的display属性会发生改变吗?3 CSS 3.0.CSS2.1 中被现代浏览器应用了的规则有哪些?4 父元素定 ...

  7. China sets economic reform priorities for 2015

    BEIJING -- China's State Council, the cabinet, on Monday unveiled this year's priorities for economi ...

  8. Spring Cloud之搭建动态Zuul网关路由转发

    传统方式将路由规则配置在配置文件中,如果路由规则发生了改变,需要重启服务器.这时候我们结合上节课内容整合SpringCloud Config分布式配置中心,实现动态路由规则. 将yml的内容粘贴到码云 ...

  9. 在cmd中将FAT32转换为NTFS分区的命令是什么?

    将FAT32转换为NTFS分区的命令是什么? ========================================== 在cmd命令行模式下输入: convert x: /fs:ntfs ...

  10. BZOJ 2527 [Poi2011]Meteors:整体二分

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2527 题意: 有n个国家和m个空间站,每个空间站都属于一个国家,一个国家可以有多个空间站, ...