Breadbox
A simple wrapper interface for uploading files to Dropbox or Amazon S3
Disclaimer
- This is a simple and fast implementation - Issues and PRs welcome.
- Currently tested on Ruby 2.1.2
Installation
Add this line to your application's Gemfile:
gem 'breadbox'
And then execute:
$ bundle
Or install it yourself as:
$ gem install breadbox
Setup for Dropbox
1. Get a Dropbox Access Token
2. Add to your initializers:
# config/initializers/breadbox.rb
Breadbox.configure do |config|
config.dropbox_access_token = xxxxxxx # THIS IS REQUIRED
config.provider = :dropbox # THIS IS REQUIRED
end
Setup for S3
1. Get your AWS Credentials
3. Add to your initializers
# config/initializers/breadbox.rb
Breadbox.configure do |config|
config.s3_region = xxxxxxxx # OPTIONAL - defaults to "us-east-1"
config.s3_bucket = "name of the bucket" # THIS IS REQUIRED
config.s3_secret_access_key = xxxxxx # THIS IS REQUIRED
config.s3_access_key_id = xxxxxxx # THIS IS REQUIRED
config.provider = :s3 # THIS IS REQUIRED
end
(Optional) Configure your root directory for uploading files
By default - the root path will be the root directory of your [DropBox folder or S3 Bucket]. You can, however, change the root path to be anything you want.
Note: You have to prefix the folder you want with a /
, ex: /uploads/my-files
# config/initializers/breadbox.rb
Breadbox.configure do |config|
config.root_path = "/uploads/my-files"
# ... more configurations ...
end
Usage
Parameters:
-
path
: defaults tonil
, but this is where you put a custom folder if you so wish (in relation to yourroot_path
, which if you didn't configure in your initializer, will be your root Dropbox folder/
. -
filename
: defaults to the name of the file you are uploading, but you can specify a custom name here. -
file
: The file object that you are uploading, ex: `file = File.open('./path-to-local-file'). -
cleanup
: defaults tofalse
, but if you passtrue
- it will remove the local file after uploading. -
public
: defaults tofalse
. Passtrue
if you'd like to set the file permission level to world readable. -
content_type
: S3 knows how to handle most file types, but occasionally, you may need to specify your own
# to upload a file to [Dropbox Folder or S3 Bucket]/uploads/my-cool-file.jpg
# and remove it after upload
file = File.open("./tmp/my-cool-file.jpg")
Breadbox.upload(path: "uploads", file: file, cleanup: true)
Running Tests
Just run rake
in the project root.
Console
For a REPL console, run rake console
in the project root.
Contributing
- Fork it ( https://github.com/[my-github-username]/breadbox/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request