繁体   English   中英

使用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_storagedynamic_storage插件,但是我没有成功。

有什么建议/解决方案吗?

非常感谢 :)

关系: Album has_many :photos Photo belongs_to :album

Photo类具有用于神社的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 ),这样路径中album_name.parameterize会有空格和多余的字符。

对于在那里寻找答案的任何人! 多数民众赞成在什么对我有用,享受。

如果您还有其他可行/更好的解决方案,请同时发布。 谢谢。

这是从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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM