2014-04-04 107 views
1

我有一個iOS api鏈接到rails應用程序。我已經發送了一個iOS應用程序的圖像,我試圖將其存儲爲header_img,但是我得到的錯誤是它們不是公共目錄。在我的heroku API中指定圖像的好路徑是什麼?我應該在哪裏把圖像放在一個heroku rails應用程序中?

這裏是Heroku的錯誤:

2014-04-04T21:51:47.179916+00:00 app[web.1]: 
2014-04-04T21:51:47.179916+00:00 app[web.1]: Errno::EISDIR (Is a directory - public/header_img/): 
2014-04-04T21:51:47.179916+00:00 app[web.1]: app/controllers/groups_controller.rb:98:in `initialize' 
2014-04-04T21:51:47.179916+00:00 app[web.1]: app/controllers/groups_controller.rb:98:in `open' 
2014-04-04T21:51:47.179916+00:00 app[web.1]: app/controllers/groups_controller.rb:98:in `upload_header_img' 
2014-04-04T21:51:47.179916+00:00 app[web.1]: app/controllers/groups_controller.rb:44:in `create' 

這裏是我的組控制器:

class GroupsController < ApplicationController 
    # GET /groups 
    # GET /groups.json 

    before_filter :require_auth 

    def index 
    @privategroups = @user.groups 
    @publicgroups = @user.followees(Group) 

    @groups = (@privategroups + @publicgroups).sort_by(&:created_at).reverse 
    end 

    # GET /groups/1 
    # GET /groups/1.json 
    def show 
    @group = Group.find(params[:id]) 
    @posts = @group.posts 
    end 

    # GET /groups/new 
    # GET /groups/new.json 
    def new 
    @group = Group.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @group } 
    end 
    end 

    # GET /groups/1/edit 
    def edit 
    @group = Group.find(params[:id]) 
    end 

    # POST /groups 
    # POST /groups.json 
    def create 
    @group = Group.new(params[:group]) 
    @group.owner_id = @user.id 
    header_img = @group.header_img || @group.decode_header_img_data 
    if header_img.present? 
     upload_header_img(header_img) 
    end 

    respond_to do |format| 
     if @group.save 
     @membership = @user.memberships.build(group_id: @group.id) 
     @membership.save 
     format.html { redirect_to @group, notice: 'Group was successfully created.' } 
     format.json { render json: @group, status: :created, location: @group } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @group.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /groups/1 
    # PUT /groups/1.json 
    def update 
    @group = Group.find(params[:id]) 

    respond_to do |format| 
     if @group.update_attributes(params[:group]) 
     format.html { redirect_to @group, notice: 'Group was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @group.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /groups/1 
    # DELETE /groups/1.json 
    def destroy 
    @group = Group.find(params[:id]) 
    @group.destroy 

    respond_to do |format| 
     format.html { redirect_to groups_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    def upload_header_img(header_img_io)   
     # upload to the exact location account id and extension 
     extension = File.extname(header_img_io.original_filename) 
     # rails take public as root directory 
     # under the root directory (public) 
     # there are a icon directory 
     header_img_url = "/header_img/"[email protected]_s + extension 

     # open in binary mode 
     File.open("public"+header_img_url,'wb') do |file| 
     file.write(header_img_io.read) 
     end 

     @group.update_attribute(:header_img_url, header_img_url) 
    end 
end 
+3

您可以創建在「/ tmp目錄」的文件夾,但請記住圖像將請求後銷燬做完了。如果您想存儲圖片,則需要上傳到AWS或其他存儲站點。 – ChrisBarthol

回答

1

雖然我不能提供一個直接的答案,我有一些想法,這可能有助於:


According to Heroku,它們運行一個只讀文件系統:

Your app is compiled into a slug for fast distribution by the dyno manager. The filesystem for the slug is read-only, which means you cannot dynamically write to the filesystem for semi-permanent storage. The following types of behaviors are not supported:

  • Caching pages in the public directory
  • Saving uploaded assets to local disk (e.g. with attachment_fu or paperclip)
  • Writing full-text indexes with Ferret Writing to a filesystem database like SQLite or GDBM
  • Accessing a git repo for an app like git-wiki

修正

以來發現上面是他們Bamboo堆棧(舊)。該cedar stack (new) supports temporary file writing

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.

我們使用回形針&上傳的Heroku(在測試時),它通常不支持任何文件將每次覆蓋您重新部署應用


存儲

ChrisBarthol給了一個很好的suggestio n - 在S3或其他第三方系統上存儲任何user-submitted資產。我會進一步說,你可能會使用Paperclip保存文件大汗受益:

#app/models/group.rb 
Class Group < ActiveRecord::Base 
    has_attached_file :header_img 
     :storage => :s3, 
     :s3_credentials => Proc.new{|a| a.instance.s3_credentials } 

    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 
end 
相關問題