间歇性Carrierwave S3 403签名与错误不匹配

时间:2012-12-03 03:17:57

标签: ruby-on-rails amazon-s3 carrierwave fog

从heroku上的应用程序上传文件到s3时,我看似随机的错误。我正在使用jquery-file-upload使用CORS方法和此代码将图片上传到我的存储桶中的tmp /目录。

  def url
    temp_url = AWS::S3::S3Object.url_for(
      s3_key,
      S3_CONFIG['bucket'],
      use_ssl: true)
    puts temp_url
    temp_url
    # temp_url.to_s.encode_signs
  end

  def delete_photo_from_s3
    begin
      photo = AWS::S3::S3Object.find(s3_key, S3_CONFIG['bucket'])
      photo.delete
    rescue Exception => e
      Rails.logger.error e.message
    end
  end

  private

  def s3_key
    parent_url = self[:uri]
    # If the url is nil, there's no need to look in the bucket for it
    return nil if parent_url.nil?
    # This will give you the last part of the URL, the 'key' params you need
    # but it's URL encoded, so you'll need to decode it
    object_key = parent_url.split(/\//)
    "#{object_key[3]}/#{object_key[4]}/#{object_key[5]}"
  end

从那里我使用carrierwave上传和处理这些图像。但是,有时上传会无声地失败,我在s3存储桶中收到403 Forbidden错误。不确定是什么导致了这一点。

从那里开始,我使用Qu处理后台作业,使用remote__url调用将图像附加到carrierwave。这是我的后台任务:

class PhotoUploader
  def self.perform(finding_id, photo_id)
    begin
      finding = Finding.find(finding_id)
      photo = Photo.find(photo_id)
      upload = finding.uploads.build
      # attached_picture = photo.temp_image_url || photo.url
      upload.remote_attachment_url = photo.url
      if upload.save!
        Rails.logger.debug "#{Time.now}: Photo #{photo_id} saved to finding..."
        photo.set(:delete_at => 1.hour.from_now)  # UTC, same as GMT (Not local time!)
        photos = Photo.where(:processing => true, :delete_at.lte => Time.now.utc)  # Query for UTC time, same type as previous line (also not local time!)
        finding.unset(:temp_image)
        if photos
          photos.each do |photo|
            photo.destroy
            Rails.logger.debug "Photo #{photo.id} - #{photo.uri} destroyed."
          end
        end
      else
        raise "Could not save to s3!"
      end
    rescue Exception => e
      Rails.logger.debug "#{Time.now}: PH01 - Error processing photo #{photo_id}, trying again... :: #{e.message}"
      retry
    end
  end
end

这有时会起作用,但并非总是如此,这真的很奇怪。 我最终在我的s3日志中得到了一堆这些错误:

  

fc96aee492e463ff67c0a9835c23c81a09c4c36a53cdf297094ded3a7d02c62f actionlog开发[02 / DEC / 2012:20:27:18 0000] 71.205.197.214 - 625CEFB5DB7867A7 REST.GET.OBJECT TMP / 4f75d2fb4e484f2ffd000001 / apcm_photomix1_0022.jpg“GET / actionlog开发/ TMP / 4f75d2fb4e484f2ffd000001 / apcm_photomix1_0022.jpg?AWSAccessKeyId = AKIAI_ _ _ZA6A& Expires = 1354480332& Signature = 4wPc + nT84WEdOuxS6 + Ry4iMNkys = HTTP / 1.1“403 SignatureDoesNotMatch 895 - 8 - ” - “” Ruby“ -

我已经阅读了很多这个问题,而且当签名中有未转义的'+'时,人们似乎有时会遇到这个问题。我不确定这是Carrierwave,Fog还是AWS :: S3问题。

如果您能提供任何帮助,我们将不胜感激。

感谢。

2 个答案:

答案 0 :(得分:0)

更好地使用v4签名,这应该可以防止这种错误。只需将选项“signature_version :: v4”添加到url_for调用。

temp_url = AWS::S3::S3Object.url_for(
  s3_key,
  S3_CONFIG['bucket'],
  use_ssl: true,
  signature_version: :v4)

答案 1 :(得分:0)

Fog Excon 存在问题。

请参阅this answer了解如何修复它并切换到使用实际aws-sdk的更好解决方案。


图书馆---磁盘空间---代码行---启动时间---运行时代 - 开发代表

雾--- 28.0M --- 133469 --- 0.693 --- 9 --- 11

aws-sdk --- 5.4M --- 90290 --- 0.098 --- 3 --- 8 *