blob-upload.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # ----------------------------------------------------------------------------------
  2. # MIT License
  3. #
  4. # Copyright(c) Microsoft Corporation. All rights reserved.
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. # ----------------------------------------------------------------------------------
  13. # The above copyright notice and this permission notice shall be included in all
  14. # copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. import os
  24. import uuid
  25. import sys
  26. from azure.storage.blob import BlockBlobService, PublicAccess
  27. # ---------------------------------------------------------------------------------------------------------
  28. # Method that creates a test file in the 'Sample' folder.
  29. # This sample application creates a test file, uploads the test file to the Blob storage,
  30. # lists the blobs in the container, and downloads the file with a new name.
  31. # ---------------------------------------------------------------------------------------------------------
  32. # Documentation References:
  33. # Associated Article - https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python
  34. # What is a Storage Account - http://azure.microsoft.com/en-us/documentation/articles/storage-whatis-account/
  35. # Getting Started with Blobs-https://docs.microsoft.com/en-us/azure/storage/blobs/storage-python-how-to-use-blob-storage
  36. # Blob Service Concepts - http://msdn.microsoft.com/en-us/library/dd179376.aspx
  37. # Blob Service REST API - http://msdn.microsoft.com/en-us/library/dd135733.aspx
  38. # ----------------------------------------------------------------------------------------------------------
  39. def run_sample():
  40. try:
  41. # Create the BlockBlobService that is used to call the Blob service for the storage account
  42. blob_service_client = BlockBlobService(
  43. account_name='accountname', account_key='accountkey')
  44. # Create a container called 'quickstartblobs'.
  45. container_name = 'quickstartblobs'
  46. blob_service_client.create_container(container_name)
  47. # Set the permission so the blobs are public.
  48. blob_service_client.set_container_acl(
  49. container_name, public_access=PublicAccess.Container)
  50. # Create Sample folder if it not exists, and create a file in folder Sample to test the upload and download.
  51. local_path = os.path.expanduser("~/Sample")
  52. if not os.path.exists(local_path):
  53. os.makedirs(os.path.expanduser("~/Sample"))
  54. local_file_name = "QuickStart_" + str(uuid.uuid4()) + ".txt"
  55. full_path_to_file = os.path.join(local_path, local_file_name)
  56. # Write text to the file.
  57. file = open(full_path_to_file, 'w')
  58. file.write("Hello, World!")
  59. file.close()
  60. print("Temp file = " + full_path_to_file)
  61. print("\nUploading to Blob storage as blob" + local_file_name)
  62. # Upload the created file, use local_file_name for the blob name
  63. blob_service_client.create_blob_from_path(
  64. container_name, local_file_name, full_path_to_file)
  65. # List the blobs in the container
  66. print("\nList blobs in the container")
  67. generator = blob_service_client.list_blobs(container_name)
  68. for blob in generator:
  69. print("\t Blob name: " + blob.name)
  70. # Download the blob(s).
  71. # Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.
  72. full_path_to_file2 = os.path.join(local_path, str.replace(
  73. local_file_name ,'.txt', '_DOWNLOADED.txt'))
  74. print("\nDownloading blob to " + full_path_to_file2)
  75. blob_service_client.get_blob_to_path(
  76. container_name, local_file_name, full_path_to_file2)
  77. sys.stdout.write("Sample finished running. When you hit <any key>, the sample will be deleted and the sample "
  78. "application will exit.")
  79. sys.stdout.flush()
  80. input()
  81. # Clean up resources. This includes the container and the temp files
  82. blob_service_client.delete_container(container_name)
  83. os.remove(full_path_to_file)
  84. os.remove(full_path_to_file2)
  85. except Exception as e:
  86. print(e)
  87. # Main method.
  88. if __name__ == '__main__':
  89. run_sample()