Spaces:
Runtime error
Runtime error
Update AzureBlobStorageAudio.py
Browse files- AzureBlobStorageAudio.py +26 -15
AzureBlobStorageAudio.py
CHANGED
|
@@ -7,23 +7,34 @@ storage_account_key = "zhrGpPBX6PVD+krncC4nVF4yoweEku/z2ErVxjLiuu/CjAVKqM5O4xlGW
|
|
| 7 |
connection_string = f"DefaultEndpointsProtocol=https;AccountName={storage_account_name};AccountKey={storage_account_key};EndpointSuffix=core.windows.net"
|
| 8 |
|
| 9 |
container_name = "useruploadhuggingfaceaudio" # Update container name for audio files
|
| 10 |
-
file_path = r"C:\Users\ASUS\Desktop\UoW\2ND YEAR\SDGP\AUDIO\edit\3_second_audio.flac" # Update path to your MP3 file
|
| 11 |
-
file_name = "uploaded_audio.mp3"
|
| 12 |
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
Args:
|
| 18 |
-
blob_client (BlobClient): The BlobClient object for the blob to delete.
|
| 19 |
-
"""
|
| 20 |
-
try:
|
| 21 |
-
# Get the blob client within the function for deletion
|
| 22 |
-
blob_client = container_client.get_blob_client(blob_name)
|
| 23 |
-
blob_client.delete_blob()
|
| 24 |
-
print(f"Audio deleted successfully from Azure Blob Storage.")
|
| 25 |
-
except Exception as e:
|
| 26 |
-
print(f"Error deleting audio: {e}")
|
| 27 |
|
| 28 |
def uploadUserAudioToBlobStorage(file_path, file_name):
|
| 29 |
"""Uploads an MP3 audio file to the specified Azure Blob Storage container and returns the URL.
|
|
@@ -77,4 +88,4 @@ if __name__ == "__main__":
|
|
| 77 |
container_client = blob_service_client.get_container_client(container_name)
|
| 78 |
|
| 79 |
# Pass container_client and file_name to the deletion function
|
| 80 |
-
|
|
|
|
| 7 |
connection_string = f"DefaultEndpointsProtocol=https;AccountName={storage_account_name};AccountKey={storage_account_key};EndpointSuffix=core.windows.net"
|
| 8 |
|
| 9 |
container_name = "useruploadhuggingfaceaudio" # Update container name for audio files
|
| 10 |
+
#file_path = r"C:\Users\ASUS\Desktop\UoW\2ND YEAR\SDGP\AUDIO\edit\3_second_audio.flac" # Update path to your MP3 file
|
| 11 |
+
#file_name = "uploaded_audio.mp3"
|
| 12 |
|
| 13 |
|
| 14 |
+
def delete_container(container_id: str) -> None:
|
| 15 |
+
"""
|
| 16 |
+
Deletes all blobs within a specified Azure Blob Storage container.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
container_id (str): The ID of the container to delete.
|
| 20 |
+
"""
|
| 21 |
+
try:
|
| 22 |
+
# Establish connection using your storage connection string (replace with yours)
|
| 23 |
+
storage_connection_string = 'DefaultEndpointsProtocol=https;AccountName=useruploadhuggingface;AccountKey=zhrGpPBX6PVD+krncC4nVF4yoweEku/z2ErVxjLiuu/CjAVKqM5O4xlGWEyuWGxptL3mA1pv/6P4+AStjSjLEQ==;EndpointSuffix=core.windows.net'
|
| 24 |
+
blob_service_client = azure.storage.blob.BlobServiceClient.from_connection_string(storage_connection_string)
|
| 25 |
+
|
| 26 |
+
# Get container client
|
| 27 |
+
container_client = blob_service_client.get_container_client(container_id)
|
| 28 |
+
|
| 29 |
+
# Delete all blobs in the container (iterator for large datasets)
|
| 30 |
+
blobs = container_client.list_blobs()
|
| 31 |
+
for blob in blobs:
|
| 32 |
+
container_client.delete_blob(blob.name)
|
| 33 |
+
print(f'Container "{container_id}" emptied successfully.')
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f'Error deleting blobs: {e}')
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def uploadUserAudioToBlobStorage(file_path, file_name):
|
| 40 |
"""Uploads an MP3 audio file to the specified Azure Blob Storage container and returns the URL.
|
|
|
|
| 88 |
container_client = blob_service_client.get_container_client(container_name)
|
| 89 |
|
| 90 |
# Pass container_client and file_name to the deletion function
|
| 91 |
+
delete_container('useruploadhuggingfaceaudio')
|