2012-02-04 38 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