Rails 3、heroku、aws-s3,只是尝试将文件上传到 S3,然后将其发布 (http/multipart) 到我们的应用程序

发布于 2024-10-17 17:52:08 字数 1017 浏览 0 评论 0原文

我们的应用程序托管在 Heroku,因此没有本地文件存储。

我们使用的第三方 api 不存储它创建的 WAV,而是将文件 (http/multi-part) POST 回我们的应用程序。他们提供了示例代码来“简单地”将该文件发送到 S3。他们提供的代码(如下)不能在 Rails 3 + Heroku 上运行。我怀疑有一些不同的语法来指定我们从中读取的输入文件和临时文件。 (该代码最初是针对 Sinatra 的。我不知道旧的 [:filename][: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}'"

更新:几乎可以工作,但创建的文件长度为零。我有点困惑,不知道如何使用 Tempfile,但我将 require 'tempfile' 添加到控制器类,并将 S3 存储行修改为上面的内容。

整个 POST-a-file 到 Heroku/Tempfile 的事情让我的大脑结冰了……任何想法将不胜感激。一方面...我不知道数据来自哪里...当我检查参数(如果它被发布到应用程序)时,我不应该看到除文件名之外的其他内容吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后来的我们 2024-10-24 17:52:08

它的工作方式应该与 HTML 表单文件上传相同

<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>

尝试首先使用该表单测试您的操作。然后确保您的第三方 api 将文件作为多部分数据 HTML POST 提交提供给您。

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.

甜扑 2024-10-24 17:52:08

答案如下。我们必须将 url 参数从 filename= 更改为 myfilename=,因为 Rails magic 自动使用“filename”作为 POST 数据,因此它们在 params[] 中相互干扰。

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

snd = AWS::S3::S3Object.store(params[:myfilename], 
                        File.open(openme), 
                        'MYBUCKET')

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文