将文件名发送到服务器,将Paperclip路径文件名从服务器更新到s3(Updating Paperclip path file names from on server to s3)...
將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
總結
以上是生活随笔為你收集整理的将文件名发送到服务器,将Paperclip路径文件名从服务器更新到s3(Updating Paperclip path file names from on server to s3)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博科b8网络版定位服务器位置,如何在企业
- 下一篇: 服务器光信号闪红灯是什么意思,路由器光信