gpt4 book ai didi

ruby-on-rails - 如何让 Carrierwave 从 S3 中删除文件?

转载 作者:行者123 更新时间:2023-12-02 04:27:50 26 4
gpt4 key购买 nike

我有一个普通的 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)

这是我的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

这是我的 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

这是我对 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 存储桶中删除文件。

这可能是什么原因造成的?

最佳答案

问题出在您的 uploader 上。您正在使用安全随机作为存储目录。

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

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

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

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

关于ruby-on-rails - 如何让 Carrierwave 从 S3 中删除文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25580950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com