Uploading files to Linode using CarrierWave

2019-09-05 21:49发布

问题:

My app is hosted on Heroku and I am wondering if its possible to upload a file not to the public/ folder on Heroku but directly to - say - Linode

My app uses both Heroku and Linode. The two talk to each other via web-service requests. But generally speaking, I try to store any generated/uploaded file on Linode only - and nothing on Heroku

Now I have a situation where I want to allow users to upload files. The final destination would be Linode. But currently, I am having to go from Client PC -> Heroku -> Linode. The optimal solution would, however, be Client PC -> Linode

I thought I would ask before I attempt to re-wire existing code. Would changing storage_dir method as follows do the trick?

def storage_dir
  return http://<linode>/<local-folder>
end

Thanks for your help Abhinav

回答1:

So, you're using CarrierWave? I'm not sure how you're currently uploading to Linode, but I'll take a stab.

So, according to CarrierWave's documentation on GitHub, you need to add something like this to perhaps a carrierwave.rb file in your config/initializers:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',          # required
    :aws_access_key_id      => 'xxx',          # required
    :aws_secret_access_key  => 'yyy',          # required
  }
  config.fog_directory  = 'name_of_directory'  # required
end 

Of course, that's if you're using Amazon AWS S3. I found some fog documentation for linode (& other storage services) at http://ruby-doc.org/gems/docs/p/phpfog-fog-0.4.1.2/Fog/Linode/Compute/Mock.html If you click on 'new(options={}),' you will see @linode_api_key = options[:linode_api_key]. So, I think you'll be able to use CarrierWave to upload directly to Linode with:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'Linode',       
    :linode_api_key         => 'xxx',          
  }
  config.fog_directory  = 'name_of_directory'  
end