File upload to Azure Storage (Python)

Procedure for uploading files to Azure Storage

The method of uploading a file to Azure Storage is described below.

Prerequisites

We are building in the following environment.

OS: Windows10 Language: Python 3.8.6

Have an Azure environment You have created a new account in the "Storage Account" service of Azure

Upload procedure

① Install the Azure Storage package for Python. $ py -m pip install azure-storage-blob == 12.5.0

② In the Azure portal, select Blob service → Container from the "Storage Account" service. Then check the connection string.

③ Execute "sample_upload.py" that describes the following program. In addition, place "Hello World.txt" (contents are free) at the execution position.

$ py sample_upload.py

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__

#Check the connection string on Azure Storage.
connect_str = <Connection string>

# Create a file in local data directory to upload and download
container_name = "<Container name>"

#Strictly speaking, Azure Storage is not a folder.
#For the time being"\"It is possible to realize a pseudo folder structure by separating
upload_path = "<Upload destination folder name>\"

#File name to upload(Describe the relative file path according to the execution position)
local_file_name = "HelloWorld.txt"

#Upload destination
upload_file_path = os.path.join(upload_path, local_file_name)

#Create an instance to connect to your Azure Storage.
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

#Create a client instance of blob (file) that connects to the specified container of Azure Storage.
blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)

print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)

#Upload to Azure Storage
with open(upload_file_path, "rb") as data:
    blob_client.upload_blob(data)

References

Azure Official Document Python Reference BlobServiceClientClass https://docs.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient?view=azure-python

Recommended Posts

File upload to Azure Storage (Python)
Image upload & download to Azure Storage. With Python + requests + REST API
Upload file to GCP's Cloud Storage (GCS) ~ Load with local Python
Output to csv file with Python
How to specify Cache-Control for blob storage in Azure Storage in Python
Use Azure Blob Storage from Python
[For Python] Quickly create an upload file to AWS Lambda Layer
[Python] How to convert db file to csv
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
Convert psd file to png in Python
Upload images to Google Drive with Python
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
Script python file
Updated to Python 2.7.9
Python file processing
"Backport" to python 2
How to upload with Heroku, Flask, Python, Git (4)
Upload text file to rental server by ftp
From file to graph drawing in Python. Elementary elementary
How to create a JSON file in Python
Writing logs to CSV file (Python, C language)
Output python log to both console and file
Upload files to Google Drive with Lambda (Python)
I want to write to a file with Python
[Azure Functions / Python] Chain functions with Queue Storage binding
Sample to put Python Kivy in one file
2017-02-19 Python> Link> Redirect to output file immediately> sys.stdout.flush ()
Parse a JSON string written to a file in Python
[Python] File / directory operations
Template of python script to read the contents of the file
[Note] File reading ~ Python ~
File processing in Python
How to install Python
Upload JPG file using Google Drive API in Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
python> Change file owner> os.chown ("path / to / file", uid, gid)
I tried to touch the CSV file with Python
Rewrite Python2 code to Python3 (2to3)
How to upload with Heroku, Flask, Python, Git (Part 3)
Automatically execute python file
python decorator to retry
How to use Azure Table storage from Django (PTVS)
Introduction to Python language
A memorandum to run a python script in a bat file
How to measure mp3 file playback time with python
Read the xml file by referring to the Python tutorial
I want to randomly sample a file in Python
Introduction to OpenCV (python)-(2)
File operations in Python
[Python] Change standard input from keyboard to text file
How to upload with Heroku, Flask, Python, Git (Part 1)
File upload with django
Note to daemonize python
Introducing Python 2.7 to CentOS 6.6
How to upload with Heroku, Flask, Python, Git (Part 2)
Procedure to exe python file from Ubunts environment construction
[Work efficiency] How to change file names in Python
Connect python to mysql
[Python MinMaxScaler] Normalize to 0 ~ 1