Spaces:
Runtime error
Runtime error
Create AzureBlobStorageAudio.py (#17)
Browse files- Create AzureBlobStorageAudio.py (011832dbe43d9cc968126a494652e486c325029e)
Co-authored-by: Shashika Sellapperuma <Shashika-HF@users.noreply.huggingface.co>
- AzureBlobStorageAudio.py +56 -0
AzureBlobStorageAudio.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from azure.storage.blob import BlobServiceClient
|
| 2 |
+
|
| 3 |
+
# Replace placeholders with your actual credentials
|
| 4 |
+
storage_account_name = "useruploadhuggingface"
|
| 5 |
+
storage_account_key = "zhrGpPBX6PVD+krncC4nVF4yoweEku/z2ErVxjLiuu/CjAVKqM5O4xlGWEyuWGxptL3mA1pv/6P4+AStjSjLEQ=="
|
| 6 |
+
connection_string = f"DefaultEndpointsProtocol=https;AccountName={storage_account_name};AccountKey={storage_account_key};EndpointSuffix=core.windows.net"
|
| 7 |
+
|
| 8 |
+
container_name = "useruploadhuggingfaceaudio" # Update container name for audio files
|
| 9 |
+
file_path = r"C:\Users\ASUS\Desktop\UoW\2ND YEAR\SDGP\AUDIO\edit\3_second_audio.flac" # Update path to your MP3 file
|
| 10 |
+
file_name = "uploaded_audio.mp3"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def uploadUserAudioToBlobStorage(file_path, file_name):
|
| 14 |
+
"""Uploads an MP3 audio file to the specified Azure Blob Storage container and returns the URL.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
file_path (str): The path to the MP3 audio file.
|
| 18 |
+
file_name (str): The desired name of the blob in Azure Blob Storage.
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
str: The URL for the uploaded audio file in Azure Blob Storage.
|
| 22 |
+
|
| 23 |
+
Raises:
|
| 24 |
+
FileNotFoundError: If the specified file is not found.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
# Create BlobServiceClient using the connection string
|
| 29 |
+
blob_service_client = BlobServiceClient.from_connection_string(conn_str=connection_string)
|
| 30 |
+
|
| 31 |
+
# Get a reference to the blob container
|
| 32 |
+
container_client = blob_service_client.get_container_client(container_name)
|
| 33 |
+
|
| 34 |
+
# Create the blob client with the specified file name
|
| 35 |
+
blob_client = container_client.get_blob_client(file_name)
|
| 36 |
+
|
| 37 |
+
# Open the audio file in binary mode for upload
|
| 38 |
+
with open(file_path, "rb") as data:
|
| 39 |
+
# Upload the audio data to the blob
|
| 40 |
+
upload_blob_result = blob_client.upload_blob(data)
|
| 41 |
+
|
| 42 |
+
# Get the URL for the uploaded blob
|
| 43 |
+
blob_url = blob_client.url
|
| 44 |
+
|
| 45 |
+
print(f"Audio '{file_name}' uploaded successfully. URL: {blob_url}")
|
| 46 |
+
return blob_url
|
| 47 |
+
|
| 48 |
+
except FileNotFoundError as e:
|
| 49 |
+
print(f"Error: File not found at {file_path}.")
|
| 50 |
+
raise # Re-raise the exception for further handling
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
# Example usage
|
| 55 |
+
uploaded_audio_url = uploadUserAudioToBlobStorage(file_path, file_name)
|
| 56 |
+
# You can now use the uploaded_audio_url for further processing or sharing
|