我正在尝试使用回形针使用AmazonS3为我的博客存储图像。该博客设计了用于登录/注册[User]的gem,我想通过将每个用户存储在Amazon S3上来添加图像。以前,我将它们存储在数据库中,并且运行良好。
我已经搜索了很多,但是找不到解决方法。
//Development.rb
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
//AddAttachment Migration
class AddAttachmentImageToUsers < ActiveRecord::Migration[5.2]
def self.up
change_table :users do |t|
t.attachment :image
end
end
def self.down
remove_attachment :users, :image
end
end
//User.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :comments, dependent: :destroy
has_many :likes, dependent: :destroy
has_many :articles
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
//Application.yml
AWS_ACCESS_KEY_ID: -----
AWS_SECRET_ACCESS_KEY: ------
S3_BUCKET_NAME: -------------
AWS_REGION: ap-south-1
//Application.html.erb
<!DOCTYPE html>
<html>
<head>
<title> TheBlog</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#" id="logo">The Blog</a>
</div>
<ul class="nav navbar-nav">
<li class="active">
<%= link_to 'Home', root_path %></li>
<li><%= link_to 'About', about_path %></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<% if user_signed_in? %>
<li> <%= image_tag current_user.image.url, size:"40x40" %></li>
<li><%= link_to current_user.email, edit_user_registration_path %></li>
<li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
<% else %>
<li><%= link_to "Sign Up", new_user_registration_path %></li>
<li><%= link_to "Login", new_user_session_path %></li>
<% end %>
</ul>
</div>
</nav>
<%= yield %>
</body>
</html>
答案 0 :(得分:0)
与以下问题类似:https://railsblogs.rohityadav.in/2018/01/awss3errorsaccessdenied-access-denied.html
如果在尝试上传到S3时遇到此错误,则需要为此IAM用户分配“ AmazonS3FullAccess”策略。
或者可能是heroku配置出现问题:
AWS::S3::Errors::AccessDenied. Cannot save to S3 with Ruby on Rails
您需要将AWS凭证设置为heroku中的配置变量(基本上是环境变量)。您可以检查是否已使用heroku config设置它们;如果未设置,则可以使用heroku config添加它们:set S3_BUCKET_NAME = AWS_ACCESS_KEY_ID =任何AWS_SECRET_ACCESS_KEY = something_secret。 heroku config --help
的更多信息