如何让 Carrierwave 从 S3 中删除文件?

mar*_*ion 1 ruby-on-rails amazon-s3 carrierwave fog ruby-on-rails-4

我有一个普通的 Carrierwave、Fog、S3 设置。上传到 S3 很好,但是当我在本地销毁 Rails 控制台中的记录,或者通过Delete链接从我的 Web 应用程序中删除时,它不会删除 S3 上的文件。

事实上,这就是我的日志的样子:

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)
Run Code Online (Sandbox Code Playgroud)

这是我的file_uploader.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
Run Code Online (Sandbox Code Playgroud)

这是我的Post.rb模型文件:

# == 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
Run Code Online (Sandbox Code Playgroud)

这是我对我的删除操作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
Run Code Online (Sandbox Code Playgroud)

我一直无法使用我为此应用程序创建的这个 AWS IAM 用户从这个 S3 存储桶中删除文件。

什么可能导致这种情况?

Dip*_*pta 5

问题出在您的上传器上。您正在使用安全随机存储目录。

def store_dir
   "files/#{SecureRandom.uuid()}"
end
Run Code Online (Sandbox Code Playgroud)

当carrierwave尝试删除文件时,由于随机值store_dir指向其他地方。我相信您也无法在任何地方显示此文件。

所以试试这样的东西或者不是随机变化的东西。

def store_dir
  "files/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
Run Code Online (Sandbox Code Playgroud)