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

时间:2022-11-26 23:02:38

I have the following code that is used to upload a local file to Amazon S3 Bucket:

我有以下代码用于将本地文件上传到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.

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

2 个解决方案

#1


12  

I haven't used the Ruby AWS SDK at all but it looks like you could do this:

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

Get the public URL:

获取公共网址:

return amazon_object.public_url

Or a signed URL for a private object:

或者私有对象的签名URL:

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

#2


2  

You know the url because it's the same url as the local file's filename. So you can compose the url from the filename and the bucket name, without getting any info from S3 other than success.

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

#1


12  

I haven't used the Ruby AWS SDK at all but it looks like you could do this:

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

Get the public URL:

获取公共网址:

return amazon_object.public_url

Or a signed URL for a private object:

或者私有对象的签名URL:

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

#2


2  

You know the url because it's the same url as the local file's filename. So you can compose the url from the filename and the bucket name, without getting any info from S3 other than success.

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