2012-02-04 74 views
21

我一直在使用這種永遠和回形針和AWS-S3:AWS :: S3 :: S3Object.url_for - 如何使用新的AWS SDK Gem進行此操作?

def authenticated_url(style = nil, expires_in = 90.minutes) 
     AWS::S3::S3Object.url_for(attachment.path(style || attachment.default_style), attachment.bucket_name, :expires_in => expires_in, :use_ssl => true) 
    end 

新回形針使用AWS-SDK的寶石,它打破這個給錯誤:

undefined method `url_for' for AWS::S3:Class 

有誰知道如何使這種方法適用於新的AWS-SDK gem?

回答

29

要使用aws-sdk gem生成url,您應該使用AWS::S3Object#url_for方法。
您可以使用#s3_object從回形針附件訪問S3Object實例。下面的代碼段可以解決你的問題。

def authenticated_url(style = nil, expires_in = 90.minutes) 
    attachment.s3_object(style).url_for(:read, :secure => true, :expires => expires_in).to_s 
end 
+0

S3Object#url_for返回一個URI :: HTTPS對象。如果你喜歡這個,你可以省略方法鏈中的#to_s。 – 2012-02-09 22:38:50

+0

AWS :: S3 :: Base是舊aws-s3 gem中的一個類,但它不作爲aws-sdk gem的一部分存在。雖然這兩個gems都定義了AWS :: S3類。我將深入瞭解堆棧跟蹤並找出引用AWS :: S3 :: Base的內容。 – 2012-02-13 22:28:00

5

查看documentation後,url_for是一個實例方法,而不是一個類方法。

要生成AWS-SDK一個URL,你需要做到以下幾點:

bucket = AWS::S3::Bucket.new(attachment.bucket_name) 
s3object = AWS::S3::S3Object.new(bucket, attachment.path(style || attachment.default_style)) 
s3object.url_for(:read, :expires => expires_in) 

的選項比你指定的人略有不同。

Options Hash (options):

:expires (Object) — Sets the expiration time of the URL; after this time S3 will return an error if the URL is used. This can be an integer (to specify the number of seconds after the current time), a string (which is parsed as a date using Time#parse), a Time, or a DateTime object. This option defaults to one hour after the current time.

:secure (String) — Whether to generate a secure (HTTPS) URL or a plain HTTP url.

:response_content_type (String) — Sets the Content-Type header of the response when performing an HTTP GET on the returned URL.

:response_content_language (String) — Sets the Content-Language header of the response when performing an HTTP GET on the returned URL.

:response_expires (String) — Sets the Expires header of the response when performing an HTTP GET on the returned URL.

:response_cache_control (String) — Sets the Cache-Control header of the response when performing an HTTP GET on the returned URL.

:response_content_disposition (String) — Sets the Content-Disposition header of the response when performing an HTTP GET on the returned URL.

:response_content_encoding (String) — Sets the Content-Encoding header of the response when performing an HTTP GET on the returned URL.

+0

「編輯在提交」?那麼我應該如何使用aws-sdk gem生成一個url? – AnApprentice 2012-02-06 20:28:03

+0

@AnApprentice查看我的編輯。另外,你的方法適用於什麼版本的gem? – Gazler 2012-02-06 20:35:23

+0

@AnApprentice我已再次編輯以反映您的示例代碼。 – Gazler 2012-02-06 20:46:03

3

我最近從AWS-S3到AWS-SDK開關爲好。我更換了我所有的url_for下列要求:

def authenticated_url(style = nil, expires_in = 90.minutes) 
    self.attachment.expiring_url(expires_in, (style || attachment.default_style)) 
end 

你可以看到在回形針問題的討論帖點擊這裏:https://github.com/thoughtbot/paperclip/issues/732

+0

仍然錯誤 – AnApprentice 2012-02-13 22:18:37

+0

請看這裏的方法定義:https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/storage/s3.rb。這與您接受的答案相同。我可以看到它不起作用的唯一原因是,如果您沒有帶有訪問密鑰和密鑰的s3.yml文件,或者您沒有在回形針選項中指定s3。你遇到了什麼錯誤? – John 2012-02-14 05:09:11

9

最近我升級到最新的寶石爲AWS SDK 2紅寶石(AWS-SDK -2.1.13),並且在此SDK版本中更改了預簽名url。

得到它的辦法:

presigner = Aws::S3::Presigner.new 
presigner.presigned_url(:get_object, #method 
         bucket: 'bucket-name', #name of the bucket 
         key: "key-name", #key name 
         expires_in: 7.days.to_i #time should be in seconds 
         ).to_s 

您可以在這裏找到更多的信息: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Presigner.html

+1

我發現Aws :: S3 :: ObjectSummary也響應'presigned_url',並且我不需要Aws :: S3 :: Presigner對象。但是,評論中的細節是黃金,謝謝! – pdobb 2016-08-09 20:25:11