CoolFace
Apppublic

SE-09/HapticsProject

sourceHugging Faceunknownupdated 2y agoView on Hugging Face
2likes
AzureBlobStorageAudio.py91 linesDownload Raw Back to root
1 2from azure.storage.blob import BlobServiceClient3 4# Replace placeholders with your actual credentials5storage_account_name = "useruploadhuggingface"6storage_account_key = "zhrGpPBX6PVD+krncC4nVF4yoweEku/z2ErVxjLiuu/CjAVKqM5O4xlGWEyuWGxptL3mA1pv/6P4+AStjSjLEQ=="7connection_string = f"DefaultEndpointsProtocol=https;AccountName={storage_account_name};AccountKey={storage_account_key};EndpointSuffix=core.windows.net"8 9container_name = "useruploadhuggingfaceaudio"  # Update container name for audio files10#file_path = r"C:\Users\ASUS\Desktop\UoW\2ND YEAR\SDGP\AUDIO\edit\3_second_audio.flac"  # Update path to your MP3 file11#file_name = "uploaded_audio.mp3"12 13 14def 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 client27    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 39def uploadUserAudioToBlobStorage(file_path, file_name):40    """Uploads an MP3 audio file to the specified Azure Blob Storage container and returns the URL.41 42    Args:43        file_path (str): The path to the MP3 audio file.44        file_name (str): The desired name of the blob in Azure Blob Storage.45 46    Returns:47        str: The URL for the uploaded audio file in Azure Blob Storage.48 49    Raises:50        FileNotFoundError: If the specified file is not found.51    """52 53    try:54        # Create BlobServiceClient using the connection string55        blob_service_client = BlobServiceClient.from_connection_string(conn_str=connection_string)56 57        # Get a reference to the blob container58        container_client = blob_service_client.get_container_client(container_name)59 60        # Create the blob client with the specified file name61        blob_client = container_client.get_blob_client(file_name)62 63 64        # Open the audio file in binary mode for upload65        with open(file_path, "rb") as data:66            # Upload the audio data to the blob allowing overwrites67            upload_blob_result = blob_client.upload_blob(data, overwrite=True)68 69 70            # Get the URL for the uploaded blob71            blob_url = blob_client.url72 73            print(f"Audio '{file_name}' uploaded successfully. URL: {blob_url}")74            return blob_url  # Only return the URL75 76    except FileNotFoundError as e:77        print(f"Error: File not found at {file_path}.")78        raise  # Re-raise the exception for further handling79 80 81if __name__ == "__main__":82    # Example usage83    uploaded_audio_url = uploadUserAudioToBlobStorage(file_path, file_name)84    # use the uploaded_audio_url for further processing or sharing85 86    # Retrieve container_client from within the upload function87    blob_service_client = BlobServiceClient.from_connection_string(conn_str=connection_string)88    container_client = blob_service_client.get_container_client(container_name)89 90    # Pass container_client and file_name to the deletion function91    delete_container('useruploadhuggingfaceaudio')