0

Our app is hosted at Heroku, so no local file storage.

A third party api we're using doesn't store a WAV it creates, it POSTs the file (http/multi-part) back to our app. They provide sample code to 'simply' send that file on to S3. The code they supply (below) doesn't run on Rails 3 + Heroku. I suspect there's some different syntax for specifying the input file and temp file from which we read. (The code was originally for Sinatra. I have NO idea what the old [:filename] and [:tempfile] were for so I removed them and took a guess the syntax was something like this using Tempfile?)

  def post_audio_to_s3
    puts "*** POST_AUDIO_TO_S3 PARAMS:" + params.inspect

    con = AWS::S3::Base.establish_connection!(
      :access_key_id     => 'MYKEY',
      :secret_access_key => 'MYSECRET')

    puts "** CON='#{con.inspect}'"

    snd = AWS::S3::S3Object.store(params[:filename], 
                        Tempfile.open(params[:filename]).path, 
                        'bb_audios')
    puts "** SND='#{snd.inspect}'"

UPDATE: Almost works ,but zero length file created. I'm sort of flailing around with no idea how to use the Tempfile, but I added require 'tempfile' to the controller class and modified the S3 storage line to the above.

This whole POST-a-file to Heroku/Tempfile thing has my brain iced... any ideas would be appreciated. For one thing... I have no idea where the DATA comes from... shouldnt I see something besides the filename when I inspect the params if it's being POSTED to the app?

jpw
  • 18,697
  • 25
  • 111
  • 187

2 Answers2

1

It should work the same way as HTML form file upload

<form action="/post_audio_to_s3" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file"/>
<input type="submit" value="Upload"/>
</form>

Try testing your action using that the form first. And then make sure your third party api is giving you the file as a multipart-data HTML POST submit.

nvunguyen
  • 203
  • 1
  • 4
  • 9
  • thanks but that completely does not address the issue... which is how to use tempfiles on heroku to handle an upload. – jpw Feb 16 '11 at 05:55
  • params[:filename] should be the path to the temporary file which has the content of the uploaded file. Open it and pass to store: AWS::S3::S3Object.store(params[:filename],File.open(params[:filename])) – nvunguyen Feb 16 '11 at 06:58
  • Thanks but no, if filename is for example 1297842022.wav that gives the error Errno::ENOENT (No such file or directory - 1297842022.wav). Using Tempfile per above (which I don't understand) at least creates a file on S3, only empty. – jpw Feb 16 '11 at 07:43
  • Correction: Given my example above. params[:file] should already a Tempfile object which you can pass to store: AWS::S3::S3Object.store(params[:file].original_filename, params[:file]) – nvunguyen Feb 16 '11 at 10:36
  • How is your action called from the third party api? – nvunguyen Feb 16 '11 at 10:36
  • your assistance was helpful in tracking down what was going wrong. Thank you! – jpw Feb 16 '11 at 21:44
0

The answer was as follows. We had to change the url parameter from filename= to myfilename= because the rails magic automatically uses 'filename' for the POSTed data so they were stomping over each other in the params[].

openme = params['filename'].tempfile.path

snd = AWS::S3::S3Object.store(params[:myfilename], 
                        File.open(openme), 
                        'MYBUCKET')
jpw
  • 18,697
  • 25
  • 111
  • 187