2012-12-03 57 views
0

从heroku上的应用程序上传文件到s3时,我得到看似随机的erorrs。我正在使用jquery-file-upload将图片上传到我的桶中的tmp /目录,使用CORS方法和此代码。Intermittent Carrierwave S3 403签名不匹配错误

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-development/tmp/4f75d2fb4e484f2ffd000001/apcm_photomix1_0022.jpg?AWSAccessKeyId=AKIAI_ _ _ZA6A &过期= 1354480332 &签名= 4wPc + nT84WEdOuxS6 + Ry4iMNkys = HTTP/1.1” 403 SignatureDoesNotMatch 895 - 8 - “ - ”“Ruby” -

我已经阅读了很多关于这方面的内容,看起来人们有时会在签名中出现非转义'+'的时候出现这个问题。我不确定这是Carrierwave,Fog还是AWS :: S3问题。

如果您可以提供任何援助,这将不胜感激。

谢谢。

+0

得到了同样的问题。你有没有找到解决办法? – damienbrz

+0

我找到了解决方法。首先,确保你的服务器的系统时间准确无误。这可能会导致AWS的“时间偏差”错误。然后将此添加到您的载波上传器 'def process_uri(uri) URI。解析(uri) 结束' 这将覆盖默认的carrierwave url解析器,我认为这是解析器。 你可以在这里阅读更多关于这个问题https://github.com/carrierwaveuploader/carrierwave/issues/700 – Bramanga

回答

0

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

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

它与EXCON问题。

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


图书馆---磁盘空间--- ---代码的启动时间---运行时的DEP的线路---开发的DEP

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

AWS-SDK - 5.4M --- 90290 --- 0.098 --- 3 --- 8 *