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. Excel——使用VLOOKUP函数关联跨工作薄数据

    实验环境 有两个工作簿,一个是<花名册>,另一个是<入离职表>,<花名册>上有所有员工的详细信息,包括员工的姓名.部门.出生日期等,<入离职表>上有离职 ...

  2. libeXosip2(1) -- Modules

    Modules Here is a list of all modules: [detail level 12] The eXtented eXosip stack LibeXosip2 Versio ...

  3. UESTC_Dividing Numbers CDOJ 1156

    Dividing Numbers Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Other ...

  4. 学习php常用算法

    <?php /*学用php算法*/ /*1.冒泡法 *思路分析:在要排序的一组数中,对当前还未排好的序列, *从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒. *即,每 ...

  5. python之路-模块安装 paramiko

    paramiko介绍(全是洋文,看不懂啊,赶紧有道翻译吧,等有朝一日,我去报个华尔街): "Paramiko" is a combination of the esperanto ...

  6. 多线程下不反复读取SQL Server 表的数据

    在进行一些如发送短信.邮件的业务时,我们常常会使用一个表来存储待发送的数据,由后台多个线程不断的从表中读取待发送的数据进行发送.发送完毕后再将数据转移到历史表中,这样保证待发送表的数据普通情况下不会太 ...

  7. android CMWAP, CMNET有何差别

    什么是CMNET,什么是CMWAP? 答:CMWAP和CMNET仅仅是中国移动为其划分的两个GPRS接入方式.中国移动对CMWAP作了一定的限制,主要表如今CMWAP接入时仅仅能訪问GPRS网络内的I ...

  8. 要理解javascript中间apply和call

    apply和call它是javascript一个非常重要的方法,.虽然与程序平时很少接触,但JS到处都在使用这个框架2方法. 2个方法是在Function.prototype中.也就是说每一个JS函数 ...

  9. Android应用程序键盘(Keyboard)消息处理机制分析

    在Android系统中,键盘按键事件是由WindowManagerService服务来管理的,然后再以消息的形 式来分发给应用程序处理,不过和普通消息不一样,它是由硬件中断触发的:在上一篇文章< ...

  10. iOS经常使用的加密算法

    在iOS开发中,为了数据的安全经常对内容进行加密,在这儿我们对经常使用的加密算法进行了总结: 1.MD5 <span style="font-size:18px;">+ ...