1

私はしばらくこれに取り組んできました。いろいろと試してみたのですが、完全に挫折しました。

Amazon s3 から一連の mp3 ファイルをダウンロードして、Heroku の tmp ディレクトリに保存し、圧縮してから、ファイルをダウンロードしようとしています。

ローカルで動作しますが、Heroku にプッシュすると zip ファイルが作成されますが、破損しているか空 (0 バイト) です。Heroku はエラーをスローしません (以下のログ ファイルを参照)。個々のファイルは正常に書き込まれているようです。それらをtmpに書き込んで送信できます。zipfileが作成されているように見えますか?

私はherokuに本当に慣れていないので、デバッグ方法のヒントも役に立ちます。

どんな助けでも大歓迎です!私はこれを私の人生で解決することはできないようです。

コード:

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

ログ ファイル:

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
4

1 に答える 1

1

だから、多くのデバッグの後。sendfile が問題であることがわかりました...詳細はわかりませんが、詳細についてはこちらを参照してください

私の解決策は、ファイルを圧縮し、zip ファイルを s3 に保存してから、s3 から zip ファイルをダウンロードすることです。確かに最もエレガントではありませんが、私の場合、ファイルを 1 回だけ圧縮する必要があり、それ以降は s3 からダウンロードするだけです。

于 2012-08-28T02:39:51.230 に答える