diff --git a/README.md b/README.md index f0fba03..5e0e341 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,14 @@ urlFragment: upload-download-blobs-python # How to upload and download blobs from Azure Blob Storage with Python -## SDK Versions -In this sample, you will find the following folders: -* **v11** - references Storage Blobs SDK v11 -* **v12** - references Storage Blobs SDK v12 +## This sample shows how to do the following operations of Storage Blobs with Storage SDK +- Create a Storage Account using the Azure Portal. +- Create a container. +- Upload a file to block blob. +- List blobs. +- Download a blob to file. +- Delete a blob. +- Delete the container. ## Prerequisites @@ -58,35 +62,37 @@ setx AZURE_STORAGE_CONNECTIONSTRING "" ``` ### Set up + First, clone the repository on your machine: ```bash git clone https://github.com/Azure-Samples/azure-sdk-for-python-storage-blob-upload-download.git ``` -Then, switch to the appropriate folder: +Then, install the dependencies: + ```bash -cd v11 +pip install -r requirements.txt ``` -or + +Finally, execute the following command to run this sample: ```bash -cd v12 +python example.py ``` -Finally, install the dependencies: +### Use latest Storage SDK -```bash -pip install -``` +The storage SDK package version here is **2.x.x**, if you are using the [latest](https://pypi.org/project/azure-storage-blob/) version of the storage SDK package, please reference to the following examples: -## This sample shows how to do the following operations of Storage Blobs -- Create a Storage Account using the Azure Portal. -- Create a container. -- Upload a file to block blob. -- List blobs. -- Download a blob to file. -- Delete a blob. -- Delete the container. +* [blob_samples_hello_world.py](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/samples/blob_samples_hello_world.py) - Examples for common Storage Blob tasks: + * Create a container + * Create a block, page, or append blob + * Upload a file to blob + * Download a blob + * Delete a blob + * Delete the container +* [blob_samples_enumerate_blobs.py](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/samples/blob_samples_enumerate_blobs.py)- Examples to enumerate blobs + * List blobs. ## Contributing diff --git a/v11/example.py b/example.py similarity index 100% rename from v11/example.py rename to example.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4249ef6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +azure-storage-blob==2.1.0 \ No newline at end of file diff --git a/v12/example.py b/v12/example.py deleted file mode 100644 index 7a60596..0000000 --- a/v12/example.py +++ /dev/null @@ -1,107 +0,0 @@ -# ---------------------------------------------------------------------------------- -# MIT License -# -# Copyright(c) Microsoft Corporation. All rights reserved. -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# ---------------------------------------------------------------------------------- -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - - - -import os -import uuid -import sys -from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, PublicAccess - -# --------------------------------------------------------------------------------------------------------- -# Method that creates a test file in the 'Sample' folder. -# This sample application creates a test file, uploads the test file to the Blob storage, -# lists the blobs in the container, and downloads the file with a new name. -# --------------------------------------------------------------------------------------------------------- -# Documentation References: -# Associated Article - https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python -# What is a Storage Account - http://azure.microsoft.com/en-us/documentation/articles/storage-whatis-account/ -# Getting Started with Blobs - https://docs.microsoft.com/en-us/azure/storage/blobs/storage-python-how-to-use-blob-storage -# Blob Service Concepts - http://msdn.microsoft.com/en-us/library/dd179376.aspx -# Blob Service REST API - http://msdn.microsoft.com/en-us/library/dd135733.aspx -# ---------------------------------------------------------------------------------------------------------- - - -def run_sample(): - try: - # Create the BlobServiceClient that is used to call the Blob service for the storage account - conn_str = os.environ['AZURE_STORAGE_CONNECTIONSTRING'] - blob_service_client = BlobServiceClient.from_connection_string(conn_str=conn_str) - - # Create a container called 'quickstartblobs' and Set the permission so the blobs are public. - container_name = 'quickstartblobs' - blob_service_client.create_container( - container_name, public_access=PublicAccess.Container) - - # Create Sample folder if it not exists, and create a file in folder Sample to test the upload and download. - local_path = os.path.expanduser("~/Sample") - if not os.path.exists(local_path): - os.makedirs(os.path.expanduser("~/Sample")) - local_file_name = "QuickStart_" + str(uuid.uuid4()) + ".txt" - full_path_to_file = os.path.join(local_path, local_file_name) - - # Write text to the file. - file = open(full_path_to_file, 'w') - file.write("Hello, World!") - file.close() - - print("Temp file = " + full_path_to_file) - print("\nUploading to Blob storage as blob" + local_file_name) - - # Upload the created file, use local_file_name for the blob name - blob_client = blob_service_client.get_blob_client( - container=container_name, blob=local_file_name) - with open(full_path_to_file, "rb") as data: - blob_client.upload_blob(data) - - # List the blobs in the container - print("\nList blobs in the container") - container = blob_service_client.get_container_client(container=container_name) - generator = container.list_blobs() - for blob in generator: - print("\t Blob name: " + blob.name) - - # Download the blob(s). - # Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents. - full_path_to_file2 = os.path.join(local_path, str.replace( - local_file_name ,'.txt', '_DOWNLOADED.txt')) - print("\nDownloading blob to " + full_path_to_file2) - with open(full_path_to_file2, "wb") as my_blob: - my_blob.writelines([blob_client.download_blob().readall()]) - - sys.stdout.write("Sample finished running. When you hit , the sample will be deleted and the sample " - "application will exit.") - sys.stdout.flush() - input() - - # Clean up resources. This includes the container and the temp files - blob_service_client.delete_container(container_name) - os.remove(full_path_to_file) - os.remove(full_path_to_file2) - except Exception as e: - print(e) - - -# Main method. -if __name__ == '__main__': - run_sample() \ No newline at end of file