I want to convert a numpy array waveform into a wav-like object so that I can upload it to S3 without having to save the waveform to file locally. Imagine I have a numpy array waveform, y
,
y, sr = librosa.load('something.wav', sr = 44100)
How can I convert this numpy array, y
into a wav file-like object to upload to S3 using boto3
's upload_fileobj
method?
According to the boto3
docs, the file-like object must have the properties:
A file-like object to upload. At a minimum, it must implement the read method, and must return bytes.
This is how I would like to upload the file-like object:
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
# with open('filename', 'rb') as data:
# bucket.upload_fileobj(data, 'mykey')
bucket.upload_fileobj(wav_object, 'something.wav')
TLDR:
I want a function that converts a numpy array into a wav-like object that implements the read method and returns bytes.
with wave.open("sound1.wav", "w") as f:
but I don't want to write to a file locally , I just want to return a file-like object. – Grisetteio.BytesIO()
, and docs.python.org/3/library/io.html#io.BytesIO.getvalue when you're done. – Electromagnetic