我有以下代码用于将本地文件上传到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
我的代码基于:Upoad file S3
我试图找到一种方法来获取刚刚上传的对象的URL,但我找不到它是什么。我试过.url
,但这给了我一个未定义的方法。我也没有在文档中看到任何内容。
答案 0 :(得分:13)
我根本没有使用过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)
答案 1 :(得分:2)
您知道该网址,因为它与本地文件的文件名相同。因此,您可以从文件名和存储桶名称中编写网址,而无需从S3获取除成功之外的任何信息。