import requests
import os
def upload_file(upload_data, folder_path, file_path):
"""Upload a single audio file"""
file_name = os.path.basename(file_path)
upload_data['fields']['key'] = folder_path + file_name
with open(file_path, 'rb') as file:
files = {'file': (file_name, file)}
response = requests.post(upload_data['url'], data=upload_data['fields'], files=files)
if response.status_code == 204:
return {'key': folder_path + file_name, 'size': os.path.getsize(file_path)}
else:
raise Exception(f"Upload failed: {response.status_code}")
# Step 1: Use the presigned URL response (from previous API call)
upload_response = {
"uploadData": {
"url": "https://m-prod-ekascribe-batch.s3.amazonaws.com/",
"fields": {
"x-amz-meta-mode": "dictation",
"x-amz-meta-uhid": "unique_patient_id",
"x-amz-meta-hfid": "unique_health_facility_id",
"key": "EC_173210496011417/txn_301/20250617_105524/${filename}",
"x-amz-algorithm": "AWS4-HMAC-SHA256",
"x-amz-credential": "...",
"x-amz-date": "...",
"policy": "...",
"x-amz-signature": "..."
}
},
"folderPath": "EC_173210496011417/txn_301/20250617_105524/",
"txn_id": "txn_301"
}
# Step 2: Add your audio file paths
audio_files = [
"/path/to/your/first_audio.wav",
"/path/to/your/second_audio.mp3",
"/path/to/your/third_audio.m4a"
]
# Step 3: Set the correct action
action = "ekascribe-v2"
# Get transaction ID from upload response
transaction_id = upload_response["txn_id"]
# Upload audio files
upload_results = []
for audio_file in audio_files:
result = upload_file(
upload_response['uploadData'].copy(), # Use copy to avoid modifying original
upload_response['folderPath'],
audio_file
)
upload_results.append(result)
print(f"Uploaded: {result['key']}")
# Process responses
for response in upload_results:
print(f"File: {response['key']}, Size: {response['size']} bytes")