Search code examples
node.jsgoogle-drive-apigoogle-drive-shared-driveshared-drive

Create a file/folder in a shared drive using the service account who has Content Manager access


I have access to shared google drive(as Content Manager) and I want to create files in that drive but when I try to do that it gives me error: Following is my code and the error that I received.

Scope I am using

`const SCOPES = ['https://www.googleapis.com/auth/drive'];`


// My authorize function:

`async function authorize() {
  const jwtClient = new google.auth.JWT(
    process.env.CLIENT_EMAIL,
    null,
    process.env.PRIVATE_KEY,
    SCOPES
  );
  await jwtClient.authorize();
  return jwtClient;
}
`

The way I create my drive

`const auth = await authorize();
 const drive = google.drive({ version: 'v3', auth });

 async function createFolderInDrive(drive: any, driveId: string) {
  try {
    const folderMetadata = {
      name: FOLDER_NAME,
      mimeType: 'application/vnd.google-apps.folder',
      parents: [driveId],
    };

    // Create the folder in the shared drive
    const folder = await drive.files.create({
      resource: folderMetadata,
      fields: 'id, name',
    });

    console.log(
      'Folder ${folder.data.name} created with ID: ${folder.data.id}'
    );
  } catch (err) {
    console.error('Error creating folder:', err.message);
  }
}
`

Error I receive: File not found: DRIVE_ID. Although I am able to list the drive using drive.drive.list.

Upload function that I am using:


`async function uploadFile(drive: any, fileName: string, stream: Readable) {
  try {
    const fileMetaData = {
      name: fileName,
      parents: [process.env.DRIVE_ID],
      mimeType: 'application/pdf',
    };

    const media = {
      mimeType: 'application/pdf',
      body: stream,
    };

    const res = await drive.files.create({
      resource: fileMetaData,
      media: media,
    });
    return res;
  } catch (err) {
    console.log('ERROR:', err.message);
  }
}
`

Error I received: 'File not found: DRIVE_ID.

Any help will be appreciated.

I tried a lot of different solutions. One thing that i am thinking that could be the problem is the role. I am not sure about my role in that drive. I have Content-Manager access but I saw that I have to have either write or owner role. I am sure that I am not owner but not sure if I am writer or not. Not sure how can I check my role. I check my permissions through API and i received a permissionId in number.


Solution

  • From Create a file/folder in a shared drive using the service account who has Content Manager access, I believe that your service account has permission to write the shared drive. And, from your showing script, I guessed that driveId might be the root folder or a specific folder of the shared drive. In this case, how about the following modification?

    From:

    const folder = await drive.files.create({
      resource: folderMetadata,
      fields: 'id, name',
    });
    

    And,

    const res = await drive.files.create({
      resource: fileMetaData,
      media: media,
    });
    

    To:

    const folder = await drive.files.create({
      resource: folderMetadata,
      fields: 'id, name',
      supportsAllDrives: true, // Added
    });
    

    And,

    const res = await drive.files.create({
      resource: fileMetaData,
      media: media,
      supportsAllDrives: true, // Added
    });
    

    Reference: