Friday, July 5, 2019

Managing Azure blob storage

Azure blob storage is the object storage solution from microsoft in azure cloud. Blob storage is designed to contain large amount of unstructured data which includes text and binary data.

Azure blob storage has following particularities
  • Serving images or documents directly to a browser.
  • Storing files for distributed access.
  • Streaming video and audio.
  • Writing to log files.
  • Storing data for backup and restore, disaster recovery, and archiving.
  • Storing data for analysis by an on-premises or Azure-hosted service.












Blob storage offers three types of resources 

storage account
A storage account provides a unique namespace in Azure for your data. Every object that you store in Azure Storage has an address that includes your unique account name. The combination of the account name and the Azure Storage blob endpoint forms the base address for the objects in your storage account.

Containers 

A container organizes a set of blobs, similar to a directory in a file system. A storage account can include an unlimited number of containers, and a container can store an unlimited number of blobs.

Blobs

Blobs are 3 types
  • Block blobs store text and binary data, up to about 4.7 TB. Block blobs are made up of blocks of data that can be managed individually.
  • Append blobs are made up of blocks like block blobs, but are optimized for append operations. Append blobs are ideal for scenarios such as logging data from virtual machines.
  • Page blobs store random access files up to 8 TB in size. Page blobs store virtual hard drive (VHD) files and serve as disks for Azure virtual machines. For more information about page blobs, see Overview of Azure page blobs

Creating a blob storage 

1. First login to the azure portal and create a storage account 

(All services --- storage account--- select the subscription and resource group) I have created "unixchipsstorage2" under unixchips resource group 














2. Once you create the storage account select the blobs from the underlined services














3. Click on blob and select container and create the blob container . Here i am giving name as "unixchipsblob" and given the access as private














4. Once you created the blob go to upload and upload the file to the container as below , we can set the permission as private,anonymous to blobs (public ) and anonymous to containers and blobs . Also we can click on download and save the blobs locally



























5. If we change the access level of the blob to anonymous we can copy paste the url in the browser to read the content of the file










Now let's create a container and upload and download the blobs using pyhon SDK


1. First we need to download the sample application from GIT





2. once it is downloaded copy the storage account name ( here unixchipsstorage2 ) and its key in the example.py as below











3. Now install the python module "azure-storage-blob"

unixchips@unixchips:~/storage-blobs-python-quickstart$ pip install azure-storage-blob
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Requirement already satisfied: azure-storage-blob in /home/unixchips/.local/lib/python2.7/site-packages (1.4.0)
Requirement already satisfied: futures; python_version < "3.0" in /home/unixchips/.local/lib/python2.7/site-packages (from azure-storage-blob) (3.2.0)
Requirement already satisfied: azure-storage-common~=1.4 in /home/unixchips/.local/lib/python2.7/site-packages (from azure-storage-blob) (1.4.2)
Requirement already satisfied: azure-common>=1.1.5 in /home/unixchips/.local/lib/python2.7/site-packages (from azure-storage-blob) (1.1.23)
Requirement already satisfied: python-dateutil in /home/unixchips/.local/lib/python2.7/site-packages (from azure-storage-common~=1.4->azure-storage-blob) (2.8.0)
Requirement already satisfied: cryptography in /home/unixchips/.local/lib/python2.7/site-packages (from azure-storage-common~=1.4->azure-storage-blob) (2.7)
Requirement already satisfied: azure-storage-nspkg; python_version < "3.0" in /home/unixchips/.local/lib/python2.7/site-packages (from azure-storage-common~=1.4->azure-storage-blob) (3.1.0)
Requirement already satisfied: requests in /home/unixchips/.local/lib/python2.7/site-packages (from azure-storage-common~=1.4->azure-storage-blob) (2.22.0)
Requirement already satisfied: azure-nspkg; python_version < "3.0" in /home/unixchips/.local/lib/python2.7/site-packages (from azure-common>=1.1.5->azure-storage-blob) (3.0.2)
Requirement already satisfied: six>=1.5 in /home/unixchips/.local/lib/python2.7/site-packages (from python-dateutil->azure-storage-common~=1.4->azure-storage-blob) (1.12.0)
Requirement already satisfied: enum34; python_version < "3" in /home/unixchips/.local/lib/python2.7/site-packages (from cryptography->azure-storage-common~=1.4->azure-storage-blob) (1.1.6)
Requirement already satisfied: cffi!=1.11.3,>=1.8 in /home/unixchips/.local/lib/python2.7/site-packages (from cryptography->azure-storage-common~=1.4->azure-storage-blob) (1.12.3)
Requirement already satisfied: asn1crypto>=0.21.0 in /home/unixchips/.local/lib/python2.7/site-packages (from cryptography->azure-storage-common~=1.4->azure-storage-blob) (0.24.0)
Requirement already satisfied: ipaddress; python_version < "3" in /home/unixchips/.local/lib/python2.7/site-packages (from cryptography->azure-storage-common~=1.4->azure-storage-blob) (1.0.22)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/unixchips/.local/lib/python2.7/site-packages (from requests->azure-storage-common~=1.4->azure-storage-blob) (1.25.3)
Requirement already satisfied: certifi>=2017.4.17 in /home/unixchips/.local/lib/python2.7/site-packages (from requests->azure-storage-common~=1.4->azure-storage-blob) (2019.6.16)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/unixchips/.local/lib/python2.7/site-packages (from requests->azure-storage-common~=1.4->azure-storage-blob) (3.0.4)
Requirement already satisfied: idna<2.9,>=2.5 in /home/unixchips/.local/lib/python2.7/site-packages (from requests->azure-storage-common~=1.4->azure-storage-blob) (2.8)
Requirement already satisfied: pycparser in /home/unixchips/.local/lib/python2.7/site-packages (from cffi!=1.11.3,>=1.8->cryptography->azure-storage-common~=1.4->azure-storage-blob) (2.19)

..................................<output is omitted>..................................................................


4. Run the python program as below 

unixchips@unixchips:~/storage-blobs-python-quickstart$ python example.py 
Temp file = /home/unixchips/Documents/QuickStart_d2eb859f-b954-4007-bc60-d0efd3cc35ed.txt

Uploading to Blob storage as blobQuickStart_d2eb859f-b954-4007-bc60-d0efd3cc35ed.txt

List blobs in the container
Blob name: QuickStart_d2eb859f-b954-4007-bc60-d0efd3cc35ed.txt

Downloading blob to /home/unixchips/Documents/QuickStart_d2eb859f-b954-4007-bc60-d0efd3cc35ed_DOWNLOADED.txt
Sample finished running. When you hit <any key>, the sample will be deleted and the sample application will exit

5. If we login to the azure console we can see new container called "quickstartblobs" is created and file is uploaded in to that 










6. Same file is downloaded to the /home/unixchips/Documents directory also 

unixchips@unixchips:~/storage-blobs-python-quickstart$ cd /home/unixchips/Documents/
unixchips@unixchips:~/Documents$ ls -lrt
total 8
-rw-rw-r-- 1 unixchips unixchips 13 Jul  5 21:34 QuickStart_d2eb859f-b954-4007-bc60-d0efd3cc35ed.txt
-rw-rw-r-- 1 unixchips unixchips 13 Jul  5 21:34 QuickStart_d2eb859f-b954-4007-bc60-d0efd3cc35ed_DOWNLOADED.txt

Creating and managing azure blob is explained step by step above ..

Thank you for reading 

No comments:

Post a Comment