首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当使用rubyzip压缩Heroku (雪松)上的临时mp3文件时,Rails Zipfile损坏

当使用rubyzip压缩Heroku (雪松)上的临时mp3文件时,Rails Zipfile损坏
EN

Stack Overflow用户
提问于 2012-08-17 10:36:51
回答 1查看 625关注 0票数 1

我已经在这方面工作了一段时间。尝试了许多不同的东西,我完全被难住了。

我正在尝试从亚马逊s3下载一系列mp3文件,然后将它们存储在Heroku的tmp目录中,压缩它们,然后下载该文件。

它在本地工作,但当它推送到Heroku时,zip文件会被创建,但已损坏/为空(0字节)。Heroku不会抛出任何错误(参见下面的日志文件)。单个文件似乎写得很好。我可以把它们写给tmp,然后把它们发送出去。所以看起来是与创建的zipfile有关?

我是heroku的新手,所以即使是关于如何调试的技巧也会很有帮助。

任何帮助都将不胜感激!我似乎永远也解决不了这个问题。

代码:

代码语言:javascript
运行
复制
def download_album
    require 'rubygems'
    require 'zip/zip'

   if(params.has_key?(:album_url_slug))

     @artist = Artist.find_by_url_slug(params[:url_slug])
     find_album(@artist,params[:album_url_slug])
   else
      @album = album
      @artist = artist

   end

     #Sets Directory Path
    directory_path = "#{Rails.root}/tmp/#{Process.pid}_mp3"
    directory_artist_path = directory_path+"/"+@artist.url_slug
    directory = directory_artist_path+"/"+@album.album_url_slug
    zipfile = @album.al_name+".zip"
    zipfile_name = directory_artist_path+"/"+zipfile

    FileUtils.mkdir_p directory

    #zips files
    Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile|

          #gets mp3's from S3 and writes them into the zip directory

          @album.songs.uniq.each do |songs|

          #sets the name of the file to be loaded
              name =  songs.song_name+".mp3"


               @song_file = AWS::S3::S3Object.value(songs.s3_id, BUCKET)
                # create the file path
                path = File.join(directory,name)




             File.open(path, 'wb') { |f| f.write(@song_file) }
             zipfile.add(name, path)
        end
     end


   send_file(directory_artist_path+"/"+zipfile,
             :filename  =>  @album.al_name+".zip")

日志文件:

代码语言:javascript
运行
复制
2012-08-17T02:32:03+00:00 heroku[web.1]: State changed from starting to up
2012-08-17T02:32:04+00:00 app[web.1]: => Booting WEBrick
2012-08-17T02:32:04+00:00 app[web.1]: => Rails 3.0.7 application starting in production on http://0.0.0.0:40764
2012-08-17T02:32:04+00:00 app[web.1]: => Call with -d to detach
2012-08-17T02:32:04+00:00 app[web.1]:
2012-08-17T02:32:04+00:00 app[web.1]: Started GET "/tedkennedy/album/download/testalbum6" for 216.58.66.202 at 2012-08-17 02:32:04 +0000
2012-08-17T02:32:04+00:00 app[web.1]:
2012-08-17T02:32:04+00:00 app[web.1]: => Ctrl-C to shutdown server
2012-08-17T02:32:05+00:00 app[web.1]: Zipping files!
2012-08-17T02:32:06+00:00 app[web.1]:   Parameters: {"url_slug"=>"tedkennedy", "album_url_slug"=>"testalbum6"}
2012-08-17T02:32:06+00:00 app[web.1]:   Processing by AlbumsController#download_album as HTML
2012-08-17T02:32:06+00:00 app[web.1]: Sent file /app/tmp/tedkennedy/Test album 6.zip (0.1ms)
2012-08-17T02:32:06+00:00 app[web.1]: Completed 200 OK in 1809ms
2012-08-17T02:32:06+00:00 heroku[router]: GET mighty-refuge-6115.herokuapp.com/tedkennedy/album/download/testalbum6 dyno=web.1 queue=0 wait=0ms service=1922ms status=200 bytes=0
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-28 10:39:51

所以经过多次调试之后。事实证明,sendfile在细节上是确定的problem...not,但可以在here中找到更多。

我的解决方案是压缩文件,然后将压缩文件存储在s3上,然后从s3下载压缩文件。诚然,这不是最优雅的,但在我的例子中,文件只需压缩一次,然后我就可以从s3下载。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11999078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档