Paperclip Dropbox
This gem extends Paperclip with Dropbox storage.
Setup
gem "paperclip-dropbox", ">= 1.1.7"
Example:
class User < ActiveRecord::Base
has_attached_file :avatar,
:storage => :dropbox,
:dropbox_credentials => Rails.root.join("config/dropbox.yml"),
:dropbox_options => {...}
end
Your config/dropbox.yml
:
app_key: "..."
app_secret: "..."
access_token: "..."
access_token_secret: "..."
user_id: "..."
access_type: "dropbox|app_folder"
You can also pass a Proc
to :dropbox_credentials
, if you want the
credentials to be dynamically evaluated.
In order to fill these in, you must create a Dropbox app
and authorize it. There are two types of Dropbox apps: App folder or Full Dropbox. You can read
about the differences and gotchas in this wiki.
When you decide, don't forget to put "dropbox"
or "app_folder"
as the :access_type
in your credentials.
After you have created an app, you will be given the "App key" and "App secret". Provide these and the access type to the authorization Rake task:
$ rake dropbox:authorize APP_KEY=your_app_key APP_SECRET=your_app_secret ACCESS_TYPE=dropbox|app_folder
For non-Rails projects, you must require this task in your Rakefile
:
# Rakefile
load "paperclip/dropbox/tasks.rake"
Follow the instructions, and it will authorize the Dropbox app and output the rest of the credentials.
Then you can fill in the rest of config/dropbox.yml
.
And that's it. Everything should work now :)
:dropbox_credentials
option
The :dropbox_credentials => Rails.root.join("config/dropbox.yml")
# or
:dropbox_credentials => {app_key: "foo", app_secret: "bar", ...}
For the list of required credentials, take a look at the config/dropbox.yml
above.
The YAML file supports ERB:
app_key: <%= ENV["DROPBOX_APP_KEY"] %>
And it supports environment nesting (just like database.yml
):
development:
app_key: "..."
...
production:
app_key: "..."
...
:dropbox_options
option
The This is a hash containing any of the following options:
:environment
– String, the environment name to use for selecting namespaced credentials in a non-Rails app
For example:
class User < ActiveRecord::Base
has_attached_file :avatar,
:storage => :dropbox,
:dropbox_credentials => Rails.root.join("config/dropbox.yml"),
:dropbox_options => {environment: ENV["RACK_ENV"]}
end
In Rails you don't need to specify it.
:dropbox_visibility
option
The This is a string "public" (default) or "private".
- "public" - Files will be placed in your "Public" directory in dropbox and will the public urls will be used to access the files (not http request required to get a url for the file)
- "private" - Files can be placed in any dropbox directory (set your :path attachment option) and private, expiring urls (4 hours) will be generated each time you access this file. Private can only be used with the 'dropbox' access type.
class User < ActiveRecord::Base
has_attached_file :avatar,
:storage => :dropbox,
:dropbox_credentials => Rails.root.join("config/dropbox.yml"),
:dropbox_visibility => 'public'
end
:path
option
The To change the path of the file, use Paperclip's :path
option:
:path => ":style/:id_:filename" # Defaults to ":filename"
In "Full Dropbox" mode with "public" visibility it will automatically be searched in the Public
folder,
so there is not need to specify it.
URL options
If you want to force the browser to always download the file, you can use
the :download
option.
user.avatar.url(:download => true)
Check if the file exists
It may happen that a file on Dropbox gets deleted remotely. To check this in
your application, you can use the #exists?
method:
user.avatar.exists?
# user.avatar.exists?(style)