Azure File File Share (6): Developing with Python

Source: Internet
Author: User
Tags hdinsight azure sdk

The Azure File Sharing service provides a variety of ways to access interfaces, including Powershell,.net, Java, Python, and so on, and this chapter focuses on using Python to access Azure file storage.

  1. For the installation of the Python environment, the installation configuration for the Azure SDK for Python, the module upgrade on Linux and Windows, please refer to the blog:

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

  2. First, import the modules required for Azure storage file:

     

    from  azure.storage.file  import  fileservice
    from  azure.storage.file  import  contentsettings

     

  3. The default service endpoint is point to global azure, so you first need to set up the services URL for China Azure, and you need to set up your storage account, stored key and other basic information

     

      endpointsuffix =   "core.chinacloudapi.cn" 
    mystorageaccount = "Mystorageacctfile"
    mystoragekey = "Your account Key"
    mysharename = "Mypythonshare"
    mydirname = "Sampledir"

     

  4. Initialize your file service:

    #initial file service
    file_service = FileService(account_name=myStorageAccount, account_key=myStorageKey,endpoint_suffix=endpointSuffix)

5. Create your file share and directory:

#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. After the catalog is created, you can upload files, write files, upload or write files in several ways, such as Create _file_from_bytes  or  create_file_from_text   ,   Create_file_from_path , create_file_from_stream create_file_from_bytes et cetera, use as needed, This test uses the 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. You can use the Python API to enumerate all the files under the share, root directory, or specify directories to list all the files or directories under the specified directory:

#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. How do I get the content from the file or get the entire file? The Python SDK also offers a variety of ways, such as Get_file_to_path, Get_file_to_stream, Get_file_to_bytes, or Get_file_to_text. And so on, this example the winning file is saved locally:

#Download file to local path

file_service.get_file_to_path(mysharename, mydirname, ‘hdinsight.publishsettings‘, ‘D:\\movie\\hdinsight.publishsettings‘)

9. Finally, see how to delete files, delete directories, delete shares, and more simple:

    #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)

The use of Python to access file shares is also very convenient, file share in a variety of different platforms, different languages, data exchange and sharing, can be used in the appropriate scenario.


Azure File File Share (6): Developing with Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.