【问题标题】:amazon s3 variables not working in heroku environment rails亚马逊 s3 变量在 heroku 环境中不起作用
【发布时间】:2025-12-09 03:50:02
【问题描述】:

你好,我已经包含了给定的代码

def store_s3(file)
    # We create a connection with amazon S3
    AWS.config(access_key_id: ENV['S3_ACCESS_KEY'], secret_access_key: ENV['S3_SECRET'])
    s3 = AWS::S3.new
    bucket = s3.buckets[ENV['S3_BUCKET_LABELS']]
    object = bucket.objects[File.basename(file)]
    # the file is not the content of the file is the route
    # file_data = File.open(file, 'rb')
    object.write(file: file)
    # save the file and return an url to download it
    object.url_for(:read, response_content_type: 'text/csv')
  end

此代码在我的本地数据中正常工作存储在亚马逊中,但是当我在 heroku 服务器中部署代码时,我也在服务器上创建了变量。

这里有什么我遗漏的地方,请告诉我问题的原因。

【问题讨论】:

  • I had made variables on server too 怎么样?
  • 我在任何地方都没有看到S3_BUCKET_LABELS
  • 检查 heroku 日志中的确切错误

标签: ruby-on-rails amazon-web-services heroku amazon-s3


【解决方案1】:

我没有看到区域,在您的示例中是 S3_Hostname 您的区域吗? 对我来说,区域就像“us-west-2”。

【讨论】:

    【解决方案2】:

    如果你想用carrierwave和gem fog设置你的s3,你可以在config/initializers/carrierwave.rb上这样做

    CarrierWave.configure do |config|
      config.fog_provider = 'fog/aws'
      config.fog_directory = 'name for s3 directory'
    
      config.fog_credentials = {
        :provider => 'AWS',
        :aws_access_key_id => 'your access key',
        :aws_secret_access_key => 'your secret key',
        :region => 'your region ex: eu-west-2'
      }
    end
    

    【讨论】: