推荐的方法来在Ruby中生成S3存储桶的预签名URL [英] Recommended way to generate a presigned url to S3 Bucket in Ruby

查看:132
本文介绍了推荐的方法来在Ruby中生成S3存储桶的预签名URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Rails服务器上生成一个预签名的URL,以发送给浏览器,以便浏览器可以上载到S3.

I am trying to generate a pre-signed url on my Rails server to send to the browser, so that the browser can upload to S3.

似乎aws-sdk-s3是将来要使用的宝石.但是不幸的是,我还没有遇到可以提供清晰说明的gem文档.似乎有几种不同的方法可以使用,并且希望就以下方法之间的差异提供任何指导-

It seems like aws-sdk-s3 is the gem to use going forward. But unfortunately, I haven't come across documentation for the gem that would provide clarity. There seem to be a few different ways of doing so, and would appreciate any guidance on the difference in the following methods -

  1. 使用Aws::S3::Presigner.new(

  1. Using Aws::S3::Presigner.new (https://github.com/aws/aws-sdk-ruby/blob/master/aws-sdk-core/lib/aws-sdk-core/s3/presigner.rb) but it doesn't seem to take in an object parameter or auth credentials.

使用Aws::S3::Resource.new,但似乎将不会维护aws-sdk-resources. ( https://aws.amazon.com/blogs/developer/upgrading-from-version-2-to-version-3-of-the-aws-sdk-for-ruby-2/)

Using Aws::S3::Resource.new, but it seems like aws-sdk-resources is not going to be maintained. (https://aws.amazon.com/blogs/developer/upgrading-from-version-2-to-version-3-of-the-aws-sdk-for-ruby-2/)

使用Aws::S3::Object.new,然后在该对象上调用put方法.

Using Aws::S3::Object.new and then calling the put method on that object.

直接使用AWS::SigV4.

我想知道它们之间的区别,以及选择一个而不是另一个的含义?任何建议,尤其是aws-sdk-s3,都非常感谢.

I am wondering how they differ, and the implications of choosing one over the other? Any recommendations are much appreciated, especially with aws-sdk-s3.

谢谢!

推荐答案

因此,感谢上面@strognjz的提示,以下是使用"aws-sdk-s3"为我工作的内容.

So, thanks to the tips by @strognjz above, here is what worked for me using `aws-sdk-s3'.

require 'aws-sdk-s3'

#credentials below for the IAM user I am using
s3 = Aws::S3::Client.new(
  region:               'us-west-2', #or any other region
  access_key_id:        AWS_ACCESS_KEY_ID,
  secret_access_key:    AWS_SECRET_ACCESS_KEY
)

signer = Aws::S3::Presigner.new(client: s3)
url = signer.presigned_url(
  :put_object,
  bucket: S3_BUCKET_NAME,
  key: "${filename}-#{SecureRandom.uuid}"
)

这篇关于推荐的方法来在Ruby中生成S3存储桶的预签名URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