12

I have the following code that is used to upload a local file to 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

I based this code on: Upoad file S3 I am trying to find out a way to get the URL of the object that was just uploaded, but I fail to find what is it. I tried .url , but that gives me an Undefined Method. I fail to see anything in the docs either.

4

2 回答 2

19

我根本没有使用过 Ruby AWS 开发工具包,但看起来你可以这样做:

获取公共 URL

return amazon_object.public_url

私有对象的签名 URL

return amazon_object.url_for(:read, :expires => 10*60)
于 2013-10-24T00:20:29.320 回答
2

您知道 url,因为它与本地文件的文件名相同。因此,您可以从文件名和存储桶名称组成 url,而无需从 S3 获取除成功之外的任何信息。

于 2013-10-23T23:32:41.543 回答