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等基本信息
    1. endpointSuffix = "core.chinacloudapi.cn"
      myStorageAccount = "mystorageacctfile"
      myStorageKey = "Your account key"
      mysharename = "mypythonshare"
      mydirname = "sampledir"
  4. 初始化你的文件service:
    1. #initial file service
      file_service = FileService(account_name=myStorageAccount, account_key=myStorageKey,endpoint_suffix=endpointSuffix)
5. 创建你的文件共享和目录:
  1. #create file share
    if(not file_service.exists(mysharename)):
    file_service.create_share(mysharename)
  2. #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:
  1.     # upload a file to the share at directory you just created
    file_service.create_file_from_path(
    mysharename,
    mydirname,
  2. 'hdinsight.publishsettings',
  3. 'd:\\hdinsight.publishsettings')
  1. 7. 你可以通过PythonAPI列举出共享下,根目录下所有的文件,或者指定目录,列出指定目录下所有的文件或者目录:
  1. #List files and directories of the share
    filediritertors = file_service.list_directories_and_files(mysharename,mydirname)
    for file_or_dir in filediritertors:
  2. print(file_or_dir.name)
  1. 8. 那么怎么从文件中获取内容或者获取整个文件? Python SDK也提供了多种方式,比如get_file_to_path, get_file_to_stream, get_file_to_bytes, or get_file_to_text. 等,本例中奖文件保存到本地:
  1. #Download file to local path
  2. file_service.get_file_to_path(mysharename, mydirname, 'hdinsight.publishsettings', 'D:\\movie\\hdinsight.publishsettings')
  1. 9. 最后,看看如何删除文件,删除目录,删除共享,也比较简单:
  1. #delete a file
    file_service.delete_file(mysharename, mydirname, 'hdinsight.publishsettings')
  2. #delete a directory
    file_service.delete_directory(mysharename,mydirname)
  3. #delete a share
    if(file_service.exists(share_name=mysharename)):
    file_service.delete_share(mysharename)
  1. 利用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] P1023 奶牛的锻炼

    奶牛的锻炼 背景 Background USACO   描述 Description 奶牛Bessie有N分钟时间跑步,每分钟她可以跑步或者休息.若她在第i分钟跑步,可以跑出D_i米,同时疲倦程度增加 ...

  2. Google Chrome源码剖析【序】

    [序(本人什么都没做,完全转载)] 开源是口好东西,它让这个充斥着大量工业垃圾代码和教材玩具代码的行业,多了一些艺术气息和美的潜质.它使得每个人,无论你来自米国纽约还是中国铁岭,都有机会站在巨人的肩膀 ...

  3. #define XBYTE ((unsigned char volatile xdata *) 0)

    今天在看别人的CAN总线程序的时候,突然发现了这么一句宏定义:#define XBYTE ((unsigned char volatile xdata *) 0),以前都没注意到过.后来查了一下,发现 ...

  4. ORA-19573: cannot obtain exclusive enqueue for datafile 1

    还原Oracle数据库时出现ORA-19870和ORA-19573错误,如: RMAN> restore database; Starting restore at 11-DEC-12 usin ...

  5. 分析统计<第三篇>

    统计是一组存储为柱状图的信息.柱状图是显示数据落入不通分类中的频率的一种统计结构.SQL Server存储的柱状图包括多大200行的列和索引键(或多列索引键的第一列)的数据分布采样.在两个连续采样值之 ...

  6. java.lang.OutOfMemoryError: unable to create new native thread(转)

    解决 - java.lang.OutOfMemoryError: unable to create new native thread 工作中碰到过这个问题好几次了,觉得有必要总结一下,所以有了这篇文 ...

  7. Java面试题之十

    五十六.java中有几种类型的流?JDK 为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类? 主要有字节流,字符流.字节流继承于InputStream .OutputStream,字符流 ...

  8. ucfirst() strtoupper() strtolower()

    ucfirst 将字符串第一个字符改大写. 语法: string ucfirst(string str); 返回值: 字符串 函数种类: 资料处理   内容说明 本函数返回字符串 str 第一个字的字 ...

  9. hdu 5611 Baby Ming and phone number(模拟)

    Problem Description Baby Ming collected lots of cell phone numbers, and he wants to sell them for mo ...

  10. IOS Main函数

    int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSS ...