0
我建立這使像這樣CORS(其在開發模式,這樣的規則是相當寬鬆)的API:如何啓用CORS on Rails和S3
config.middleware.insert_before 0, "Rack::Cors", :debug => true, :logger => (-> { Rails.logger }) do
allow do
origins '*'
resource '/cors',
:headers => :any,
:methods => [:post],
:credentials => true,
:max_age => 0
resource '*',
:headers => :any,
:methods => [:get, :post, :delete, :put, :options, :head],
:max_age => 0
end
end
,並且客戶端消耗API好,沒問題。現在我試圖通過Carrierwave,Fog和S3添加文件上傳。在我的配置我有以下幾點:
每當我添加以下配置:
CarrierWave.configure do |config|
config.fog_provider = 'fog/aws' # required
config.fog_credentials = {
provider: 'AWS', # required
aws_access_key_id: 'xxx', # required
aws_secret_access_key: 'yyy', # required
region: 'eu-west-1', # optional, defaults to 'us-east-1'
host: 's3.example.com', # optional, defaults to nil
endpoint: 'https://s3.example.com:8080' # optional, defaults to nil
}
config.fog_directory = 'name_of_directory' # required
config.fog_public = false # optional, defaults to true
config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" } # optional, defaults to {}
end
當不使用S3,我的API任何接觸/ CORS停止工作。所以我認爲問題是我的S3存儲桶上的CORS。
所以我增加了對桶以下CORS:
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>MYIP</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
我仍然得到CORS阻塞調用API。關於如何使這項工作的任何想法?
不幸的是這似乎並沒有工作... – WagnerMatosUK
請確保在標籤使用的是正確的格式。 我的本地開發服務器的示例是: 我使用MYIP = http:// localhost:3000 這對我來說很好,請重新檢查格式並讓我知道這是否適用於您。請在此確認您的問題是由於CORS,即交叉來源請求。 –