将Paperclip路径文件名从服务器更新到s3(Updating Paperclip path file names from on server to s3)
我有一个回形针实例,我将我的文件迁移到另一个区域。 最初文件存储在我的服务器上,只根据创建的记录的id和原始id给出了文件名。 现在我将它们移动到s3并希望更新文件名以使其正常工作。 我设置了我的回形针配置,如下所示:
:path => ":class/:attachment/:hash-:style.:extension",
:url => ":s3_domain_url",
:hash_secret => SECRET,
:hash_data => ":class/:attachment/:id/:updated_at"
我更新了我的文件的原始记录文件名是唯一的,并将它们移动到我的s3实例。 不幸的是现在我无法从s3下载文件,我认为这是因为paperclip使用了错误的文件名路径。 一个基于路径默认值的,现在使用我的配置文件设置。 我希望能够更新我的文件file_name字段,以便新文件的路径正确,我可以适当地下载它们。 有没有办法直接根据我的秘密和hash_data调用回形针散列函数,这样我就可以更新那些file_name字段并能够立即提取这些记录? 自从我的原始服务器移动后上传的所有内容似乎都正常工作。
I have a paperclip instance that I am migrating my files to a different area. Originally the files were stored on my server and just given a filename based on the id of the record created and the original id. Now I'm moving them to s3 and want to update the filenames to work appropriately. I setup my paperclip config like so:
:path => ":class/:attachment/:hash-:style.:extension",
:url => ":s3_domain_url",
:hash_secret => SECRET,
:hash_data => ":class/:attachment/:id/:updated_at"
I updated the original records filenames for my files to be unique and moved them over to my s3 instance. Unfortunately now I am unable to pull down the files from s3 and I think it is because paperclip is using the wrong path for the filenames. One that is based off the path default that is now set using my config file. I want to be able to update my files file_name field so that the path is correct for the new files and I am able to download them appropriately. Is there a way to call paperclips hashing function based on my secret and hash_data directly so I can update those file_name fields and be able to pull those records now? Everything that has been uploaded since the move from my original servers seems to work appropriately.
原文:https://stackoverflow.com/questions/31347373
更新时间:2021-01-16 09:01
最满意答案
假设您有一个名为profile_pic的附件的模型用户 ;
进入rails控制台,例如。 rails c然后获取你有附件的模型的对象,例如。 u = User.find(100) 。
现在输入u.profile_pic.url以获取url或u.profile_pic_file_name以获取文件名。
要查看其他选项(例如旧选项)的效果,您可以执行此操作;
p = u.profile_pic # gets the paperclip attachment for profile_pic
puts p.url # gets the current url
p.options.merge!(url: '/blah/:class/:attachment/:id_partition/:style/:filename')
puts p.url # now shows url with the new options
类似地, p.path将显示本地文件路径,其中包含您选择的任何选项。
长话短说,有点像;
User.where('created_at < some_date').map do |x|
"#{x.id} #{x.profile_pic_file_name} #{x.profile_pic.path}"
end
应该给你你想要的:)
Say you have a model User with an attachment named profile_pic;
Go into the rails console eg. rails c and then get an object for the model you have the attachment on, eg. u = User.find(100).
Now type u.profile_pic.url to get the url or u.profile_pic_file_name to get the filename.
To see the effect of other options (for example your old options) you can do;
p = u.profile_pic # gets the paperclip attachment for profile_pic
puts p.url # gets the current url
p.options.merge!(url: '/blah/:class/:attachment/:id_partition/:style/:filename')
puts p.url # now shows url with the new options
Similarly p.path will show the local file path with whatever options you pick.
Long story short, something like;
User.where('created_at < some_date').map do |x|
"#{x.id} #{x.profile_pic_file_name} #{x.profile_pic.path}"
end
should give you what you want :)
相关问答
假设你在用户的实例上有一个名为头像的附件,你可以使用user.avatar.path来获得文件系统上文件的完整路径,并且你可以使用user.avatar.url来给出你可能的路径用于图像标签和什么。 那是你的意思吗? Assuming you had an attachment called avatar on an instance of a user, you can use user.avatar.path to get the full path of the file on the fi
...
假设您有一个名为profile_pic的附件的模型用户 ; 进入rails控制台,例如。 rails c然后获取你有附件的模型的对象,例如。 u = User.find(100) 。 现在输入u.profile_pic.url以获取url或u.profile_pic_file_name以获取文件名。 要查看其他选项(例如旧选项)的效果,您可以执行此操作; p = u.profile_pic # gets the paperclip attachment for profile_pic
puts p
...
你没有设置一个桶。 它位于你的s3.yml文件中,但是你没有从你对has_attached_file的调用中读取这个值。 Paperclip S3 docs: http : //rubydoc.info/gems/paperclip/Paperclip/Storage/S3#s3_protocol-instance_method 另外,请注意那些告诉你不要使用Heroku的s3.yml文件的人。 这是一种浪费,只是添加了抽象,无需购买任何东西。 您已经使用所需的值设置了ENV,因此请使用它们。 我
...
我有同样的问题。 我认为这与Paperclip v3代码有关。 我在我的Gemfile中指定使用旧版本。 # Gemfile
gem "paperclip", "~> 2.7.0"
和裁剪工作。 我不确定这对你来说是否是一个很好的解决方案,但它暂时对我有用。 I have the same issue. I think it's related to the Paperclip v3 code. I specified in my Gemfile to use the older versio
...
无论如何,任何可以使用开发工具的人都可以看到S3 URL,因此暴露它不是一个安全漏洞。 有些人认为这是糟糕的用户体验,但这是另一次的讨论。 The S3 URL will be visible to anyone who can use dev tools anyway so exposing it is not a security vulnerability. Some would argue it is bad UX but that is a discussion for another
...
以前有这个问题! 通过将桶变量放入模型本身(实时代码)来解决它: #app/models/image.rb
has_attached_file :image,
:styles => { :medium => "x300", :thumb => "x100" },
:default_url => "**********",
:storage => :s3,
:bucke
...
我不完全确定这是它,但是你加载的s3_credentials与我在生产网站上使用的不同。 我的配置行是: :s3_credentials => "#{RAILS_ROOT}/config/s3.yml"
代替 :s3_credentials => YAML.load_file("#{RAILS_ROOT}/config/s3.yml")[RAILS_ENV]
I'm not entirely sure this is it, but your loading of the s3_credent
...
据我所知,我确实需要将S3对象转换为File ,如@ oregontrail256所示。 我使用Fog gem来做到这一点。 s3 = Fog::Storage.new(
:provider => 'AWS',
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
)
directory
...
哦耶!! 最后我抓住了它:我需要更改我的_form.html.erb
...
至 { :multipart => true } do |f| %>
...
Oh yeah!! Finally i caught it: I need to change my _form.html.erb, from
...
我通过使用后保存解决了这个问题。 请在此处查看与此主题相关的答案 I have worked around this, by using the after save. See my answer related to this subject here