0

I'm implementing Google Drive file management feature into my project using the following library:

github.com/tanaikech/ResumableUploadForGoogleDrive_js

I am able to upload file onto Google Drive, however, the file uploaded is only viewable by the owner. I would like to set the file permission to be "Anyone with Link can view". Am I able to do that directly using the upload API, or must I make a separate API call later to change the permission?

Appreciate help in advance.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
pinghsien422
  • 19,836
  • 1
  • 20
  • 22

1 Answers1

1

I believe your goal is as follows.

In this case, how about the following modification?

From:

if (res.status == "Uploading") {
  msg =
    Math.round(
      (res.progressNumber.current / res.progressNumber.end) * 100
    ) + "%";
} else {
  msg = res.status;
}

To:

if (res.status == "Uploading") {
  msg =
    Math.round(
      (res.progressNumber.current / res.progressNumber.end) * 100
    ) + "%";
} else {
  msg = res.status;
  
  // I added the below script.
  if (msg == "Done") {
    const url = "https://www.googleapis.com/drive/v3/files/" + res.result.id + "/permissions?supportsAllDrives=true";
    fetch(url, { method: "POST", headers: { authorization: "Bearer " + resource.accessToken, "Content-Type": "application/json" }, body: JSON.stringify({ role: "reader", type: "anyone" }) })
      .then(res => res.json())
      .then(res => console.log(res));
  }

}
  • In this modification, after the file was uploaded, the permission is created using "Permissions: create".

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you for your help! Ok so this is issuing another API call after the uploading is done. I was hoping the resumable upload API is able to add the file metadata to set its permission together during creation, I guess I was too greedy :P – pinghsien422 Mar 16 '23 at 08:53
  • @pinghsien422 Thank you for replying. I understand your reply. But, unfortunately, in the current stage, the permission data cannot be included in the request for uploading files. In order to achieve your goal, it is required to use the method of "Permissions: create". It seems that this is the current specification. By the way, if my proposed answer was not useful, I apologize. – Tanaike Mar 16 '23 at 08:57
  • @pinghsien422 And also, of course, this process can be included in the Javascript library. But, in the current stage, I would like to separate the upload process and the post process. I apologize for this situation. – Tanaike Mar 16 '23 at 09:04
  • I ended up changing the permission of the folder which the files are upload into on the server-side via "service.permissions().create(folderId=fid, body={...}).execute() using the python library. This way I don't have to update the permission of the files uploaded. – pinghsien422 Mar 16 '23 at 10:15
  • Hi, can you check to see if you have a bug in the library? Because the res.progressNumber.end is always just 1 no matter what chunkSize I passed into the resource. The file only goes from 0% to 100% in 1 chunk. – pinghsien422 Apr 17 '23 at 10:08
  • @pinghsien422 About `can you check to see if you have a bug in the library?`, when I tested my library, I cannot find the issue. But, I would like to try to find the issue. When I found it, I would like to think of a solution. I deeply apologize for my poor skill. – Tanaike Apr 17 '23 at 11:40
  • I updated the bug report, after finding out that chunkSize must be % 1024 == 0 in order to take effect. However, spliting into multiple chunks results in getting a 503 error from google. Can you please help? Thank you! – pinghsien422 Apr 17 '23 at 17:28