Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cloudflare-gdrive

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cloudflare-gdrive

Leverage Google Drive as File Server (with direct download, fileupload, and urlupload feature) using Cloudflare Workers

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

cloudflare-gdrive

Leverage Google Drive as File Server (with direct download, fileupload, and urlupload feature) using Cloudflare Workers.

Setup

  1. Get your refresh token (using rclone).

  2. Follow Workers Get Started Guide, or use my template.

  3. Install cloudflare-gdrive package

yarn add cloudflare-gdrive
  1. Use createHandler import from cloudflare-gdrive to create the handler.

Options:

  • base: Only accept request prefixed with this string, default to '' (accept all requests).
  • oauth: Put client_id, client_secret, and refresh_token you got from step 1 here.
  • requireAuth: Record of HTTP Methods (GET and POST) mapped with Bearer Token to authorize the client (POST is required, GET optional).
  • rootFolderId: ID of the Google Drive folder you wish to be at base path, default to 'root' (your My Drive folder).
Example
import { createHandler } from 'cloudflare-gdrive'

const fetchHandler = () => {
	return createHandler({
		base: '/api',
		oauth: {
			clientId: YOUR_CLIENT_ID,
			clientSecret: YOUR_CLIENT_SECRET,
			refreshToken: YOUR_REFRESH_TOKEN,
		},
		requireAuth: {
			GET: 'tokenhere',
			POST: 'veryhardbearertoken',
		},
		rootFolderId:
			'0B8VJ-gRi4t_9fnZzWGZHMzNBSG9lR1JlRGxwMGVZWUlONzdBeVB3dnRPTDgyQUJwT3RpMVU',
	})
}

export default {
	fetch: fetchHandler,
}

Usage

GET

GET-ting is pretty straight forward. You can fetch as you would like while using ftp, the path will be mapped to the correct file.


GET http://example.com/path/to/file/

Search query
"list", list the files instead of downloading them.
Example
GET http://example.com/?list

response:

[
	{
		"mimeType": "application/vnd.google-apps.folder",
		"path": "/test/",
		"url": "http://example.com/test/",
		"id": "1JFE64puRxwB3MdasFrumhTFYcFxJiN4Z"
	},
	{
		"mimeType": "text/plain",
		"path": "/text.txt",
		"url": "http://example.com/text.txt",
		"id": "13FmU4rGY2j5NLmzW0cmxetlHvYpF0eET",
		"size": "18848"
	}
]
"recursive", set with number to recurse for a specified time or truthy value to recurse indefinitely. Only recognized when "list" is specified.
Example

Recurse folder once.

GET http://example.com/?list&recurse=1

response:

[
	{
		"mimeType": "application/vnd.google-apps.folder",
		"path": "/test/",
		"url": "http://example.com/test/",
		"id": "1JFE64puRxwB3MdasFrumhTFYcFxJiN4Z"
	},
	{
		"mimeType": "text/plain",
		"path": "/test/no.txt",
		"url": "http://example.com/test/no.txt",
		"id": "1--ZX0dbRcpw1JItDMIKNIBl9Ej1g12sG",
		"size": "28592"
	},
	{
		"mimeType": "text/plain",
		"path": "/text.txt",
		"url": "http://example.com/text.txt",
		"id": "13FmU4rGY2j5NLmzW0cmxetlHvYpF0eET",
		"size": "18848"
	}
]
"nofile" or "nofolder", hide file or folder respectively. Only recognized when "list" is specified.
Example

List, but without folders.

GET http://example.com/?list&nofolder

response:

[
	{
		"mimeType": "text/plain",
		"path": "/text.txt",
		"url": "http://example.com/text.txt",
		"id": "13FmU4rGY2j5NLmzW0cmxetlHvYpF0eET",
		"size": "18848"
	}
]

POST

Post is used to upload files using multipart/form-data, either from URL or from your machine. Examples below will be using cURL.

Upload from URL

Required form:

  • mode: upload mode, set with urlupload.
  • url: url of the content you wish to upload.

Optional form:

  • path: folder path to save the content (default to /)
  • filename: saved filename (default to url's last segment)
Example

Upload file from https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg to folder /private/cat with name cat.jpeg.

curl --request POST http://example.com \
  -F "mode=urlupload" \
  -F "url=https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg" \
  -F "path=/private/cat" \
  -F "filename=cat.jpeg"
Upload from local file

Required form:

  • mode: upload mode, set with fileupload.
  • file: the file.

Optional form:

  • path: folder path to save the content (default to /)
  • filename: saved filename (default to file's filename)
Example

Upload mycat.jpeg to folder /private/cat with name cat.jpeg.

curl --request POST http://example.com \
  -F "mode=fileupload" \
  -F "file=@mycat.jpeg" \
  -F "path=/private/cat" \
  -F "filename=cat.jpeg"
Search query
  • "create", create path folder if not exists (including parent folders).

Getting refresh_token

  1. Install rclone.

  2. Create your own Google Drive client_id.

  3. Create a Google Drive remote in rclone and fill in client_id and client_secret with the one you made before.

  4. Copy the refresh_token in this step (it's the last step).

...
[remote]

client_id =
client_secret =
scope = drive
root_folder_id =
service_account_file =
token = {"access_token":"XXX","token_type":"Bearer","refresh_token":"XXX","expiry":"2014-03-16T13:57:58.955387075Z"}
---

y) Yes this is OK
e) Edit this remote
d) Delete this remote
...

FAQs

Package last updated on 15 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc