亚马逊s3 - 红宝石.获取刚刚上传的资源的URL

Hom*_*ith 6 ruby ruby-on-rails amazon-s3 amazon-web-services

我有以下代码用于将本地文件上传到Amazon S3 Bucket:

require 'aws/s3'
module AmazonS3

  def self.upload_file(local_file)
    bucket_name = "bucketfortest"

      s3 = AWS::S3.new(
        :access_key_id     => ENV["AMAZON_ACCESS_KEY"], 
        :secret_access_key => ENV["AMAZON_SECRET_KEY"] 
      )


    key = File.basename(local_file)

    amazon_object = s3.buckets[bucket_name].objects[key].write(:file => local_file)
    return amazon_object #How can I get the URL of the object here?

  end
end
Run Code Online (Sandbox Code Playgroud)

我基于这个代码:Upoad文件S3 我试图找到一种方法来获取刚刚上传的对象的URL,但我找不到它是什么.我试过.url,但这给了我一个未定义的方法.我也没有在文档中看到任何内容.

Mat*_*per 13

我根本没有使用过Ruby AWS SDK,但看起来你可以这样做:

获取公共网址:

return amazon_object.public_url
Run Code Online (Sandbox Code Playgroud)

或者私有对象签名URL:

return amazon_object.url_for(:read, :expires => 10*60)
Run Code Online (Sandbox Code Playgroud)