GithubHelp home page GithubHelp logo

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 ins429 avatar leonid-shevtsov avatar sman591 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

paperclip-googledrive's Issues

Cannot load storage module 'GoogleDrive'

Hi

I get this error when trying to use the Google Drive storage. Here is a sample stack trace. The error is in the following line:

def create
@store = Store.new(store_params)
...

paperclip (3.5.1) lib/paperclip/attachment.rb:388:in rescue in initialize_storage' paperclip (3.5.1) lib/paperclip/attachment.rb:385:ininitialize_storage'
paperclip (3.5.1) lib/paperclip/attachment.rb:85:in initialize' paperclip (3.5.1) lib/paperclip/has_attached_file.rb:46:innew'
paperclip (3.5.1) lib/paperclip/has_attached_file.rb:46:in block in define_instance_getter' paperclip (3.5.1) lib/paperclip/has_attached_file.rb:65:inblock in define_setter'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in public_send' activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in_assign_attribute'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:29:in block in assign_attributes' activerecord (4.0.0) lib/active_record/attribute_assignment.rb:23:ineach'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:23:in assign_attributes' activerecord (4.0.0) lib/active_record/core.rb:192:ininitialize'
activerecord (4.0.0) lib/active_record/inheritance.rb:27:in new' activerecord (4.0.0) lib/active_record/inheritance.rb:27:innew'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in send_action' actionpack (4.0.0) lib/abstract_controller/base.rb:189:inprocess_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in process_action' actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:inblock in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:413:in `_run__948680893__process_action__callbacks'
...

Google::APIClient::ClientError (Insufficient Permission)

Thank you for creating this gem.

I am trying to use this gem in one of my Rails app. I have a simple upload form. When I try to upload a file I get Google::APIClient::ClientError (Insufficient Permission) error.

For this I have created an app in google API console, enabled both Drive API and Drive SDK services and have created a OAuth 2.0 client for web applications. I got all the credentials correctly by running 'rake google_drive:authorize'. provided full access scope https://www.googleapis.com/auth/drive.

Can you please put some insight on this error I am getting?

Thank you.

KeyError - you must set a Public folder if into options

Hi, how do you define public folder?

has_attached_file :cover,
  storage: :google_drive,
  google_drive_credentials: "#{Rails.root}/config/google_drive.yml",
  styles: { thumb: "160x120>" }, default_url: "/images/:style/missing.png",
  google_drive_options: {
    folder_id: 'BD'
   }

I have a folder 'BD' set to public in my Drive.

Googledrive-paperclip

Signet::AuthorizationError: Authorization failed. Server message:
{
"error" : "invalid_grant"
}
/home/nyros/.rvm/gems/ruby-2.2.1/gems/signet-0.6.1/lib/signet/oauth_2/client.rb:966:in fetch_access_token' /home/nyros/.rvm/gems/ruby-2.2.1/gems/signet-0.6.1/lib/signet/oauth_2/client.rb:983:infetch_access_token!'
/home/nyros/.rvm/gems/ruby-2.2.1/gems/paperclip-googledrive-0.1.3/lib/paperclip/google_drive/rake.rb:39:in authorize' /home/nyros/.rvm/gems/ruby-2.2.1/gems/paperclip-googledrive-0.1.3/lib/paperclip/google_drive/tasks.rake:7:inblock (2 levels) in <top (required)>'
/home/nyros/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in eval' /home/nyros/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in

'
Tasks: TOP => google_drive:authorize
(See full trace by running task with --trace)

undefined method `call' for "result == false":String

I am kinda running in circles around here. I had this gem:

gem "paperclip", git: "git://github.com/thoughtbot/paperclip.git"

But when I added gem 'paperclip-googledrive' I got an error: paperclip-googledrive (>= 0) ruby depends on paperclip (~> 3.4) ruby

I solved this error by removing the git part from paperclip gem and making a new bundle install with gem "paperclip" only, but now after following paperclip-googledrive tutorial on create method I have got this error:

