簡體   English   中英

當我從Amazon S3打開簽名的URL時,為什么訪問被拒絕?

[英]Why do I get access denied when I open a signed URL from Amazon S3?

我需要從Amazon S3打開一個已簽名的URL,但我一直拒絕訪問。

例如,訪問以下URL:

https://aidin.s3.amazonaws.com/aidin/staging/attachments/faxattach/94DD749z65ejkh353?AWSAccessKeyId=AKIAJPBOMKHDQ5WNJM3Q&Expires=1373047778&Signature=Y%2F968F9LIfkfHwsp7T8P0CqjQhQ%3D

從此代碼:

 def download path
    local_file = File.new 'attachment' + (Time.now.strftime("%Y%m%d%H%M%S")+(rand * 1000000).round.to_s) + ".pdf", 'wb+'
    # HTTPError is caught in the /process in faxattach.rb
    uri = URI.parse(path)
    http_object = Net::HTTP.new(uri.host, uri.port)
    http_object.use_ssl = true if uri.scheme == 'https'
    request = Net::HTTP::Get.new uri.request_uri
    http_object.start do |http|
      response = http.request request
      local_file.write(response.read_body)
    end
    debugger
    local_file.rewind
    local_file
  end

我在local_file得到這個:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>11A4FB2DF920A272</RequestId><HostId>6qH/f602NxfJa38oW+67Q+5GVx5XD+BJqTzNVR5IhhnvEDhiXUoTR0K90/quZTWk</HostId></Error>

有人知道為什么嗎? 該URL在我的瀏覽器中可以很好地打開,並且我需要額外的查詢參數(例如AWSAccessKeyIDExpires等),出於安全原因,這些參數是存在的。

我將簡化並使用OpenURI:

require 'open-uri'
url = "http://aidin.s3.amazonaws.com/aidin/staging/attachments/faxattach/94DD749z65ejkh353?AWSAccessKeyId=AKIAJPBOMKHDQ5WNJM3Q&Expires=1373047778&Signature=Y%2F968F9LIfkfHwsp7T8P0CqjQhQ%3D"

File.open( 'attachment' + (Time.now.strftime("%Y%m%d%H%M%S")+(rand * 1000000).round.to_s) + ".pdf", 'wb+') do |file|
  file << open(url).read
end

您想要request = Net::HTTP::Get.new(path)

我在上下文中的測試:

require 'net/http'
path = "https://aidin.s3.amazonaws.com/aidin/staging/attachments/faxattach/94DD749z65ejkh353?AWSAccessKeyId=AKIAJPBOMKHDQ5WNJM3Q&Expires=1373047778&Signature=Y%2F968F9LIfkfHwsp7T8P0CqjQhQ%3D"
uri = URI.parse(path)
http_object = Net::HTTP.new(uri.host, uri.port)
http_object.use_ssl = true if uri.scheme == 'https'
request = Net::HTTP::Get.new(path)
http_object.start do |http|
  response = http.request request
  puts response.read_body
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM