Leverage Google Drive as File Server (with direct download, fileupload, and urlupload feature) using Cloudflare Workers.
-
Get your
refresh token
(using rclone). -
Follow Workers Get Started Guide, or use my template.
-
Install
cloudflare-gdrive
package
yarn add cloudflare-gdrive
- Use
createHandler
import fromcloudflare-gdrive
to create thehandler
.
Options:
base
: Only accept request prefixed with this string, default to '' (accept all requests).oauth
: Putclient_id
,client_secret
, andrefresh_token
you got from step 1 here.requireAuth
: Record of HTTP Methods (GET
andPOST
) mapped with Bearer Token to authorize the client (POST
is required,GET
optional).rootFolderId
: ID of the Google Drive folder you wish to be atbase
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,
}
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/
"list", list the files instead of downloading them.
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.
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.
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 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 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)
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 withfileupload
.file
: the file.Optional form:
path
: folder path to save the content (default to/
)filename
: saved filename (default to file's filename)
Upload mycat.jpeg
to folder /private/cat
with name cat.jpeg
.
curl --request POST http://example.com \
-F "mode=fileupload" \
-F "[email protected]" \
-F "path=/private/cat" \
-F "filename=cat.jpeg"
- "create", create
path
folder if not exists (including parent folders).
-
Install rclone.
-
Create your own Google Drive client_id.
-
Create a Google Drive remote in rclone and fill in
client_id
andclient_secret
with the one you made before. -
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
...