使用 Shrine gem 将图像上传到 s3 中的不同文件夹(按相册名称)
Upload images to different folders in s3 (by album names) using Shrine gem
我设法使用 shrine 将文件上传到 s3,但我正在尝试根据它所属的相册将每张照片上传到不同的文件夹。
假设我有一个名为:abc
:
的存储桶
上传图片到相册:family
应该上传图片到:abc/family/...
上传图片到相册:friends
应该上传图片到:abc/friends/...
我在初始化文件的 Shrine.storages
中没有找到方法。
我想这样做的方法是使用 default_storage
和 dynamic_storage
插件,但我还没有成功。
有什么建议/解决方案吗?
非常感谢:)
关系:
Album has_many :photos
Photo belongs_to :album
Photo
class 有 image_data
神社字段。
我在初始化程序中的代码:(基本内容)
s3_options = {
access_key_id: ENV["S3_KEY"],
secret_access_key: ENV["S3_SECRET"],
region: ENV["S3_REGION"],
bucket: ENV["S3_BUCKET"],
}
Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: "cache", **s3_options),
store: Shrine::Storage::S3.new(prefix: "store", **s3_options),
}
编辑:
我发现有一个名为:pretty_location
的插件,它添加了一个更好的文件夹结构,但它不是我需要的,它在存储桶下添加了 /Photo/:photo_id/image/:image_name
,但我需要相册取而代之的是名字。
我做到了!
通过覆盖 ImageUploader
文件中的 generate_location
:
class ImageUploader < Shrine
def generate_location(io, context = {})
album_name = context[:record].album_name if context[:record].album
name = super # the default unique identifier
[album_name, name].compact.join("/")
end
end
这会将文件上传到::bucket_name/storage/:album_name/:file_name
如果您想要其他文件夹然后“storage
”,您需要更改初始化文件中 Shrine.storages
下的 prefix
。
您可能想在 field_name
上使用 parameterize
(在我的例子中是 album_name.parameterize
),这样路径中就不会有空格和不需要的字符。
致所有正在寻找答案的人!这就是对我有用的,享受吧。
如果您有其他 working/better 解决方案,也请 post 解决。
谢谢。
这是一个示例,其中包含从 JSON 文件中读取的更多自定义信息
{"ID":"14","post_author":"2","guid":"wp-content\/uploads\/2012\/02\/Be-True-Website-Logo3.png","post_type":"attachment","post_parent":"13"}
这允许您通过 运行像这样的每个循环来获取 guid 的位置
require 'json'
require 'pry'
file = File.read('files.json')
array = JSON.parse(file)
posts = array[2]["data"] #please change this to match your JSON file
posts.each do |post|
post_id = post['post_parent']
if Post.find_by_id(post_id)
p = Post.find(post_id)
g = Graphic.new(post_id: post_id)
g.graphic = File.open(post["guid"])
else
g = Graphic.new
g.graphic = File.open(post["guid"])
end
g.save
end
您也可以在 rails 控制台中 运行 以上文件..
下面是您的上传器的样子...
class GraphicUploader < Shrine
include ImageProcessing::MiniMagick
plugin :activerecord
plugin :cached_attachment_data # for retaining the cached file across form redisplays
plugin :restore_cached_data # re-extract metadata when attaching a cached file
plugin :determine_mime_type
plugin :remove_attachment
def generate_location(io, context = {})
name = super # the default unique identifier
if io.is_a?(File)
initial_location = io.to_path
new_location = initial_location.match(/(.*\/).*/)[1]
final_location = [new_location + name].compact.join # returns original path
else
foo = [io.id].compact.join.to_s
end
end
end
end
我设法使用 shrine 将文件上传到 s3,但我正在尝试根据它所属的相册将每张照片上传到不同的文件夹。
假设我有一个名为:abc
:
上传图片到相册:family
应该上传图片到:abc/family/...
上传图片到相册:friends
应该上传图片到:abc/friends/...
我在初始化文件的 Shrine.storages
中没有找到方法。
我想这样做的方法是使用 default_storage
和 dynamic_storage
插件,但我还没有成功。
有什么建议/解决方案吗?
非常感谢:)
关系:
Album has_many :photos
Photo belongs_to :album
Photo
class 有 image_data
神社字段。
我在初始化程序中的代码:(基本内容)
s3_options = {
access_key_id: ENV["S3_KEY"],
secret_access_key: ENV["S3_SECRET"],
region: ENV["S3_REGION"],
bucket: ENV["S3_BUCKET"],
}
Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: "cache", **s3_options),
store: Shrine::Storage::S3.new(prefix: "store", **s3_options),
}
编辑:
我发现有一个名为:pretty_location
的插件,它添加了一个更好的文件夹结构,但它不是我需要的,它在存储桶下添加了 /Photo/:photo_id/image/:image_name
,但我需要相册取而代之的是名字。
我做到了!
通过覆盖 ImageUploader
文件中的 generate_location
:
class ImageUploader < Shrine
def generate_location(io, context = {})
album_name = context[:record].album_name if context[:record].album
name = super # the default unique identifier
[album_name, name].compact.join("/")
end
end
这会将文件上传到::bucket_name/storage/:album_name/:file_name
如果您想要其他文件夹然后“storage
”,您需要更改初始化文件中 Shrine.storages
下的 prefix
。
您可能想在 field_name
上使用 parameterize
(在我的例子中是 album_name.parameterize
),这样路径中就不会有空格和不需要的字符。
致所有正在寻找答案的人!这就是对我有用的,享受吧。
如果您有其他 working/better 解决方案,也请 post 解决。 谢谢。
这是一个示例,其中包含从 JSON 文件中读取的更多自定义信息
{"ID":"14","post_author":"2","guid":"wp-content\/uploads\/2012\/02\/Be-True-Website-Logo3.png","post_type":"attachment","post_parent":"13"}
这允许您通过 运行像这样的每个循环来获取 guid 的位置
require 'json'
require 'pry'
file = File.read('files.json')
array = JSON.parse(file)
posts = array[2]["data"] #please change this to match your JSON file
posts.each do |post|
post_id = post['post_parent']
if Post.find_by_id(post_id)
p = Post.find(post_id)
g = Graphic.new(post_id: post_id)
g.graphic = File.open(post["guid"])
else
g = Graphic.new
g.graphic = File.open(post["guid"])
end
g.save
end
您也可以在 rails 控制台中 运行 以上文件..
下面是您的上传器的样子...
class GraphicUploader < Shrine
include ImageProcessing::MiniMagick
plugin :activerecord
plugin :cached_attachment_data # for retaining the cached file across form redisplays
plugin :restore_cached_data # re-extract metadata when attaching a cached file
plugin :determine_mime_type
plugin :remove_attachment
def generate_location(io, context = {})
name = super # the default unique identifier
if io.is_a?(File)
initial_location = io.to_path
new_location = initial_location.match(/(.*\/).*/)[1]
final_location = [new_location + name].compact.join # returns original path
else
foo = [io.id].compact.join.to_s
end
end
end
end