2012-02-04 13 views
21

AWS :: S3 :: S3Object.url_for - 新しいAWS SDK Gemでこれを行うにはどうすればいいですか?私はクリップとAWS-S3と永遠にこれを使用してきた

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の宝石と連携させるには?

答えて

29

あなたがAWS::S3Object#url_forメソッドを使用する必要がありAWS-SDKの宝石を使用してURLを生成します。
#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を省略することができます。 –

+0

AWS :: S3 :: Baseは古いaws-s3宝石の中のクラスですが、aws-sdk宝石の一部としては存在しません。どちらの宝石もAWS :: S3クラスを定義します。私はスタックトレースを調べ、AWS :: S3 :: Baseを参照しているものを見つけました。 –

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

"コミット編集":あなたがここでより多くの情報を見つけることができます

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 

?それでは、aws-sdk gemでURLを生成する方法はありますか? – AnApprentice

+0

@AnApprentice私の編集を参照してください。また、あなたのメソッドはどのようなバージョンの宝石のために機能しましたか? – Gazler

+0

@AnApprentice再度サンプルコードを反映するように編集しました。 – Gazler

3

私は最近、同様AWS-SDKにAWS-S3からスイッチを作りました。私は次のようにすべての私な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

+0

https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/storage/s3.rbのメソッド定義を見てください。あなたの受け入れられた答えと同じです。あなたがアクセスキーと秘密鍵を持つs3.ymlファイルを持っていない場合、またはあなたのペーパークリップオプションにs3を指定していない場合、私はそれが動作しないことがわかる唯一の理由です。あなたはどんなエラーを出していますか? – John

9

を最近、私はルビー(AWS-SDK用AWS SDK 2用の最新の宝石へのアップグレード-2.1.13)、このSDKバージョンでは、署名済みURLの取得が変更されました。

それを得るための方法: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Presigner.html

+1

私はAws :: S3 :: ObjectSummaryが 'presigned_url'にも反応することを発見しました.Aws :: S3 :: Presignerオブジェクトは必要ありませんでした。コメントの詳細は金ですが、ありがとう! – pdobb

関連する問題