我有以下用于将本地文件上传到 Amazon S3 存储桶的代码:
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
,但这给了我一个未定义的方法。我也没有在文档中看到任何内容。
最佳答案
我根本没有使用过 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)
关于ruby-on-rails - 亚马逊 s3 - ruby 。获取刚刚上传的资源的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19553716/