将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)...相关推荐

  1. 流放者柯南自建服务器 linux,流放者柯南个人服务器修改怎么配置 个人服务器修改配置路径介绍...

    流放者柯南个人服务器修改配置路径: 下面开始更新新开服务器需要进行修改的主要配置. 服务器名修改:路径 X:\conanexiles\ConanSandbox\Config下的DefaultEngin ...

  2. 流浪者柯南自定义服务器,流放者柯南个人服务器修改怎么配置 个人服务器修改配置路径介绍...

    流放者柯南个人服务器修改配置路径: 下面开始更新新开服务器需要进行修改的主要配置. 服务器名修改:路径 X:\conanexiles\ConanSandbox\Config下的DefaultEngin ...

  3. vba不能提取服务器上文件名,从全路径文件名中获取文件名(不含路径)

    从全路径文件名中获取文件名(不含路径) 方法一: Public Function gf_GetFileName(strFullPath As String) As String Dim splitLi ...

  4. arm服务器获取文件路径中文,ssh 访问远程服务器文件路径

    ssh 访问远程服务器文件路径 内容精选 换一换 在IntelliJ上选择"项目",找到".idea"文件夹,单击右键选择"新建>文件" ...

  5. php获取服务器文件路径,php获取服务器路径

    php获取服务器路径 内容精选 换一换 为加强对系统数据的容灾管理,云堡垒机支持配置日志备份,提高审计数据安全性和系统可扩展性.本小节主要介绍如何在系统配置FTP/SFTP服务器参数,将日志远程备份至 ...

  6. ftpclient读取服务器文件能获得文件名文件大小0_Spring WEB工程整合使用FTP,ftp文本文件解析入库,文件上传下载

    友情提示:文章比较长,方法都是有一层层封装的,阅读需要按照文章顺序阅读 首先写一个简单的FTP工具类,先实现最基本的文件上传,下载,删除,拷贝功能.这里操作FTP是用的commons-net-3.3. ...

  7. 物理服务器转虚拟路径,服务器配置虚拟路径

    服务器配置虚拟路径 内容精选 换一换 目标服务器已安装操作系统,并且处于联网状态.目标服务器已安装鲲鹏编译插件. AI Core Error工具依赖于adc和cce,需要在模型转换执行服务器配置如下环 ...

  8. c 服务器文件路径,c打开服务器文件路径

    c打开服务器文件路径 内容精选 换一换 文件作用:可以关闭/打开监听端口.指定监听端口.指定监听IP等.文件路径:Atlas 300场景下,在Host侧服务器上,在~/ide_daemon目录下查看i ...

  9. 服务器的虚拟路径是什么,设置服务器的虚拟路径

    设置服务器的虚拟路径 内容精选 换一换 通过在BIOS中设置一些高级选项,可以有效提升虚拟化平台性能.表1列出了TaiShan服务器和性能相关的BIOS推荐配置项.开启CPU预取配置选项的目的在于CP ...

最新文章

  1. VBA最常用的基础代码、基础功能写法总结
  2. 金融科技监管何时才能完善?
  3. leetcode 229. Majority Element II(多数投票算法)
  4. MySQL 日志文件 说明
  5. 阿里云迁云方式大汇总 1
  6. ssh免密码登陆(三个节点master slave1 slave2)详细带命令版
  7. 我们大家都知道mysql_10个mysql中select语句的简单用法
  8. python迭代函数例题_python map 函数使用,遍历访问可迭代对象
  9. 裘宗燕:C/C++ 语言中的表达式求值
  10. 忘了微信密码怎么办_微信支付密码怎么改?微信支付密码忘了怎么办?详细教程来了!...
  11. 一步一步安装服务器监视软件MRTG
  12. ps、ai、cdr2021平面设计三大软件的区别有哪些?
  13. 2021非科班生的Java面试之路,java思维导图笔记
  14. YOLO系列详解:YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5
  15. php.ini在哪?找不到php.ini,已解决
  16. 数据结构(一)--ArrayList and LinkerList
  17. js学习笔记----JavaScript中DOM扩展的那些事
  18. PlantUML 之时序图
  19. 2021高考仙桃中学成绩查询,仙桃2020高考最高分出炉!汇总仙桃各大中学喜报
  20. 关于SuperSlide插件的使用

热门文章

  1. nurbs非均匀有理B样条实现船体重建
  2. DTS和PTS的解释
  3. FFmpeg编解码处理1-转码全流程简介
  4. Caffe官方教程翻译(5):Classification: Instant Recognition with Caffe
  5. 2017计算机等级考试题,2017年计算机二级考试题库及答案
  6. 讯飞输入法皮肤制作_讯飞输入法拍了拍你 粤语专项计划进展神速应用广泛
  7. 文件系统磁盘管理(一)--文件系统
  8. NDArray自动求导
  9. 对比云备份:企业做出最佳选择
  10. Redis集群的基本配置