2017-07-26 158 views
0

因此,我有一個當前部署到Heroku的rails/react應用程序。一切工作減去上傳功能。我從本地存儲切換到AWS(CarrierWave/Fog),但當發送PUT請求用於上傳時,它會出錯。以下是我有...AWS/Heroku上傳文件時出錯

2017-07-26T00:15:26.403722+00:00 app[web.1]: [d2e717f7-5bc0-4832-a441-0f382dffc6c7] (3.9ms) BEGIN 
2017-07-26T00:15:26.409753+00:00 app[web.1]: [d2e717f7-5bc0-4832-a441-0f382dffc6c7] SQL (3.3ms) INSERT INTO "photos" ("user_id", "created_at", "updated_at", "photos") VALUES ($1, $2, $3, $4) RETURNING "id" [["user_id", 4], ["created_at", "2017-07-26 00:15:26.404361"], ["updated_at", "2017-07-26 00:15:26.404361"], ["photos", "[\"15740973_10211933753000319_7147015776553812508_n.jpg\"]"]] 
2017-07-26T00:15:27.103871+00:00 heroku[router]: at=info method=POST path="/photos" host=www.--MY-DOMAIN-NAME--.com request_id=d2e717f7-5bc0-4832-a441-0f382dffc6c7 fwd="76.118.245.207" dyno=web.1 connect=1ms service=3593ms status=500 bytes=1827 protocol=http 
2017-07-26T00:15:27.099506+00:00 app[web.1]: [d2e717f7-5bc0-4832-a441-0f382dffc6c7] (5.3ms) ROLLBACK 
2017-07-26T00:15:27.100107+00:00 app[web.1]: [d2e717f7-5bc0-4832-a441-0f382dffc6c7] Completed 500 Internal Server Error in 3404ms (ActiveRecord: 13.3ms) 
2017-07-26T00:15:27.101564+00:00 app[web.1]: [d2e717f7-5bc0-4832-a441-0f382dffc6c7] 
2017-07-26T00:15:27.101609+00:00 app[web.1]: [d2e717f7-5bc0-4832-a441-0f382dffc6c7] Excon::Error::Forbidden (Expected(200) <=> Actual(403 Forbidden) 
2017-07-26T00:15:27.101611+00:00 app[web.1]: excon.error.response 
2017-07-26T00:15:27.101625+00:00 app[web.1]: :body   => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>70D9E2369BE5D8A0</RequestId><HostId>qFX1z2eqsP+/Mt9j+uompGs2Z4RRfNDb1ZqMJ6Bfs+ijY8oUEI56XLXeM8kAoWvK276DauKGRYg=</HostId></Error>" 
2017-07-26T00:15:27.101626+00:00 app[web.1]: :cookies  => [ 
2017-07-26T00:15:27.101627+00:00 app[web.1]: ] 
2017-07-26T00:15:27.101628+00:00 app[web.1]: :headers  => { 
2017-07-26T00:15:27.101629+00:00 app[web.1]:  "Connection"  => "close" 
2017-07-26T00:15:27.101630+00:00 app[web.1]:  "Content-Type"  => "application/xml" 
2017-07-26T00:15:27.101631+00:00 app[web.1]:  "Date"    => "Wed, 26 Jul 2017 00:15:26 GMT" 
2017-07-26T00:15:27.101632+00:00 app[web.1]:  "x-amz-id-2"  => "qFX1z2eqsP+/Mt9j+uompGs2Z4RRfNDb1ZqMJ6Bfs+ijY8oUEI56XLXeM8kAoWvK276DauKGRYg=" 
2017-07-26T00:15:27.101634+00:00 app[web.1]: } 
2017-07-26T00:15:27.101631+00:00 app[web.1]:  "Server"   => "AmazonS3" 
2017-07-26T00:15:27.101633+00:00 app[web.1]:  "x-amz-request-id" => "70D9E2369BE5D8A0" 
2017-07-26T00:15:27.101635+00:00 app[web.1]: :host   => "MY-BUCKET-NAME-production.s3-us-east-2.amazonaws.com" 
2017-07-26T00:15:27.101636+00:00 app[web.1]: :local_address => "172.16.75.138" 
2017-07-26T00:15:27.101636+00:00 app[web.1]: :local_port => 50501 
2017-07-26T00:15:27.101637+00:00 app[web.1]: :path   => "/uploads/photo/photos/20/15740973_10211933753000319_7147015776553812508_n.jpg" 
2017-07-26T00:15:27.101638+00:00 app[web.1]: :port   => 443 
2017-07-26T00:15:27.101638+00:00 app[web.1]: :reason_phrase => "Forbidden" 
2017-07-26T00:15:27.101639+00:00 app[web.1]: :remote_ip  => "52.219.80.56" 
2017-07-26T00:15:27.101640+00:00 app[web.1]: :status  => 403 
2017-07-26T00:15:27.101640+00:00 app[web.1]: :status_line => "HTTP/1.1 403 Forbidden\r\n" 
2017-07-26T00:15:27.101641+00:00 app[web.1]:): 
2017-07-26T00:15:27.101673+00:00 app[web.1]: [d2e717f7-5bc0-4832-a441-0f382dffc6c7] 
2017-07-26T00:15:27.101718+00:00 app[web.1]: [d2e717f7-5bc0-4832-a441-0f382dffc6c7] app/controllers/photos_controller.rb:36:in `create' 

.env file

AWS_ACCESS_KEY_ID  = Retracted 
AWS_SECRET_ACCESS_KEY = Retracted 
S3_BUCKET    = Retracted 

carrierwave.rb文件。位於config/initializers/carrierwave.rb

require 'carrierwave/orm/activerecord' 

CarrierWave.configure do |config| 
    if !Rails.env.test? 
    config.fog_credentials = { 
     provider: "AWS", 
     aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"], 
     aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"], 
     :region => 'us-east-2' 
    } 
    config.fog_directory = ENV["S3_BUCKET"] 
    end 
end 

我上傳文件,photos_uploader.rb

class PhotosUploader < CarrierWave::Uploader::Base 

    ... 

    # Choose what kind of storage to use for this uploader: 
    if Rails.env.test? 
     storage :file 
    else 
     storage :fog 
    end 

    ... 

我用先前的專案這種方法並沒有問題。兩者之間唯一的區別在於我使用的是Heroku域名。在這一個上,我購買了我自己的域名,並將其設置爲使DNS指向Heroku。我在想,可能這是在搞亂事情,但我現在沒有想法。

正在運行,heroku run rails c,然後Photo.all確認沒有任何內容保存到數據庫。

+0

你肯定已經給'寫'對與您正在使用的憑據相關的用戶/身份的權限?你的地區確實是正確的,你的水桶名稱(我想你在日誌中改變了它)?你沒有限制基於IP或任何其他限制的存取權限?順便說一句,我認爲你會更好地使用'carrierwave-aws'而不是'fog',儘管這在這裏不重要。 –

+0

是的。用戶擁有AmazonSESFullAccess。存儲桶名稱也匹配。我不知道限制訪問,但一切都應該在這裏結帳... –

+0

你確定Bucket的名字? '我的 - 桶 - 名字 - 生產.3-us-east-2.amazonaws.com'看起來很奇怪。 –

回答

0

所以,這裏有點尷尬。務必檢查拼寫...

我對我的用戶的權限是AmazonSESFullAccess

,當然這是一個很大的不同是我想要的東西 - AmazonS3FullAccess