GithubHelp home page GithubHelp logo

wandergink / paperclip-googledrive Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evinsou/paperclip-googledrive

0.0 2.0 0.0 23 KB

extension for paperclip with google drive storage

License: MIT License

Ruby 100.00%

paperclip-googledrive's Introduction

PaperclipGoogledrive

Gem Version Code Climate

This gem currently is not supported, because of changes in google-api-ruby-client. There is working alternative for google-api-ruby-client with 0.8.0 version.

PaperclipGoogledrive is a gem that extends paperclip storage for Google Drive. Works with Rails 3.x.

Installation

Add this line to your application's Gemfile:

gem 'paperclip-googledrive'

And then execute:

$ bundle

Or install it yourself as:

$ gem install paperclip-googledrive

Google Drive Setup

Google Drive is a free service for file storage files. In order to use this storage you need a Google (or Google Apps) user which will own the files, and a Google API client.

  1. Go to the Google Developers console and create a new project.

  2. Go to "APIs & Auth > APIs" and enable "Drive API". If you are getting an "Access Not Configured" error while uploading files, this is due to this API not being enabled.

  3. Go to "APIs & Auth > Credentials" and create a new OAuth 2.0 Client ID; select "web application" type, specify http://localhost for application home page.

  4. Now you will have a Client ID, Client Secret, and Redirect URL.

  5. Run the authorization task:

    $ rake google_drive:authorize
    

    When you call this Rake task, it will ask you to provide the client id, client secret, redirect url and auth scope. Specify https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.profile for scope (more on Google Drive scopes).

  6. The Rake task will give you an auth url. Simply go to that url (while signed in as the designated uploads owner), authorize the app, then enter code from url in the console. The rake task will output valid ruby code which you can use to create a client, in particular, the access and refresh tokens.

  7. Create a folder in which the files will be uploaded; note the folder's ID.

Configuration

Example:

class Product < ActiveRecord::Base
 has_attached_file :photo,
    :storage => :google_drive,
    :google_drive_credentials => "#{Rails.root}/config/google_drive.yml"
end

The :google_drive_credentials option

This can be a hash or path to a YAML file containing the keys listed in the example below. These are obtained from your Google Drive app settings and the authorization Rake task.

Example config/google_drive.yml:

application_name: MyApp
application_version: 1.0.0
client_id: <%= ENV["CLIENT_ID"] %>
client_secret: <%= ENV["CLIENT_SECRET"] %>
access_token: <%= ENV["ACCESS_TOKEN"] %>
refresh_token: <%= ENV["REFRESH_TOKEN"] %>

It is good practice to not include the credentials directly in the YAML file. Instead you can set them in environment variables and embed them with ERB.

Options

The :google_drive_options option

This is a hash containing any of the following options:

  • :path โ€“ block, works similarly to Paperclip's :path option
  • :public_folder_id- id of folder that must be created in google drive and set public permissions on it
  • :default_image - an image in Public folder that used for attachments if attachment is not present

The :path option should be a block that returns a path that the uploaded file should be saved to. The block yields the attachment style and is executed in the scope of the model instance. For example:

class Product < ActiveRecord::Base
  has_attached_file :photo,
    :storage => :google_drive,
    :google_drive_credentials => "#{Rails.root}/config/google_drive.yml",
    :styles => { :medium => "300x300" },
    :google_drive_options => {
      :path => proc { |style| "#{style}_#{id}_#{photo.original_filename}" }
    }
end

For example, a new product is created with the ID of 14, and a some_photo.jpg as its photo. The following files would be saved to the Google Drive:

Public/14_some_photo.jpg Public/14_some_photo_medium.jpg

The another file is called some_photo_medium.jpg because style names (other than original) will always be appended to the filenames, for better management.

Misc

Useful links Google APIs console

Google Drive scopes

Enable the Drive API and SDK

Quickstart

License

MIT License

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

paperclip-googledrive's People

Contributors

evinsou avatar leonid-shevtsov avatar sman591 avatar ins429 avatar

Watchers

James Cloos avatar Wander Gink avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.