2013-10-23 38 views
4

我有下面的代码,用于本地文件上传到亚马逊S3斗:我根据这个代码亚马逊S3 - 红宝石。获取刚上传的资源的URL

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 

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

+0

唐S3的网站只是遵循固定的布局?类似于 .amazonaws.com// .s3.amazonaws.com/。你可以看看'雾'是如何构造URL的线索(实际上,使用雾 - 它使用起来很不错)。 – struthersneil

+0

'.public_url'怎么办? –

回答

11

我没有使用Ruby的AWS SDK可言,但它看起来像你可以这样做:

获取public URL

return amazon_object.public_url 

还是一个signed URL for a private object

return amazon_object.url_for(:read, :expires => 10*60) 
2

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