无法通过 Rails Docker、Carrierwave 将文件上传到 AWS S3

Can't Upload File throught Rails Docker, Carrierwave to AWS S3

我在 RAILS 运行 下的 Docker 中有一个应用程序,我希望它使用 Gem Carrierwave

将文档上传到 S3 Bucket

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

我错过了什么吗?也许是许可或开放端口?

我的配置是:

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 文件夹已创建并可从 public

问题似乎来自我的“DocumentUploader

的“secure_token”和“filename”方法

我不得不改变这个

"#{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)

我很难找到它,因为它基本上是无声的。它没有出现在任何日志、输出或屏幕中。当我到处留下痕迹时,我会在上面留茬 ;)