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. android 显示特殊符号

    http://hsx9566.iteye.com/blog/1305052 在android上使用ASCII显示特殊符号 在xml中表示如下: <string name="symbol ...

  2. 关于box-sizing

    http://www.zhangxinxu.com/css3/css3-box-sizing.php box-sizing:border-box; -o-box-sizing:border-box; ...

  3. LeetCode_Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. linux操作系下RAR的使用

    ============zip文件的操作================================== zip -r data.zip data 解释:将data文件夹压缩成了data.zip格 ...

  5. 【转】使用vnc连接linux服务器方便hadoop开发调试

    VNC(Virtual Network Computing)它能将完整的窗口界面通过网络,传输到另一台计算机的屏幕上. 类似的软件在Windows服务器中包含的"Terminal Serve ...

  6. POJ 1300 欧拉通路&欧拉回路

    系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...

  7. Android应用程序的Activity启动过程简要介绍和学习计划

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6685853 在Android系统中,Activ ...

  8. Java线程状态:BLOCKED与WAITING的区别

    Doc说明: /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked ...

  9. Kernel Regression from Nando's Deep Learning lecture 5

    require 'torch' require 'gnuplot' , , nData) ) print(xTrain) print(yTrain) local yTrain = yTrain + t ...

  10. shell中的退出状态码

    shell中的退出状态码最大只有255,如果超过这个值,就会进行取余运算,即如果执行如下命令: exit exitCode 如果exitCode大于255,那么实际的状态码为exitCode % 25 ...