Python Operations Azure Blob Storage

Source: Internet
Author: User
Tags azure sdk


Install the Azure Storage SDK for Python


The simplest way is to execute the following command directly on a machine that has Python and Pip installed:


Pip Install Azure-storage


After the installation is complete, view the installed version via the PIP Freeze command:





Since Azure Storage SDK for Python is an open source project, you can also install it from the source code, please refer to the official documentation.


Create Blob Container


Since any blob must be contained in a BLOB Container, our first task is to create a blob Container.
The SDK provides us with an object named Blockblobservice. With this object we can create and manipulate Blob Container. The following code creates a Container named "Nickcon":





The code itself is simple, where account_name and Account_key are your storage account and its access key. We use the GUI tools Microsoft Azure Storage Explorer to view the results of the code actions:





The Blob Container named Nickcon has been successfully created.


Uploading files


Next we'll upload the local file to the Blob Container we just created. The Azure SDK provides us with the following four methods:

create_blob_from_path #Upload a file with the specified path. create_blob_from_stream #Upload content from a data stream. create_blob_from_bytes #Upload a bype array. create_blob_from_text #Upload a string using a specific encoding format.


Yes, you are not mistaken, all methods have no upload words in their names, but use create. This also means that the nature of the upload file is to create a Blob object in the cloud.




from azure.storage.blob import BlockBlobServicefrom azure.storage.blob import ContentSettings

mystoragename = "xxxx"mystoragekey = "yyyy"blob_service = BlockBlobService(account_name=mystoragename, account_key=mystoragekey)

blob_service.create_blob_from_path(    ‘nickcon‘,    ‘myblobcortana.jpg‘,    ‘cortana-wallpaper.jpg‘,
    content_settings=ContentSettings(content_type=‘image/jpg‘))




This time we introduced the type contentsettings, which is mainly the type of the specified file. Note the second argument of the Create_blob_from_path method, we need to specify a name for the new Blob object. The first parameter is the target Container, and the third parameter is the local file path to be uploaded. Executing the above script will upload a local wallpaper cortana-wallpaper.jpg to Azure Blob Container:





The name of the Blob object created in Container is not already the name of the source file, but the myblobcortana.jpg we specified.


Controlling access rights


Files stored in Blob Container have a corresponding URL, which is the default policy for Azure blob Storage. So that we can access these files from anywhere via URLs. For example, the URL of the myblobcortana.jpg file is:



Https://nickpsdk.blob.core.windows.net/nickcon/myblobcortana.jpg
Paste this address directly into the browser's address bar:





Ah oh, embarrassed, received a ruthless error!



Seriously, it is reasonable to receive such a mistake. Otherwise, anyone can see the contents of my saved files, what is the privacy? Will anyone else pay for Azure Blob Storage? The truth of the matter is that, by default, the Blob Container and BLOB objects we create are private, that is, they must be accessed through an account and access key. If you want content to become a common resource that everyone can access, you can specify it as publicaccess when you create it. You can also modify its properties to publicaccess after the creation is complete. Below we set the Nickcon Container to publicaccess:



from azure.storage.blob import BlockBlobServicefrom azure.storage.blob import PublicAccess

mystoragename = "xxxx"mystoragekey = "yyyy"blob_service = BlockBlobService(account_name=mystoragename, account_key=mystoragekey)

blob_service.set_container_acl(‘nickcon‘, public_access=PublicAccess.Container)




The publicaccess type is import here, and the Set_container_acl method is called to modify the access rights of the container. Try refreshing the page again:





At this point, do not go to your Blob Container privacy photos Oh!


List all files in Blob Container


It is important to check which files are in the Container, but we can do it easily:


Generator = Blob_service.list_blobs (' Nickcon ') for blob in Generator:print (Blob.name)


Use the List_blobs method to get all the Blob objects in the Container. The above code prints the names of all Blob objects.


Download Blob Object


As with creating BLOB objects, there are four ways to download blob objects. For simple periods we only demonstrate the Get_blob_to_path method, other usages are similar:


Blob_service.get_blob_to_path (' Nickcon ', ' myblobcortana.jpg ', ' newimage.png ')


The second parameter is the name of the Blob object in Container, and the third parameter is the path to the local file.


Delete Blob Object


There is a natural deletion of the creation, the code is simple, no longer verbose:


Blob_service.delete_blob (' Nickcon ', ' myblobcortana.jpg ')
Backing up files in Blob Container


Yes, you heard me wrong!
We believe in the security of cloud storage, but it is also necessary to back up important data to other storage. The following code backs up the contents of all Blob Container in an Azure Storage account to a local disk:




from azure.storage.blob import BlockBlobServiceimport os

mystoragename = "xxxx" mystoragekey = "yyyy" blob_service = BlockBlobService (account_name = mystoragename, account_key = mystoragekey) # Download all files in a Blob Containerdef downloadFilesInContainer (blobContainName):
    generator = blob_service.list_blobs (blobContainName) for blob in generator: # get the directory path of the blob file
        blobDirName = os.path.dirname (blob.name) # add the name of the Blob Container as a first-level directory
        newBlobDirName = os.path.join (blobContainName, blobDirName) # check if the file directory exists, create it if it does not exist
        if not os.path.exists (newBlobDirName):
            os.makedirs (newBlobDirName)
        localFileName = os.path.join (blobContainName, blob.name)
        blob_service.get_blob_to_path (blobContainName, blob.name, localFileName) # Get user-owned Blob ContainercontainerGenerator = blob_service.list_containers () for con in containerGenerator:
    downloadFilesInContainer (con.name)




One thing to note here is that blob.name contains a directory of files in container. For example, a file in the Blob Container path is Abc/test.txt, then its blog.name is abc/test.txt. To maintain the name and path of the file in Blob Container, create the corresponding directory structure locally.






Python Operations Azure Blob Storage


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.