0

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])
Shakeeb Ahmad
  • 2,010
  • 1
  • 25
  • 30

2 Answers2

3

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.

Anatoly
  • 15,298
  • 5
  • 53
  • 77
Kyle Rames
  • 398
  • 2
  • 5
  • 1
    GOT IT! My container's region was 'Chicago', deleted that, made a new container at 'Dallas' and everything worked fine. Apparently: https://github.com/fog/fog/issues/1135 no one has fixed this yet. Phew, nearly drove me mad, this one. Thanks man! – Shakeeb Ahmad Jan 08 '13 at 17:47
  • Glad hear it! Just a tip, if you are using Cloud Servers with Cloud Files, you should use the same data center to reduce your costs. – Kyle Rames Jan 08 '13 at 18:22
  • Accepting this as the answer because led me to think about regions as it couldn't find the directory! – Shakeeb Ahmad Jan 10 '13 at 00:31
  • The latest version of Fog supports specifying the region of the Storage service. For more information please refer to https://github.com/fog/fog/blob/master/lib/fog/rackspace/docs/storage.md#create-service – Kyle Rames Apr 05 '13 at 15:17
  • **service.directories.get** it is very slow because it returns redundant details: bytes, count. How can I speed it up? – Anatoly Feb 26 '14 at 16:10
2

Paperclip and ActiveRecord should automatically handle the file upload for you. Here is a good quick start explaining the process:

https://github.com/thoughtbot/paperclip#quick-start

If you are still having issues, can you provide me with the relevant controller and model code?

Kyle Rames
  • 398
  • 2
  • 5
  • 1
    updated code above - and no active record and I'm using `'mongoid-paperclip'` gem – Shakeeb Ahmad Jan 04 '13 at 17:49
  • Are you getting any exceptions when you execute the controller code? Is it storing anything in Mongo? – Kyle Rames Jan 04 '13 at 19:08
  • No, exceptions. It is storing this in mongo: `[#]` but not uploading to cloud files! – Shakeeb Ahmad Jan 05 '13 at 09:13
  • OK! now it is throwing this exception `Fog::Storage::Rackspace::NotFound in ClothController#create` and saving images to public/system but not uploading them – Shakeeb Ahmad Jan 07 '13 at 12:09
  • Ah! Looks like you are making some progress. It looks like the fog_directory setting should correspond to the container the files should be stored in. Do you have this value set to the name of the container you are using? – Kyle Rames Jan 07 '13 at 14:16
  • Yes! fog_directory, rackspace_api_key and rackspace_username are all correct, verified a thousand times – Shakeeb Ahmad Jan 07 '13 at 18:11