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

时间:2018-07-09 13:22:03

标签: ruby-on-rails docker amazon-s3 docker-compose carrierwave

我在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

1 个答案:

答案 0 :(得分:0)

第一: 我向自己保证,“ 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?

错误消息是

  

无法修改冻结的NilClass模型。instance_variable_get(var)或   model.instance_variable_set(var,SecureRandom.uuid)

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