PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/articles/storage-python-how-to-use-blob-storage.md

https://github.com/WalterHub/azure-content
Markdown | 166 lines | 119 code | 47 blank | 0 comment | 0 complexity | cabb16a194cce23a05cfc41ed27e492b MD5 | raw file
Possible License(s): CC-BY-3.0
  1. <properties linkid="develop-python-blob-service" urlDisplayName="Blob Service" pageTitle="How to use blob storage (Python) | Microsoft Azure" metaKeywords="Azure blob service Python, Azure blobs Python" description="Learn how to use the Azure Blob service to upload, list, download, and delete blobs." metaCanonical="" disqusComments="1" umbracoNaviHide="0" services="storage" documentationCenter="Python" title="How to use the Blob service from Python" authors="" videoId="" scriptId="" />
  2. # How to Use the Blob Storage Service from Python
  3. This guide will show you how to perform common scenarios using the
  4. Azure Blob storage service. The samples are written using the
  5. Python API. The scenarios covered include **uploading**, **listing**,
  6. **downloading**, and **deleting** blobs. For more information on blobs,
  7. see the [Next Steps][] section.
  8. ## Table of Contents
  9. [What is Blob Storage?][]
  10. [Concepts][]
  11. [Create an Azure Storage Account][]
  12. [How To: Create a Container][]
  13. [How To: Upload a Blob into a Container][]
  14. [How To: List the Blobs in a Container][]
  15. [How To: Download Blobs][]
  16. [How To: Delete a Blob][]
  17. [How To: Upload and Download Large Blobs][]
  18. [Next Steps][]
  19. [WACOM.INCLUDE [howto-blob-storage](../includes/howto-blob-storage.md)]
  20. ## <a name="create-account"> </a>Create an Azure Storage Account
  21. [WACOM.INCLUDE [create-storage-account](../includes/create-storage-account.md)]
  22. ## <a name="create-container"> </a>How to: Create a Container
  23. **Note:** If you need to install Python or the Client Libraries, please see the [Python Installation Guide](../python-how-to-install/).
  24. The **BlobService** object lets you work with containers and blobs. The
  25. following code creates a **BlobService** object. Add the following near
  26. the top of any Python file in which you wish to programmatically access Azure Storage:
  27. from azure.storage import *
  28. The following code creates a **BlobService** object using the storage account name and account key. Replace 'myaccount' and 'mykey' with the real account and key.
  29. blob_service = BlobService(account_name='myaccount', account_key='mykey')
  30. All storage blobs reside in a container. You can use a **BlobService** object to create the container if it doesn't exist:
  31. blob_service.create_container('mycontainer')
  32. By default, the new container is private, so you must specify your storage access key (as you did above) to download blobs from this container. If you want to make the files within the container available to everyone, you can create the container and pass the public access level using the following code:
  33. blob_service.create_container('mycontainer', x_ms_blob_public_access='container')
  34. Alternatively, you can modify a container after you have created it using the following code:
  35. blob_service.set_container_acl('mycontainer', x_ms_blob_public_access='container')
  36. After this change, anyone on the Internet can see blobs in a public
  37. container, but only you can modify or delete them.
  38. ## <a name="upload-blob"> </a>How to: Upload a Blob into a Container
  39. To upload a file to a blob, use the **put_blob** method
  40. to create the blob, using a file stream as the contents of the blob.
  41. First, create a file called **task1.txt** (arbitrary content is fine)
  42. and store it in the same directory as your Python file.
  43. myblob = open(r'task1.txt', 'r').read()
  44. blob_service.put_blob('mycontainer', 'myblob', myblob, x_ms_blob_type='BlockBlob')
  45. ## <a name="list-blob"> </a>How to: List the Blobs in a Container
  46. To list the blobs in a container, use the **list_blobs** method with a
  47. **for** loop to display the name of each blob in the container. The
  48. following code outputs the **name** and **url** of each blob in a container to the
  49. console.
  50. blobs = blob_service.list_blobs('mycontainer')
  51. for blob in blobs:
  52. print(blob.name)
  53. print(blob.url)
  54. ## <a name="download-blobs"> </a>How to: Download Blobs
  55. To download blobs, use the **get_blob** method to transfer the
  56. blob contents to a stream object that you can then persist to a local
  57. file.
  58. blob = blob_service.get_blob('mycontainer', 'myblob')
  59. with open(r'out-task1.txt', 'w') as f:
  60. f.write(blob)
  61. ## <a name="delete-blobs"> </a>How to: Delete a Blob
  62. Finally, to delete a blob, call **delete_blob**.
  63. blob_service.delete_blob('mycontainer', 'myblob')
  64. ## <a name="large-blobs"> </a>How to: Upload and Download Large Blobs
  65. The maximum size for a block blob is 200 GB. For blobs smaller than 64 MB, the blob can be uploaded or downloaded using a single call to **put\_blob** or **get\_blob**, as shown previously. For blobs larger than 64 MB, the blob needs to be uploaded or downloaded in blocks of 4 MB or smaller.
  66. The following code shows examples of functions to upload or download block blobs of any size.
  67. import base64
  68. chunk_size = 4 * 1024 * 1024
  69. def upload(blob_service, container_name, blob_name, file_path):
  70. blob_service.create_container(container_name, None, None, False)
  71. blob_service.put_blob(container_name, blob_name, '', 'BlockBlob')
  72. block_ids = []
  73. index = 0
  74. with open(file_path, 'rb') as f:
  75. while True:
  76. data = f.read(chunk_size)
  77. if data:
  78. length = len(data)
  79. block_id = base64.b64encode(str(index))
  80. blob_service.put_block(container_name, blob_name, data, block_id)
  81. block_ids.append(block_id)
  82. index += 1
  83. else:
  84. break
  85. blob_service.put_block_list(container_name, blob_name, block_ids)
  86. def download(blob_service, container_name, blob_name, file_path):
  87. props = blob_service.get_blob_properties(container_name, blob_name)
  88. blob_size = int(props['content-length'])
  89. index = 0
  90. with open(file_path, 'wb') as f:
  91. while index < blob_size:
  92. chunk_range = 'bytes={}-{}'.format(index, index + chunk_size - 1)
  93. data = blob_service.get_blob(container_name, blob_name, x_ms_range=chunk_range)
  94. length = len(data)
  95. index += length
  96. if length > 0:
  97. f.write(data)
  98. if length < chunk_size:
  99. break
  100. else:
  101. break
  102. If you need blobs larger than 200 GB, you can use a page blob instead of a block blob. The maximum size of a page blob is 1 TB, with pages that align to 512-byte page boundaries. Use **put\_blob** to create a page blob, **put\_page** to write to it, and **get\_blob** to read from it.
  103. ## <a name="next-steps"> </a>Next Steps
  104. Now that you’ve learned the basics of blob storage, follow these links
  105. to learn how to do more complex storage tasks.
  106. - See the MSDN Reference: [Storing and Accessing Data in Azure][]
  107. - Visit the [Azure Storage Team Blog][]
  108. [Next Steps]: #next-steps
  109. [What is Blob Storage?]: #what-is
  110. [Concepts]: #concepts
  111. [Create an Azure Storage Account]: #create-account
  112. [How To: Create a Container]: #create-container
  113. [How To: Upload a Blob into a Container]: #upload-blob
  114. [How To: List the Blobs in a Container]: #list-blob
  115. [How To: Download Blobs]: #download-blobs
  116. [How To: Delete a Blob]: #delete-blobs
  117. [How To: Upload and Download Large Blobs]: #large-blobs
  118. [Storing and Accessing Data in Azure]: http://msdn.microsoft.com/en-us/library/windowsazure/gg433040.aspx
  119. [Azure Storage Team Blog]: http://blogs.msdn.com/b/windowsazurestorage/