ruby-on-rails-3papercliprackspace-cloudfogcloudfiles

Uploading to Rackspace Cloud Files with paperclip and fog


Can't figure out how to do this? and couldn't find much help from anywhere else!

I have set up the paperclip and fog like this;

config/initializers/fog.rb

     connection = Fog::Storage.new({
       :provider           => 'Rackspace',
       :rackspace_username => '',
       :rackspace_api_key  => ''
     })

environment.rb;

    Paperclip::Attachment.default_options.update({
    :path             => ":attachment/:id/:timestamp_:style.:extension",
    :storage          => :fog,
    :fog_credentials  => {
      :provider           => 'Rackspace',
      :rackspace_username => '',
      :rackspace_api_key  => '',
      :persistent         => false
    },
    :fog_directory    => '',
    :fog_public       => true

})

I am using file_field to get an image and then posting it to my controller. This gets me something like this in;

"pic"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007f90ac06a6c8 @original_filename="3245-1920x1200.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"cloth[pic][image]\"; filename=\"3245-1920x1200.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20130104-5386-103laem>>}

What I can't understand is that how do I go about actually saving this file to cloud files using something like this;

file = directory.files.create(
  :key    => 'resume.html',
  :body   => File.open("/path/to/my/resume.html"),
  :public => true
)

EDIT

Relevant Models;

class Cloth
  include Mongoid::Document
  has_many :pics

class Pic
  include Mongoid::Document
  include Mongoid::Paperclip
    belongs_to :cloth

    has_mongoid_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }

And in the controller this is how I am currently creating pic based on above params;

@cloth = Cloth.new
@cloth.pics.create!(params[:cloth][:pic])

Solution

  • Let's step back and look at the problem from different perspective. Can you see if the following script will upload an image to your container:

    require 'fog'
    service = Fog::Storage.new({
      :provider                 => 'Rackspace',
      :rackspace_username        => YOUR_USERNAME,
      :rackspace_api_key    => YOUR_API_KEY
      })
    
    container = service.directories.new(:key => YOUR_CONTAINER_NAME)
    container.files.create :key => 'my-pix.jpg', :body => File.open PATH_TO_FILE
    

    Update the uppercase parameters with the appropriate variables and let me know what happens. Hopefully this will help narrow down the problem.