-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient | ||
|
||
def upload_image_to_azure(local_image_path, container_name, blob_name, connection_string): | ||
try: | ||
# Create a BlobServiceClient | ||
blob_service_client = BlobServiceClient.from_connection_string(connection_string) | ||
|
||
# Create a ContainerClient | ||
container_client = blob_service_client.get_container_client(container_name) | ||
|
||
# Upload the image to Azure Blob Storage | ||
with open(local_image_path, "rb") as data: | ||
container_client.upload_blob(name=blob_name, data=data) | ||
|
||
# Generate a URL for the uploaded image | ||
blob_url = f"https://{blob_service_client.account_name}.blob.core.windows.net/{container_name}/{blob_name}" | ||
|
||
return blob_url | ||
|
||
except Exception as e: | ||
return {"Message": str(e)} | ||
|
||
# # Example usage: | ||
# if __name__ == "__main__": | ||
# local_image_path = "path_to_local_image.jpg" # Replace with the path to your local image | ||
# container_name = "your_container_name" # Replace with your Azure Blob Storage container name | ||
# blob_name = "image.jpg" # Replace with the name you want for the image in Azure | ||
# connection_string = "your_connection_string" # Replace with your Azure Storage Account connection string | ||
|
||
# image_url = upload_image_to_azure(local_image_path, container_name, blob_name, connection_string) | ||
|
||
# if not image_url.startswith("Error"): | ||
# print(f"Image uploaded successfully. URL: {image_url}") | ||
# else: | ||
# print(f"Error uploading |