载波文件未从 S3 中删除
Carrierwave files not deleting from S3
我正在使用 carrierwave
和 carrierwave-aws
gem 从 Rails 应用程序上传到 S3。我可以毫无问题地上传文件,但我无法将它们从 S3 中删除。
我的 ActiveRecord 模型称为 Episode,其视频 属性 由我的 CarrierWave::Uploader::Base
上传者(包括 CarrierWave::Video
)设置。
以下是我遵循的步骤:
- 创建剧集
- 上传视频,验证它是否存在于 S3 上(有效!)
- 从控制台调用
episode.remove_video!; episode.save!
- 此时,视频仍然存在于 S3 上,尽管没有收到任何错误消息
我也试过:
episode.video.remove!
episode.save!
生成此输出,但不会从 S3 中删除文件:
[180] pry(main)> episode.video.remove!
=> [:remove_versions!]
我也试过:
episode.destroy!
应该调用 ActiveRecord 回调 added by Carrierwave,但没有。我添加了自己的 before_destroy 方法(下面的 destroy_assets
),但这也不起作用。
这是我的设置:
carrierwave.rb
CarrierWave.configure do |config|
config.storage = :aws
config.aws_bucket = 'BUCKET_NAME'
config.aws_acl = 'public-read'
config.aws_authenticated_url_expiration = 60 * 60 * 24 * 7
config.aws_attributes = {
expires: 2.week.from_now.httpdate,
cache_control: 'max-age=604800'
}
config.aws_credentials = {
access_key_id: ENV['aws_access_key'],
secret_access_key: ENV['aws_secret'],
region: 'us-east-1'
}
config.remove_previously_stored_files_after_update = true
end
video_uploader.rb
class VideoUploader < CarrierWave::Uploader::Base
include CarrierWave::Video
storage :was
def store_dir
"uploads/videos/#{model.id}"
end
end
episode.rb
class Episode < ActiveRecord::Base
mount_uploader :video, VideoUploader
before_destroy :destroy_assets
def destroy_assets
self.video.remove! if self.video
self.save!
end
end
我使用的 AWS 凭证适用于具有 AmazonS3FullAccess
策略的 IAM 用户,如果这对此处有任何影响的话。
我会将您的 store_dir 更新为:
"uploads/videos/#{mounted_as}/#{model.id}"
这样carrierwave在利用挂载系统删除文件的时候,内部调用全名方法就知道去哪里找了!
我正在使用 carrierwave
和 carrierwave-aws
gem 从 Rails 应用程序上传到 S3。我可以毫无问题地上传文件,但我无法将它们从 S3 中删除。
我的 ActiveRecord 模型称为 Episode,其视频 属性 由我的 CarrierWave::Uploader::Base
上传者(包括 CarrierWave::Video
)设置。
以下是我遵循的步骤:
- 创建剧集
- 上传视频,验证它是否存在于 S3 上(有效!)
- 从控制台调用
episode.remove_video!; episode.save!
- 此时,视频仍然存在于 S3 上,尽管没有收到任何错误消息
我也试过:
episode.video.remove!
episode.save!
生成此输出,但不会从 S3 中删除文件:
[180] pry(main)> episode.video.remove!
=> [:remove_versions!]
我也试过:
episode.destroy!
应该调用 ActiveRecord 回调 added by Carrierwave,但没有。我添加了自己的 before_destroy 方法(下面的 destroy_assets
),但这也不起作用。
这是我的设置:
carrierwave.rb
CarrierWave.configure do |config|
config.storage = :aws
config.aws_bucket = 'BUCKET_NAME'
config.aws_acl = 'public-read'
config.aws_authenticated_url_expiration = 60 * 60 * 24 * 7
config.aws_attributes = {
expires: 2.week.from_now.httpdate,
cache_control: 'max-age=604800'
}
config.aws_credentials = {
access_key_id: ENV['aws_access_key'],
secret_access_key: ENV['aws_secret'],
region: 'us-east-1'
}
config.remove_previously_stored_files_after_update = true
end
video_uploader.rb
class VideoUploader < CarrierWave::Uploader::Base
include CarrierWave::Video
storage :was
def store_dir
"uploads/videos/#{model.id}"
end
end
episode.rb
class Episode < ActiveRecord::Base
mount_uploader :video, VideoUploader
before_destroy :destroy_assets
def destroy_assets
self.video.remove! if self.video
self.save!
end
end
我使用的 AWS 凭证适用于具有 AmazonS3FullAccess
策略的 IAM 用户,如果这对此处有任何影响的话。
我会将您的 store_dir 更新为:
"uploads/videos/#{mounted_as}/#{model.id}"
这样carrierwave在利用挂载系统删除文件的时候,内部调用全名方法就知道去哪里找了!