This Google Apps Script processes POST requests containing file URLs and saves the files to Google Drive.
Original Blog Post: https://blog.greenflux.us/save-files-to-google-drive-by-post-ing-the-url-to-a-web-app
This script is designed to be deployed as a web app and handle POST requests containing file URLs. It downloads the file from the provided URL and saves it to a specified folder in Google Drive.
- Copy Script: Copy this script into the script editor and save it.
- Trigger Permission Dialog: Run the
doPostfunction once to trigger the permission dialog, and then click allow. - Publish as Web App:
- Go to "Publish" > "Deploy as web app" in the script editor.
- Choose a version name and click "Deploy."
- Set access permissions to "Anyone, even anonymous" or "Anyone within organization."
- Click "Deploy" and authorize the script when prompted.
- Note Web App URL: Note the web app URL generated after deployment, which will be used to send POST requests.
The script expects POST requests with a JSON body containing the following fields:
key: A custom string to authenticate the request. Change the value ofkeyto match the one used in the request.fileUrl: The URL of the file to be downloaded and saved to Google Drive.folderId(optional): The ID of the folder in Google Drive where the file should be saved. If not provided, the file will be saved to the default folder specified in the script.
/**
* @param {Object} e The event object representing the HTTP POST request.
* @return {TextOutput} Text output containing the URL of the new file Drive.
*/
function doPost(e) {
// Function logic
}