Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 当我从AmazonS3打开签名URL时,为什么访问被拒绝?_Ruby On Rails_Ruby_Amazon S3 - Fatal编程技术网

Ruby on rails 当我从AmazonS3打开签名URL时,为什么访问被拒绝?

Ruby on rails 当我从AmazonS3打开签名URL时,为什么访问被拒绝?,ruby-on-rails,ruby,amazon-s3,Ruby On Rails,Ruby,Amazon S3,我需要从AmazonS3打开一个签名的URL,我一直被拒绝访问 例如,访问此URL: https://aidin.s3.amazonaws.com/aidin/staging/attachments/faxattach/94DD749z65ejkh353?AWSAccessKeyId=AKIAJPBOMKHDQ5WNJM3Q&Expires=1373047778&Signature=Y%2F968F9LIfkfHwsp7T8P0CqjQhQ%3D 根据该代码: def dow

我需要从AmazonS3打开一个签名的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\u文件中得到这个:

<?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在我的浏览器中可以正常打开,我需要额外的查询参数,如
AWSAccessKeyID
Expires
等,这些参数是出于安全原因而存在的。

我会简化并使用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
需要“打开uri”
url=”http://aidin.s3.amazonaws.com/aidin/staging/attachments/faxattach/94DD749z65ejkh353?AWSAccessKeyId=AKIAJPBOMKHDQ5WNJM3Q&Expires=1373047778&Signature=Y%2F968F9LIfkfHwsp7T8P0CqjQhQ%3D"
文件.open('attachment'+(Time.now.strftime(“%Y%m%d%H%m%S”)+(rand*1000000).round.tos)+“.pdf”,“wb+”)do |文件|

文件您需要
request=Net::HTTP::Get.new(路径)

我在上下文中的测试:

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

你能得到一个你没有处理的重定向吗?
response
包含什么?@theTinMan响应是403禁止(#),我想这就是问题所在。如果请求的文件大于可用内存怎么办?不确定你的意思。我建议你改变那一行的语法。为我工作。@aceofspades我得到一个403
打开(url)。read
将把整个资源拉入内存,然后将其传递给文件句柄。如果文件大于内存,您只需自己动手即可。相反,将一个块传递给
open
。参见文档中的前两个示例。是。这是在你提到的条件下推荐的。这太奇怪了。因此,如果我在Sinatra应用程序之外运行该代码,那么该代码工作得非常完美。然而,我需要在Sinatra应用程序中使用它,而Sinatra给了我403。有什么想法吗?@user1045696似乎真的不太可能。。。放入github repo,让我看看代码?@user1045696如果:你有一个局部变量的路径,它工作得很好。使用调试器,您可以看到它删除了URL字符串。因此,您需要对url中使用的url进行CGI::编码。然后,在使用路径之前,需要先对CGI::unencode进行编码。但这一部分确实是一个不同的问题。