Azure File文件共享(6):使用Python开发
2016-05-18 00:15
507 查看
Azure文件共享服务提供了多种方式的访问接口,包括Powershell,.Net, Java, Python等等,本章主要介绍如何使用Python来访问Azure File存储。
关于Python环境的安装,Azure SDK for python的安装配置,Linux和Windows上的模块升级,请参考博客:
http://cloudapps.blog.51cto.com/3136598/1772880
首先,导入Azure storage file所需要的模块:
from azure.storage.file import FileService
from azure.storage.file import ContentSettings
默认的service endpoint是指向global Azure的,所以首先需要设置中国Azure的服务URL,另外也需要设置你的存储账户,存储的key等基本信息
初始化你的文件service:
5. 创建你的文件共享和目录:
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:
关于Python环境的安装,Azure SDK for python的安装配置,Linux和Windows上的模块升级,请参考博客:
http://cloudapps.blog.51cto.com/3136598/1772880
首先,导入Azure storage file所需要的模块:
from azure.storage.file import FileService
from azure.storage.file import ContentSettings
默认的service endpoint是指向global Azure的,所以首先需要设置中国Azure的服务URL,另外也需要设置你的存储账户,存储的key等基本信息
endpointSuffix = "core.chinacloudapi.cn" myStorageAccount = "mystorageacctfile" myStorageKey = "Your account key" mysharename = "mypythonshare" mydirname = "sampledir"
初始化你的文件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_path, get_file_to_stream, get_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的方式也利用多种不同的平台,不同的语言,提供数据交换和共享,可以在合适的场景中使用。
相关文章推荐
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
- python中的id() 函数深度解剖
- Python爬虫学习资料书籍推荐
- Python文件迭代的用法实例教程
- Twitter的分布式自增ID算法Snowflake实现分析及其Java、Php和Python版
- Python实战:爬虫的基础
- Python performance
- PySide——Python图形化界面
- Python 代码性能优化
- python匹配ip正则
- python 基于次世代验证码识别系统的小demo
- 发现使用python+tushare做量化交易 其实不是很难
- kombu------python的消息库
- python 安装 cairo
- 步步学习之用python实战机器学习1-kNN (K-NearestNeighbors)算法(a)
- 简单的python爬取网页字符串内容并保存
- python一些内建函数(map,zip,filter,reduce,yield等)
- Python之常用模块(待更新)
- Tornado X MVC
- [Python笔记]第九篇:re正则表达式