ruby-on-rails – 用s3和雾上传Carrierwave
发布时间:2020-12-17 04:32:00 所属栏目:百科 来源:网络整理
导读:一直试图搜索这个错误的原因很长一段时间,似乎无法找到任何… 所以我有一个rails应用程序,我利用carrierwave上传图片.我还想在我的应用程序中使用Amazon S3进行文件上传存储. 最初,当我开发应用程序时,我允许文件上传到on:文件,即 image_uploader.rb # Choo
一直试图搜索这个错误的原因很长一段时间,似乎无法找到任何…
所以我有一个rails应用程序,我利用carrierwave上传图片.我还想在我的应用程序中使用Amazon S3进行文件上传存储. 最初,当我开发应用程序时,我允许文件上传到on:文件,即 image_uploader.rb # Choose what kind of storage to use for this uploader: storage :file # storage :fog 现在,在完成开发并将其置于实时(我使用heroku)之后,我决定将载波存储更改为S3以在本地进行测试. image_uploader.rb # Choose what kind of storage to use for this uploader: # storage :file storage :fog 但是,现在当我尝试上传图片(无论是用户头像等)时,我收到此错误: Excon::Errors::Forbidden in UsersController#update Expected(200) <=> Actual(403 Forbidden) request => {:connect_timeout=>60,:headers=>{"Content-Length"=>74577,"x-amz- acl"=>"private","Content-Type"=>"image/png","Date"=>"Sun,26 Feb 2012 10:00:43 +0000","Authorization"=>"AWS AKIAJOCDPFOU7UTT4HOQ:8ZnOy7X71nQAM87yraSI24Y5bSw=","Host"=>"s3.amazonaws.com:443"},:instrumentor_name=>"excon",:mock=>false,:read_timeout=>60,:retry_limit=>4,:ssl_verify_peer=>true,:write_timeout=>60,:host=>"s3.amazonaws.com",:path=>"/uploads//uploads%2Fuser%2Favatar%2F1%2Fjeffportraitmedium.png",:port=>"443",:query=>nil,:scheme=>"https",:body=>"x89PNGrnx1Anx00x00x00rIHDRx00x00x00xC2x00x00x00xC3bx06x00x00x00xD0xBDxCEx94x00x00nCiCCPICC Profilex00x00xx01x9Dx96wTSYx13xC0xEF{/xBDxD0x12Bx91x12zrMJx00x91x12zx91^E%$ ... # The code you see above to the far right repeats itself a LOT ... 1@x85xB5txFC_y~xA6=:xB2xD0^xBB~ixBBx82x8Fx9BxAFxE7x04mxB2ixFFx17Ox94SxF7lx87xA8&x00x00x00x00IENDxAEB`x82",:expects=>200,:idempotent=>true,:method=>"PUT"} response => #<Excon::Response:0x007fc88ca9f3d8 @body="<?xml version="1.0" encoding="UTF-8"?>n<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>8EFA56C0DDDC8878</RequestId><HostId>1OxWXppSSUq1MFjQwvnFptuCM3gKOuKdlQQyVSEgvzzv4Aj+r2hSFM2UUw2NYyrR</HostId></Error>",@headers={"x-amz-request-id"=>"8EFA56C0DDDC8878","x-amz-id-2"=>"1OxWXppSSUq1MFjQwvnFptuCM3gKOuKdlQQyVSEgvzzv4Aj+r2hSFM2UUw2NYyrR","Content-Type"=>"application/xml","Transfer-Encoding"=>"chunked",26 Feb 2012 10:00:47 GMT","Connection"=>"close","Server"=>"AmazonS3"},@status=403> 然后它也说明了我的应用程序跟踪: app/controllers/users_controller.rb:39:in `update' 我的REQUEST参数: {"utf8"=>"?","_method"=>"put","authenticity_token"=>"DvADD1vYpCLcghq+EIOwVSjsfmAWCHhtA3VI5VGD/q8=","user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007fc88cde76f8 @original_filename="JeffPortraitMedium.png",@content_type="image/png",@headers="Content-Disposition: form-data; name="user[avatar]"; filename="JeffPortraitMedium.png"rnContent-Type: image/pngrn",@tempfile=#<File:/var/folders/vg/98nv58ss4v7gcbf8px_8dyqc0000gq/T/RackMultipart20120226- 19096-1ppu2sr>>,"remote_avatar_url"=>"","name"=>"Jeff Lam ","email"=>"email@gmail.com","user_bio"=>"Tester Hello","shop"=>"1"},"commit"=>"Update Changes","id"=>"1"} 这是我的users_controller.rb部分代码: def update @user = User.find(params[:id]) if @user.update_attributes(params[:user]) redirect_back_or root_path flash[:success] = "Your have updated your settings successfully." else flash.now[:error] = "Sorry! We are unable to update your settings. Please check your fields and try again." render 'edit' end end 我的image_uploader.rb代码 # encoding: utf-8 class ImageUploader < CarrierWave::Uploader::Base # Include RMagick or MiniMagick support: # include CarrierWave::RMagick include CarrierWave::MiniMagick # Choose what kind of storage to use for this uploader: # storage :file storage :fog # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end # Provide a default URL as a default if there hasn't been a file uploaded: # def default_url # "/images/fallback/" + [version_name,"default.png"].compact.join('_') # end # Process files as they are uploaded: # process :scale => [200,300] # # def scale(width,height) # # do something # end # Create different versions of your uploaded files: version :thumb do process resize_to_fill: [360,250] end version :cover_photo_thumb do process resize_to_fill: [1170,400] end version :event do process resize_to_fill: [550,382] end version :product do process resize_to_fit: [226,316] end # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: def extension_white_list %w(jpg jpeg gif png) end # Override the filename of the uploaded files: # Avoid using model.id or version_name here,see uploader/store.rb for details. # def filename # "something.jpg" if original_filename # end # fix for Heroku,unfortunately,it disables caching,# see: https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Make-Carrierwave-work-on-Heroku def cache_dir "#{Rails.root}/tmp/uploads" end end 最后,在config / initializers中我的fog.rb文件 CarrierWave.configure do |config| config.fog_credentials = { :provider => 'AWS',# required :aws_access_key_id => 'ACCESS_KEY',# required :aws_secret_access_key => 'SECRET_ACCESS_KEY/ZN5SkOUtOEHd61/Cglq9',# required :region => 'Singapore' # optional,defaults to 'us-east-1' } config.fog_directory = 'ruuva/' # required config.fog_public = false # optional,defaults to true end 我真的很困惑我的雾中的一些东西.rb.首先,如果我在我的亚马逊s3账户上创建一个名为“ruuva”的区域“新加坡”,我应该将我的区域更改为新加坡吗? 感谢任何可以提前帮助的人! 解决方法
首先确保通过不设置自定义区域和自定义目录来使用正确的凭据(在默认区域中免费创建假桶)
然后我认为你没有在该地区使用正确的名称.尝试像这样设置您的区域: :region => 'ap-southeast-1' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |