cloudflare-gdrive v1.0.1
cloudflare-gdrive
Leverage Google Drive as File Server (with direct download, fileupload, and urlupload feature) using Cloudflare Workers.
Setup
Get your
refresh token(using rclone).Follow Workers Get Started Guide, or use my template.
Install
cloudflare-gdrivepackage
yarn add cloudflare-gdrive- Use
createHandlerimport fromcloudflare-gdriveto create thehandler.
Options:
base: Only accept request prefixed with this string, default to '' (accept all requests).oauth: Putclient_id,client_secret, andrefresh_tokenyou got from step 1 here.requireAuth: Record of HTTP Methods (GETandPOST) mapped with Bearer Token to authorize the client (POSTis required,GEToptional).rootFolderId: ID of the Google Drive folder you wish to be atbasepath, default to 'root' (your My Drive folder).
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
Example
GET http://example.com/?listresponse:
[
{
"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"
}
]Example
Recurse folder once.
GET http://example.com/?list&recurse=1response:
[
{
"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"
}
]Example
List, but without folders.
GET http://example.com/?list&nofolderresponse:
[
{
"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.
Required form:
mode: upload mode, set withurlupload.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"Required form:
mode: upload mode, set withfileupload.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
pathfolder if not exists (including parent folders).
Getting refresh_token
Install rclone.
Create your own Google Drive client_id.
Create a Google Drive remote in rclone and fill in
client_idandclient_secretwith the one you made before.Copy the
refresh_tokenin 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
...