gpt4 book ai didi

amazon-web-services - 如何在 heroku 上正确配置 Rails 5、paperclip 5、aws-sdk 2?

转载 作者:行者123 更新时间:2023-12-02 03:06:56 25 4
gpt4 key购买 nike

我在本地部署了带有回形针附件的 Rails 5 应用程序:一切正常。仅在 heroku 上我需要 AWS s3,它不适用于 Rails 5.0.1/paperclip 5.1.0/aws-sdk 2.7.3。

有人有在 s3 上存储附件的有效配置吗?

这是我的模型/article.rb:

class Article < ApplicationRecord
has_many :comments, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5 }
has_attached_file :attachment,
:url => ":s3_domain_url",
#:path => ':filename',
:storage => :s3,
:bucket => ENV['AWS_BUCKET'],
:s3_bucket => ENV['AWS_BUCKET'],
:s3_permissions => :private,
:s3_protocol => 'http',
:s3_host_name => 's3.amazonaws.com',
#:s3_host_alias => 's3.amazonaws.com',
:s3_region => ENV['AWS_REGION'],
#:region => ENV['AWS_REGION'],
:s3_credentials => { :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_KEY_ID'],
:endpoint => 's3.amazonaws.com' }

# Explicitly do not validate
do_not_validate_attachment_file_type :attachment
#validates_attachment :attachment, content_type: { content_type: 'application/x-java-archive'}, size: { in: 0..10.megabytes }
end

环境变量设置为:

AWS_ACCESS_KEY_ID=....
AWS_BUCKET=....
AWS_REGION=us-east-1
AWS_SECRET_KEY_ID=....

articles_controller 是:

class ArticlesController < ApplicationController

http_basic_authenticate_with name: "gerrit", password: "_tVo1I44iyLe", except: [:index, :show]

def index
@articles = Article.all
end

def show
@article = Article.find(params[:id])
end



def new
@article = Article.new
end

def edit
@article = Article.find(params[:id])
end

def create
@article = Article.new(article_params)

if @article.save
redirect_to @article
else
render 'new'
end
end

def update
@article = Article.find(params[:id])

if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end

def destroy
@article = Article.find(params[:id])
@article.destroy

redirect_to articles_path
end

private
def article_params
#article_params =
params.require(:article).permit(:title, :text, :attachment)
#@article.update_attributes( article_params )
end
end

这是我现在在本地和 heroku 上遇到的错误,之前还有很多其他错误:

未初始化常量 Paperclip::Storage::S3::AWS提取的源代码(大约第 26 行):

  @article = Article.new(article_params)

if @article.save #line 26
redirect_to @article
else
render 'new'

参数:

{"utf8"=>"✓", "authenticity_token"=>"snGgVwA5tqQTPHXwXW1Fx/lmLMS1bL+94jF68KQh031gpX2N78gOK45hCP8w71ObFo6moHQuJXxNnUW0bCUeVw==",
"article"=>
{"title"=>"test1",
"text"=>"",
"attachment"=>
#<ActionDispatch::Http::UploadedFile:0x007f46c00881e0
@content_type="application/x-desktop",
@headers="Content-Disposition: form-data; name=\"article[attachment]\"; filename=\"Antichamber.desktop\"\r\n" + "Content-Type: application/x-desktop\r\n",
@original_filename="Antichamber.desktop",
@tempfile=#<File:/tmp/RackMultipart20170306-5350-esbhtd.desktop>>},
"commit"=>"Create Article"}

为了方便,我在此处添加了损坏的 Rails 5 应用程序的 URL:

gmr-heroku

现在一切正常!

注意:您希望我提出不同的问题,而不是添加到原始问题中。现在您不想要重复的答案。

最佳答案

:url => :s3_domain_url 应该是 :url => ":s3_domain_url"

关于amazon-web-services - 如何在 heroku 上正确配置 Rails 5、paperclip 5、aws-sdk 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42087502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com