Rails 和 Paperclip:无法使用 Cloudfront 上传图像

问题描述 投票:0回答:1

我有一个旧的 Rails 应用程序,它使用 Paperclip 与 AWS 交互。由于 TLS 兼容性问题,我正在迁移到 Cloudfront,但在上传照片时遇到了问题。我可以很好地获取它们,并确保使用 Cloudfront URL 获取它们。

这是我模型中的配置:

has_attached_file :image, :storage => :s3,
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
    :path => "images/:attachment/:id/:style.:extension",
    :url => ':s3_alias_url',
    :s3_host_alias => 'XXX.cloudfront.net',
    :styles => {
      :large => {:geometry => "3500x3500>", :processors => [:cropper]},
      :medium => {:geometry => "512x512>", :processors => [:cropper]},
      :thumb => {:geometry => "160x160>", :processors => [:cropper]}    }

但是,当我尝试上传照片时,我收到一个错误,从该错误来看,它似乎没有尝试使用 Cloudfront URL:

Processing Admin::ImagesController#create (for 24.154.30.187 at 2023-11-03 01:39:51) [POST]
  Parameters: {"controller"=>"admin/images", "commit"=>"Upload Images", "property_id"=>"15948", "action"=>"create", "image"=>{"image"=>[#<File:/tmp/RackMultipart20231103-1151-c5g19p-0>, #<File:/tmp/RackMultipart20231103-1151-y1vfa4-0>], "caption"=>""}}
[paperclip] Saving attachments.
[paperclip] saving images/images/467324/thumb.jpg
New RightAws::S3Interface using shared connections mode
Opening new HTTPS connection to s3.amazonaws.com:443
##### RightAws::S3Interface returned an error: 403 Forbidden
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>InvalidTlsVersion</Code><Message>Amazon S3 will stop supporting TLS 1.0 and TLS 1.1 connections. Please update your client to use TLS version 1.2 or above. To learn more and to update your client, see https://go.aws/3AUlVSb. For further assistance, contact AWS suppo$
##### RightAws::S3Interface request: https://s3.amazonaws.com:443/ ####
Closing HTTPS connection to s3.amazonaws.com:443, reason: 'RightAws::AWSErrorHandler: code: 403: 'Forbidden''

RightAws::AwsError (InvalidTlsVersion: Amazon S3 will stop supporting TLS 1.0 and TLS 1.1 connections. Please update your client to use TLS version 1.2 or above. To learn more and to update your client, see https://go.aws/3AUlVSb. For further assistance, contact AWS support.):
  /home/ubuntu/.rvm/gems/ruby-1.8.7-p371@vires/gems/right_aws-3.0.4/lib/awsbase/right_awsbase.rb:562:in `request_info_impl'
  /home/ubuntu/.rvm/gems/ruby-1.8.7-p371@vires/gems/right_aws-3.0.4/lib/s3/right_s3_interface.rb:203:in `request_info'
  /home/ubuntu/.rvm/gems/ruby-1.8.7-p371@vires/gems/right_aws-3.0.4/lib/s3/right_s3_interface.rb:240:in `create_bucket'
  /home/ubuntu/.rvm/gems/ruby-1.8.7-p371@vires/gems/right_aws-3.0.4/lib/s3/right_s3.rb:103:in `bucket'
  vendor/plugins/paperclip/lib/paperclip/storage.rb:163:in `s3_bucket'
  vendor/plugins/paperclip/lib/paperclip/storage.rb:199:in `flush_writes'
  vendor/plugins/paperclip/lib/paperclip/storage.rb:195:in `each'
  vendor/plugins/paperclip/lib/paperclip/storage.rb:195:in `flush_writes'
  vendor/plugins/paperclip/lib/paperclip/attachment.rb:141:in `save'
  vendor/plugins/paperclip/lib/paperclip.rb:331:in `send'
  vendor/plugins/paperclip/lib/paperclip.rb:331:in `save_attached_files'
  vendor/plugins/paperclip/lib/paperclip.rb:324:in `each_attachment'
  vendor/plugins/paperclip/lib/paperclip.rb:323:in `each'
  vendor/plugins/paperclip/lib/paperclip.rb:323:in `each_attachment'
  vendor/plugins/paperclip/lib/paperclip.rb:330:in `save_attached_files'
  activesupport (2.3.2) lib/active_support/callbacks.rb:178:in `send'
  activesupport (2.3.2) lib/active_support/callbacks.rb:178:in `evaluate_method'
  activesupport (2.3.2) lib/active_support/callbacks.rb:166:in `call'
  activesupport (2.3.2) lib/active_support/callbacks.rb:93:in `run'
  activesupport (2.3.2) lib/active_support/callbacks.rb:92:in `each'
  activesupport (2.3.2) lib/active_support/callbacks.rb:92:in `send'
  activesupport (2.3.2) lib/active_support/callbacks.rb:92:in `run'
  activesupport (2.3.2) lib/active_support/callbacks.rb:276:in `run_callbacks'
  activerecord (2.3.2) lib/active_record/callbacks.rb:344:in `callback'
  activerecord (2.3.2) lib/active_record/callbacks.rb:251:in `create_or_update'
  activerecord (2.3.2) lib/active_record/base.rb:2539:in `save_without_validation'
  activerecord (2.3.2) lib/active_record/validations.rb:1009:in `save_without_dirty'
  activerecord (2.3.2) lib/active_record/dirty.rb:79:in `save_without_transactions'
  activerecord (2.3.2) lib/active_record/transactions.rb:229:in `send'
  activerecord (2.3.2) lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
  activerecord (2.3.2) lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
  activerecord (2.3.2) lib/active_record/transactions.rb:182:in `transaction'
  activerecord (2.3.2) lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
  activerecord (2.3.2) lib/active_record/transactions.rb:196:in `save'
  activerecord (2.3.2) lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
  activerecord (2.3.2) lib/active_record/transactions.rb:196:in `save'
  app/controllers/admin/images_controller.rb:28:in `create'
  app/controllers/admin/images_controller.rb:26:in `each'
  app/controllers/admin/images_controller.rb:26:in `create'

当我查看以前上传的照片时,它会显示正确的网址:https://XXX.cloudfront.net/images/images/XXX/thumb.jpg?2023?1698977169

我错过了什么?

ruby-on-rails amazon-cloudfront paperclip
1个回答
0
投票

第一个问题是我所使用的 Paperclip 版本使用

right_aws
gem,而不是直接使用 AWS SDK。因此,未设置参数来将响应发送到 Cloudfront URL。我必须手动将
request
对象中的服务器设置为 Cloudfront URL。

其次,当通过 Cloudfront 列出来自 S3 的对象或存储桶时,它正在查找的信息没有被返回。我必须删除所有检查,以查看我尝试删除或上传的对象是否已经存在。之后,我成功地使用 TLS 1.1 上传到 S3!

© www.soinside.com 2019 - 2024. All rights reserved.