Azure文件共享服务提供了多种方式的访问接口,包括Powershell,.Net, Java, Python等等,本章主要介绍如何使用Python来访问Azure File存储。

  1. 关于Python环境的安装,Azure SDK for python的安装配置,Linux和Windows上的模块升级,请参考博客:

    http://cloudapps.blog.51cto.com/3136598/1772880

  2. 首先,导入Azure storage file所需要的模块:

    from azure.storage.file import FileService
    from azure.storage.file import ContentSettings

  3. 默认的service endpoint是指向global Azure的,所以首先需要设置中国Azure的服务URL,另外也需要设置你的存储账户,存储的key等基本信息
    endpointSuffix = "core.chinacloudapi.cn"
    myStorageAccount = "mystorageacctfile"
    myStorageKey = "Your account key"
    mysharename = "mypythonshare"
    mydirname = "sampledir"
  4. 初始化你的文件service:
    #initial file service
    file_service = FileService(account_name=myStorageAccount, account_key=myStorageKey,endpoint_suffix=endpointSuffix)
5. 创建你的文件共享和目录:
#create file share
if(not file_service.exists(mysharename)):
file_service.create_share(mysharename) #create directory under the share
if(not file_service.exists(mysharename,mydirname)):
file_service.create_directory(mysharename, mydirname)
6. 目录创建完成后,就可以上传文件,或者写入文件,上传或写入文件有多种方式,比如create_file_from_bytes or create_file_from_text , create_file_from_path,create_file_from_stream,create_file_from_bytes等等,根据需要使用,本测试中使用create_file_from_path:
    # upload a file to the share at directory you just created
file_service.create_file_from_path(
mysharename,
mydirname,
'hdinsight.publishsettings',
'd:\\hdinsight.publishsettings')
7. 你可以通过Python的API列举出共享下,根目录下所有的文件,或者指定目录,列出指定目录下所有的文件或者目录:
#List files and directories of the share
filediritertors = file_service.list_directories_and_files(mysharename,mydirname)
for file_or_dir in filediritertors:
print(file_or_dir.name)
8. 那么怎么从文件中获取内容或者获取整个文件? Python SDK也提供了多种方式,比如get_file_to_pathget_file_to_streamget_file_to_bytes, or get_file_to_text. 等,本例中奖文件保存到本地:
#Download file to local path

file_service.get_file_to_path(mysharename, mydirname, 'hdinsight.publishsettings', 'D:\\movie\\hdinsight.publishsettings')
9. 最后,看看如何删除文件,删除目录,删除共享,也比较简单:
#delete a file
file_service.delete_file(mysharename, mydirname, 'hdinsight.publishsettings') #delete a directory
file_service.delete_directory(mysharename,mydirname) #delete a share
if(file_service.exists(share_name=mysharename)):
file_service.delete_share(mysharename)
利用Python来访问文件共享还是非常方便的,File share的方式也利用多种不同的平台,不同的语言,提供数据交换和共享,可以在合适的场景中使用。

Azure File文件共享(6):使用Python开发的更多相关文章

  1. Azure File SMB3.0文件共享服务(5)

      使用Java管理Azure文件共享服务   Azure文件共享服务提供了多种方式的访问接口,包括Powershell,.Net, Java, Python等等,本章主要介绍如何使用Java来访问A ...

  2. Azure File SMB3.0文件共享服务(1)

    Azure Storage File是Azure推出的文件共享服务,目前的版本同时支持SMB 2.1和SMB 3.0协议.文件共享服务非常适合那些希望把自己数据中心中使用文件共享的应用程序,在云端需要 ...

  3. Azure File SMB3.0文件共享服务(4)

    在Linux上使用Azure文件共享服务 使用SMB 3.0从用户自己的数据连接到Azure,需要加密连接,但目前的Linux SMB客户端都暂时都不支持,Linux的开源社区正在努力将该功能添加到L ...

  4. Azure File SMB3.0文件共享服务(2)

    使用Powershell创建文件共享 Azure的文件存储结构如下所示,最基本的文件存储包含存储账号,文件共享,在文件共享下面你可以建立文件目录,上传文件: 在开始使用Powershell创建文件共享 ...

  5. Azure File SMB3.0文件共享服务(3)

    在Windows上使用Azure文件共享服务 之前简单介绍过,你可以通过SMB 3.0协议,将Azure文件共享挂载在本地,就如使用一个网络驱动器是一样的,但需要注意不同版本的Windows对于SMB ...

  6. Windows Azure Storage (20) 使用Azure File实现共享文件夹

    <Windows Azure Platform 系列文章目录> Update 2016-4-14.在Azure VM配置FTP和IIS,请参考: http://blogs.iis.net/ ...

  7. Microsoft Azure File 服务简介

    我们非常高兴地宣布在微软Azure中国区推出 Microsoft Azure File 服务预览版.Azure File 服务使用标准 SMB 2.1 协议提供文件共享.Azure 中运行的应用程序现 ...

  8. Azure Storage 系列(七)使用Azure File Storage

    一,引言 今天我们开始介绍 Storage 中的最后一个类型的存储----- File Storage(文件存储),Azure File Storage 在云端提供完全托管的文件共享,这些共享项可通过 ...

  9. python开发环境搭建

    虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上python官网下载python运 ...

随机推荐

  1. [TYVJ] P1031 热浪

    热浪 背景 Background USACO OCT09 9TH   描述 Description 德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很 ...

  2. ftp二进制与ascii传输方式区别

    ASCII 和BINARY模式区别:    用HTML 和文本编写的文件必须用ASCII模式上传,用BINARY模式上传会破坏文件,导致文件执行出错.    BINARY模式用来传送可执行文件,压缩文 ...

  3. bzoj 1912 巡逻(树直径)

    Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Ou ...

  4. Windows 下 pip和easy_install 的安装与使用

    最简单的莫过于用vs 开发python 的程序,集成PTVS插件后,果然简单易用,调试也方便. 手工搭建的话: 官网:https://pypi.python.org/pypi/setuptools 主 ...

  5. Linux上的设备管理器

    一般windows上我们用它自带的“设备管理器”来查看,管理,安装,卸载驱动. 那么问题来了,Linux上用什么命令来看呢? 可以用: lshw   lsusb lspci lsmod 查看特定模块. ...

  6. python 程序穩定性閒談-續集

    前文再續,書接上一回.上次總結python錯誤碼返回與異常機制共用的一些原則,毫無疑問地,仍會出現程序不穩定的現象(好吧,可能是我個人能力問題).在公司做的一個項目中,出現了因爲使用外部模塊造成pyt ...

  7. 线段树(build,insert,dfs操作)

    模板原型: 解决零散数点在已知线段上的出现次数.思想是将线段用长线覆盖,将长线转化成线段树.用权值记录各个数点出现的次数,最后进行查询.代码解释见注释. #include <bits/stdc+ ...

  8. 自定义的string类

    头文件Hi_String.h #include<iostream> #include<string.h> using namespace std; class Hi_Strin ...

  9. flex渐变色制作圆角橙色按钮

    <?xml version="1.0" encoding="utf-8"?> <s:SparkButtonSkin xmlns:fx=&quo ...

  10. shell编程while

    脚本编程:    顺序结构    选择结构        if        case    循环结构        for        while        until        whil ...