Create AzureBlobStorage.py

#13
by Shashika-HF - opened
Files changed (1) hide show
  1. AzureBlobStorage.py +47 -0
AzureBlobStorage.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from azure.storage.blob import BlobServiceClient
2
+
3
+ #parameters for linking azure to the application
4
+ storage_account_key="Q+EneUx5hlODHCjsSo49mm1bGVNdBd2wZ/T0yZMtag1C6FUIwr/yKf+XqDPmVF1PU81eitB2L3tN+AStD/eZ+A=="
5
+ storage_account_name="phonebrrdemonstration2"
6
+ connection_string="DefaultEndpointsProtocol=https;AccountName=phonebrrdemonstration2;AccountKey=Q+EneUx5hlODHCjsSo49mm1bGVNdBd2wZ/T0yZMtag1C6FUIwr/yKf+XqDPmVF1PU81eitB2L3tN+AStD/eZ+A==;EndpointSuffix=core.windows.net"
7
+ container_name="useruploadvideo"
8
+ file_path = r"C:\Users\ASUS\Desktop\UoW\2ND YEAR\SDGP\HuggingFace\Video\production_id_5091624 (1080p).mp4"
9
+ file_name = "uploaded_video.mp4"
10
+
11
+ def uploadUserVideoToBlobStorage(file_path,file_name):
12
+ """Uploads an MP4 video file to the specified Azure Blob Storage container.
13
+
14
+ Args:
15
+ file_path (str): The path to the MP4 video file.
16
+ file_name (str): The desired name of the blob in Azure Blob Storage.
17
+
18
+ Raises:
19
+ FileNotFoundError: If the specified file is not found.
20
+ """
21
+
22
+
23
+ try:
24
+ # Create BlobServiceClient using the connection string
25
+ blob_service_client = BlobServiceClient.from_connection_string(conn_str=connection_string)
26
+
27
+ # Get a reference to the blob container
28
+ container_client = blob_service_client.get_container_client(container_name)
29
+
30
+ # Create the blob client with the specified file name
31
+ blob_client = container_client.get_blob_client(file_name)
32
+
33
+ # Open the video file in binary mode for upload
34
+ with open(file_path, "rb") as data:
35
+ # Upload the video data to the blob
36
+ upload_blob_result = blob_client.upload_blob(data)
37
+
38
+ print(f"Video '{file_name}' uploaded successfully. Response: {upload_blob_result}")
39
+
40
+ except FileNotFoundError as e:
41
+ print(f"Error: File not found at {file_path}.")
42
+ raise # Re-raise the exception for further handling
43
+
44
+ if __name__ == "__main__":
45
+ # Example usage
46
+ uploadUserVideoToBlobStorage(file_path,file_name)
47
+