'How to upload file to a shared folder (fully accessible by anyone with link) using Google API?
If the only thing given, is a link to a shared folder (belongs to a different person), can I upload data to that folder?
Solution 1:[1]
If the folder is publicly shared with anyone as the writer, you can upload a file to the shared folder.
Sample curl command:
For example, when the curl command is used, the sample curl command is as follows.
curl -X POST \
-H "Authorization: Bearer ###your access token###" \
-F 'metadata={"parents": ["###folderId###"], "name": "sample filename"};type=application/json;charset=UTF-8' \
-F "[email protected];type=text/plain;charset=UTF-8" \
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
- In this curl command, a text file of
sample.txtis uploaded to the shared folder. - About
-H "Authorization: Bearer ###your access token###", in this case, please use your access token.
Sample script:
If you cannot retrieve your access token, for example, as a simple sample script for uploading a file, how about using Google Apps Script? When Google Apps Script is used, the access token can be automatically retrieved. But in this case, the file for uploading is required to be put to the Google Drive. If you want to test this script, please copy and paste the following script to the script editor of Google Apps Script project. And, please set the file ID and folder ID, and run sample. When this script is run, a dialog for authorizing scopes is opened. When you authorize it, the file is uploaded to the folder.
function sample() {
const fileId = "###"; // Please set the file ID of the file you want to upload.
const folderId = "###"; // Please set the folder ID of the shared folder.
const file = DriveApp.getFileById(fileId);
const folder = DriveApp.getFolderById(folderId);
file.makeCopy(file.getName(), folder);
}
Note:
- In the case of
uploadType=media, the maximum file size is 5 MB. When you want to upload a large file, please useuploadType=resumable. In this case, please check the official document. Ref
Reference:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tanaike |
