You need to write WAV files in a Django application, but cannot on Heroku (or S3)

I have a Django application where users upload a WAV file, the backend does some processing, and then pours out the processed .wav file for download. After deploying to Heroku, I found that I cannot write wav files to the "Heroku ephemeral file system" (locally, I write WAV files to myapp / media).

Consolidating SO is that you must use Amazon S3 to store static files. I'm fine with that. The problem is that the python function I use (and all the others that I looked at) needs a file path to write the file. My code that writes .wav files is:

output_path = "..../some_folder"

output_wav = wave.open (output_path, "w")
output_wav.setparams((nchannels, sampwidth, framerate, nframes, comptype,  compname))
output_wav.writeframes(scaled_e)

I researched boto, django-storages and Heroku Directly loading S3 files in Python . However, these solutions assume that you already have a file that it points to (exactly what I cannot create without a path to which I can write).

The tmp folder available for Cedar cedar may work for my purposes (although this is not an ideal solution due to its instability), but I can’t even find on Google how it can be used in a python / Django application ...

Django / Heroku noob gathers here in circles and goes crazy! Any help was greatly appreciated.

+4
source share
3 answers

OK, I managed to get this to work:

  • Python Python open().
  • WAV wav().
  • S3 set_contents_from_file().

( Heroku) :

output_filename = "my_file_name"
output_file = open(output_filename, "w+")
k = Key(bucket)
k.key = output_filename
k.set_contents_from_file(output_file, rewind=True)

:

  • "w +" .
  • "rewind = True" S3.
  • output_filename, S3.
  • .wav Django S3.
+2

S3 , minio-py :

import os
from minio import Minio

# find out your s3 end point here:
# http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region

client = Minio('https://<your-s3-endpoint>',
               access_key='YOUR-ACCESSKEYID',
               secret_key='YOUR-SECRETACCESSKEY')

client.put_object('my-bucket', 'my_file_name', <sizeofdata>, <data>)
+2

s3 boto , , , WAV :

import wave, StringIO

file_via_s3 = Key(objectkey).get_contents_as_string()  #via boto
output_path = StringIO(file_via_s3)
output_wav = wave.open(output_path, "w")
output_wav.setparams((nchannels, sampwidth, framerate, nframes, comptype,  compname))
output_wav.writeframes(scaled_e)
...
#save it back to s3
Key(objectkey).set_contents_from_string(output_wav)
+1
source

Source: https://habr.com/ru/post/1615153/


All Articles