NoMethodError in VideosController#create 
undefined method `call' for "result == false":String

I've been reading around the internet that is may be due to incompatibilies with that paperclip version and rails 4.2.4 so it can be a really big problem. Anyone know how to fix this?

This is my model btw:

class Video < ActiveRecord::Base
  belongs_to :channel
  has_many :video_tags
  letsrate_rateable "rate_video"
  has_attached_file :videofile, styles: {
      :medium => {
          :geometry => "640x480",
          :format => 'mp4'
      },
      :thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
  }, :processors => [:transcoder], :storage => :google_drive, :google_drive_credentials => "#{Rails.root}/config/google_drive.yml"

  validates_attachment_content_type :videofile, content_type: /\Avideo\/.*\Z/

Can't download semi large Images

Hello!

I'm using this gem to upload images to a google Drive account, good stuff!!

Sadly I found that I can't upload images higher than 8 or 10 kb.. not MB.. images higher than that will upload to the server but when I try to get the url it will return "No Picture"

I tried getting the url via rails console and sometimes It gets it, sometimes it doesnt.

How can I fix this?

Google API Client 0.9.x has breaking changes

The 0.9 release of google-api-client has breaking changes, so the paperclip-googledrive gemspec needs to either be updated to support the breaking changes or lock the google-api-client gem version at < 0.9

Workaround for those encountering this is to manually add gem 'google-api-client', '< 0.9' to your app's Gemfile and run bundle update google-api-client

I can't solve url

Good day,

I can't solve url for my model on Rails 3.2.

Model:
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title ,:media
if Rails.env.development?
has_attached_file :media,
:styles=> {original: '800x600'},
:storage => :google_drive,
:google_drive_credentials => "#{Rails.root}/config/google_drive.yml",
:google_drive_options => {
:path => proc { |style| "#{style}#{id}#{media.original_filename}" },
:public_folder_id=> "0Bx-14dUHOQScVjlmSExDd0NueUE"
}
end
end

Next i try solve url:
Post.last.media.url(:original)
Post Load (0.4ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT 1
=> "No picture"

What i doing wrong? because files upload correctly and public folder have permission for read and write for every body.

Thank you!

Missing access token

I use rails 4.2, i have created a project on google APIs and i have followed instructions I have runing rake google_drive:authorize and i entred the required informations and i have got an access token

my config/google_drive.yml:

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

So when i click on upload file button, it show error says: Missing access token , i have copied access token code from the console and paste it to config/google_drive.yml in the third line in place of <%= ENV["ACCESS_TOKEN"] %>, that's work well and the file was uploaded successfuly, but after 1 hour i have tried the operation but it was failed, and i think that the access token haven't been refreshed, so what to do to refresh it automaticly ?

the case of refresh_token is empty after running rake google_drive:authorize i don't know why!

client = Google::APIClient.new
client.authorization.client_id = '370811699552-phao5na0eh0cn6magsqua1rnfnmbffdn.apps.googleusercontent.com'
client.authorization.client_secret = 'IDCh534rpOzpuLhZi7e7TYrC'
client.authorization.access_token = 'ya29.Ci9KAy0Ny5xbT0oUkHWr18_SEIwKGNPe2w2bZ3VYcdGgoEb6zTvzHouV2CYjnnscEA'
client.authorization.refresh_token = ' '

Gem release

Can you please release a new version to Rubygems.org?

The current highest version there requires Paperlcip ~> 3.4, though 4.X is out

This is fixed on master, by doing '=> 3.4' rather than than the pessimistic operator.

I'm getting around this by doing gem "paperclip-googledrive", github: "evinsou/paperclip-googledrive", but a proper gem release would be nice :)

Thanks!
Sean

No Redirect URL

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.

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

I followed all the steps but I didn't got any Redirect URL in the end. Here's how it looks like.

google

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.