0.0
No release in over a year
百度网盘文件上传客户端Ruby版
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.6.0
~> 2.8.1
~> 0.14.2
~> 13.0.6
~> 3.12.0
~> 0.21.2
~> 6.1.0
~> 3.18.1

Runtime

~> 2.1.0
 Project Readme

Baidu Netdisk

Gem Version CI codecov Maintainability

A Ruby client to upload files to Baidu NetDisk (PCS). It'll auto-split big files and upload slices parallelly.

Installation

Install with bundler:

bundle add baidu-netdisk

Or install with ruby gem

gem install baidu-netdisk

Configuration

BaiduNetDisk.config do |c|
  # Required configs
  c.app_id     = 'your_app_id'
  c.app_key    = 'your_app_key'
  c.secret_key = 'your_secret_key'

  # Max threads for uploading file slices, default to 1
  c.max_uploading_threads = 4

  # The following two are optional;
  # Fill them in if you want to explicitly indicate uploading to someone else's storage space
  c.access_token  = 'your_access_token'
  c.refresh_token = 'your_refresh_token'
end

Usage

This client is designed for the two following scenarios.

Scenario 1: Using Baidu NetDisk as a backup file storage

You always save files to your net disk storage. You don't need a callback to receive auth codes or tokens from Baidu; Instead, you can just get them from your Ruby console.

require 'baidu-netdisk'

# It opens a browser to ask for access to Baidu OAuth;
# you will get your auth code after you log in to your Baidu NetDisk
BaiduNetDisk::Auth.get_auth_code

# Pass the code as the argument your received from the previous step
BaiduNetDisk::Auth.get_token(auth_code)

uploader = BaiduNetDisk::Uploader.new(source_path, target_path)
uploader.execute

#  => {"category"=>6, "ctime"=>1676019860, "from_type"=>1, "fs_id"=>121127634951625, "isdir"=>0, "md5"=>"79835de6btc0b3482f51b49088c8ccfb", "mtime"=>1676019860, "path"=>"<target_path>", "server_filename"=>"<file_name>", "size"=>76267, "errno"=>0, "name"=>"<target_path>"} 

Scenario 2: Upload files to your clients' Baidu NetDisk

You should have a web server and need to implement your webhook to receive a callback from Baidu NetDisk after auth.

Say your callback URL is https://www.example.com/webhook, you should have the:

BaiduNetDisk::Auth.get_auth_code('https://www.example.com/webhook')


# Auth code is fetched in your callbacks
BaiduNetDisk::Auth.get_token(auth_code, 'https://www.example.com/webhook')

uploader = BaiduNetDisk::Uploader.new(source_path, target_path,
  { access_token: 'client.access_token', refresh_token: 'client.refresh_token' })

uploader.execute

It provides a hook after token refreshed so you can save tokens after refreshing.

BaiduNetDisk.config do |c|
  c.after_token_refreshed = -> (access_token, refresh_token) {
    user.update access_token: access_token, refresh_token: refresh_token
  }
end

Development

Copy the env file

It reads ENV from .env for testing.

cp .env.example .env

Run tests

bundle install
bundle exec rake

Todo

  1. Improve MAINTAINABILITY

  2. To use other HTTP clients instead of RestClient:

  3. RestClient can't catch response body when response is 400 BadRequest.

  4. Currently, we implemented multi-threaded uploading by hands. Consider to use typhoeus with its native hydra to run HTTP requests in parallel.