gpt4 book ai didi

ruby-on-rails - 无法通过Rails Docker,Carrierwave将文件上传到AWS S3

转载 作者:行者123 更新时间:2023-12-02 20:04:06 26 4
gpt4 key购买 nike

我在Docker下运行的RAILS中有一个应用程序,我希望它使用Gem Carrierwave将文件上传到S3存储桶上

当我上传文件时,一切似乎都很好,但是文件从未出现在我的存储桶中。

我想念什么吗?也许是许可还是开放端口?

我的配置和其他人一样:

aws.rb

require 'aws-sdk'

Aws.config.update({
region: ENV['aws_region'],
credentials: Aws::Credentials.new(ENV['aws_access_key_id'],ENV['aws_secret_access_key'])
})

carrierwave.rb
CarrierWave.configure do |config|
config.storage = :aws
config.aws_bucket = ENV['aws_bucket']
config.aws_acl = 'public-read'


config.aws_authenticated_url_expiration = 60 * 60 * 24 * 7
config.aws_attributes = {
expires: 1.week.from_now.httpdate,
cache_control: 'max-age=604800'
}

config.aws_credentials = {
:region => ENV['aws_region'],
:force_path_style => true,
:signature_version => 'v4',
:credentials => Aws::Credentials.new(ENV['aws_access_key_id'],ENV['aws_secret_access_key']),
}

end

docker-compose.yml
appsnginx:
build:
context: .
dockerfile: Dockerfile-nginx
volumes:
- ./log:/apps/log
ports:
- "3000:80"
- "8443:443"
depends_on:
- apps
apps:
build: .
command: puma -C config/puma.rb
volumes:
- .:/apps
- ./tmp/uploads-cache:/apps/tmp/uploads
expose:
- "3000"

document_uploader.rb
class DocumentUploader < CarrierWave::Uploader::Base

storage :aws

def store_dir
"#{Rails.env}/#{model.class.to_s.underscore}/#{mounted_as}"
end

def default_url
"https://placehold.it/1920X1280"
end


def cache_dir
"#{Rails.root}/tmp/uploads-cache"
end


def extension_whitelist
%w(pdf doc htm html docx txt jpg jpeg png)
end

def filename
"#{File.basename(file.filename,".*")}__#{secure_token}.#{file.extension}" if original_filename.present?
end

protected
def secure_token
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
end

end

最佳答案

第一:
我向自己保证,可以访问“cache_dir”文件夹并将其设置为系统和docker之间的共享卷
我向自己保证,我的AWS Bucket文件夹已创建并可以从公众访问

问题似乎来自我的“secure_token”的“filename”和“DocumentUploader”方法

我不得不改变这个

"#{File.basename(file.filename,".*")}__#{secure_token}.#{file.extension}" if original_filename.present?

通过
"#{File.basename(original_filename,".*")}__#{secure_token}.#{file.extension}" if original_filename.present?

错误消息为

can't modify frozen NilClass model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)



我很难找到它,因为它基本上保持沉默。它没有显示在任何日志,输出或屏幕中。当我到处都放痕迹时,我在上面胡扯;)

关于ruby-on-rails - 无法通过Rails Docker,Carrierwave将文件上传到AWS S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51246637/

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