2014-08-30 3 views
0

바닐라 Carrierwave, Fog, S3 설정이 있습니다. S3에 업로드해도 문제가 없지만 레일즈 콘솔의 레코드를 로컬에서 삭제하거나 Delete 링크를 통해 내 웹 앱에서 삭제하면 S3의 파일이 삭제되지 않습니다.Carrierwave에서 S3에서 파일을 삭제하려면 어떻게해야합니까?

# == Schema Information 
# 
# Table name: posts 
# 
# id   :integer   not null, primary key 
# status  :string(255) 
# title  :string(255) 
# photo  :string(255) 
# body  :text 
# created_at :datetime 
# updated_at :datetime 
# user_id :integer 
# ancestry :string(255) 
# file  :string(255) 
# 

class Post < ActiveRecord::Base 
    has_ancestry 
    belongs_to :user 
    resourcify 

    mount_uploader :photo, PhotoUploader 
    mount_uploader :file, FileUploader 
end 

이 : 이것은 내 Post.rb 모델 파일이

# encoding: utf-8 

class FileUploader < CarrierWave::Uploader::Base 
    storage :fog 

    # Override the directory where uploaded files will be stored. 
    # This is a sensible default for uploaders that are meant to be mounted: 
    def store_dir 
    "files/#{SecureRandom.uuid()}" 
    end 

    def extension_white_list 
    %w(pdf) 
    end 
end 

입니다 : 이것은 내 file_uploader.rb 파일입니다

Started DELETE "/posts/13" for 127.0.0.1 at 2014-08-30 04:19:39 -0500 
Processing by PostsController#destroy as HTML 
    Parameters: {"authenticity_token"=>"jcyevWU9M=", "id"=>"13"} 
    Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1 [["id", 13]] 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1 
    (0.3ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]] 
    (0.2ms) BEGIN 
    Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE (("posts"."ancestry" ILIKE '13/%' OR "posts"."ancestry" = '13')) 
    Role Load (0.2ms) SELECT "roles".* FROM "roles" WHERE "roles"."resource_id" = $1 AND "roles"."resource_type" = $2 [["resource_id", 13], ["resource_type", "Post"]] 
    SQL (0.3ms) DELETE FROM "posts" WHERE "posts"."id" = $1 [["id", 13]] 
    (0.4ms) COMMIT 
Redirected to http://localhost:3000/posts 
Completed 302 Found in 401ms (ActiveRecord: 2.2ms) 

:

사실

,이처럼 내 로그가 모습입니다 내 PostsController.rb에 대한 내 삭제 조치 :

# DELETE /posts/1 
    # DELETE /posts/1.json 
    def destroy 
    @post.destroy 
    respond_to do |format| 
     format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

이 응용 프로그램 용으로 만든 AWS IAM 사용자와 함께이 S3 버킷에서 파일을 삭제할 수 없었습니다.

이 문제의 원인은 무엇일까요?

답변

4

업 로더에 문제가 있습니다. 저장소 디렉토리에 보안 임의를 사용하고 있습니다.

def store_dir 
    "files/#{SecureRandom.uuid()}" 
end 

반송파가 파일을 삭제하려고하면 임의의 값 store_dir로 인해 다른 곳을 가리 킵니다. 나는이 파일을 어디에도 표시 할 수 없다고 확신합니다.

이렇게 무작위로 변경되지 않는 무언가를 시도해보십시오.

def store_dir 
    "files/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
end 
+0

어디서나이 파일을 표시 할 수 없으므로 100 % 돈이됩니다. 흥미 롭 군! 그래서 무작위 폴더 이름을 얻을 수있는 방법이 없을까요? – marcamillion

+0

불가능합니다. id, name 또는 다른 모델의 속성으로 추적 할 수있는 경로가 항상 있습니다. –

+0

그래서 임의의 파일 이름일까요? - https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Create-random-and-unique-filenames-for-all-versioned-files – marcamillion