[英]Ruby on Rails - AWS-SDK configuration file
I'm using AWS-SDK gem in my Rails project, and I want a kind-of initializer file to connect directly to my repo and make changes directly in the Rails console, something like this: 我在我的Rails项目中使用AWS-SDK gem,我想要一种初始化文件直接连接到我的repo并直接在Rails控制台中进行更改,如下所示:
# At config/initializers/aws.rb
Aws::S3::Client.new(
:access_key_id => 'ACCESS_KEY_ID',
:secret_access_key => 'SECRET_ACCESS_KEY'
)
I've looked for documentation or tutorials but it's not clear for me. 我已经找了文档或教程,但对我来说并不清楚。 How do I do it? 我该怎么做? Thank you! 谢谢!
i think you can try like this 我想你可以这样试试
put this in aws.rb 把它放在aws.rb中
AWS.config(
:access_key_id => ENV['ACCESS_KEY_ID'],
:secret_access_key => ENV['SECRET_ACCESS_KEY']
)
and when you initialize the object wherever you need, will call the configuration 当您在任何需要的地方初始化对象时,将调用配置
s3 = AWS::S3.new
To share configuration between AWS service client in a Rails application, configure the AWS SDK for Ruby from a config initializer. 要在Rails应用程序中的AWS服务客户端之间共享配置,请从配置初始化程序配置AWS SDK for Ruby。
# config/initializers/aws-sdk.rb
Aws.config.update(
credentials: Aws::Credentials.new('access-key-id', 'secret-access-key'),
region: 'us-east-1',
)
Now you can construct a client object from any service without any options: 现在,您可以从任何服务构造客户端对象,而无需任何选项:
s3 = Aws::S3::Client.new
ec2 = Aws::EC2::Client.new
Please note, you should avoid hard-coding credentials into your application. 请注意,您应该避免将凭据硬编码到您的应用程序中。 This can be a security risk if your source code is accessed and it makes it difficult to rotate credentials. 如果访问源代码并且难以轮换凭据,则可能存在安全风险。
I recommend using hands-off configuration via ENV['AWS_ACCESS_KEY_ID']
and ENV['AWS_SECRET_ACCESS_KEY']
, or an EC2 instance profile. 我建议通过ENV['AWS_ACCESS_KEY_ID']
和ENV['AWS_SECRET_ACCESS_KEY']
或EC2实例配置文件使用ENV['AWS_SECRET_ACCESS_KEY']
配置。
Finally, I've found the solution: 最后,我找到了解决方案:
aws.rb
in your /config/initializers
folder. 在/config/initializers
文件夹中创建文件aws.rb
In aws.rb
write: 在aws.rb
写道:
S3Client = Aws::S3::Client.new( access_key_id: 'ACCESS_KEY_ID', secret_access_key: 'SECRET_ACCESS_KEY', region: 'REGION' )
That's it. 而已。 Thank you all for your answers! 谢谢大家的答案!
Also with aws-sdk-rails (1.0.0) 还有aws-sdk-rails(1.0.0)
# config/initializers/aws.rb
Aws.config[:credentials] = Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
TRY THIS CONFIGURATION:- 尝试这个配置: -
in config/intitalizers/s3.rb 在config / intitalizers / s3.rb中
Paperclip.interpolates(:s3_eu_url) { |attachment, style|
"#{attachment.s3_protocol}://s3-eu-west-1.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
}
config/initializers/paperclip.rb 配置/初始化/ paperclip.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:id/:style/:filename'
S3_CREDENTIALS = Rails.root.join("config/s3.yml")
/config/s3.yml /config/s3.yml
development:
bucket: development_bucket
access_key_id: AKIA-----API KEYS---------MCLXQ
secret_access_key: qTNF1-------API KEYS--------DTy+rPubaaG
production:
bucket: production_bucket
access_key_id: AKI-----API KEYS--------LXQ
secret_access_key: qTNF1dW---API KEYS---+rPubaaG
hope you have gem "aws-sdk"
in the Gemfile
希望你在Gemfile
有gem "aws-sdk"
add you asset in model 在模型中添加资产
has_attached_file :avatar,
:styles => {:view => "187x260#"},
:storage => :s3,
:s3_permissions => :private,
:s3_credentials => S3_CREDENTIALS
verify using rails console with static image in public 公共使用带有静态图像的rails控制台进行验证
Image.create(avatar: File.new("#{Rails.root}/public/images/colorful_blue.jpg"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.