0

I am trying to upload a file to a shared drive, however; if I set my drive_id in the options parameters, it still used my default drive, which is "My Drive".

What am I doing wrong? (The drive_id is just an example)

google_file = Google::Apis::DriveV3::File.new(drive_id: "123456789", name: 'test.txt')
drive = Google::Apis::DriveV3::DriveService.new
source_file Tempfile.new("text.txt")
drive.create_file(google_file, upload_source: file)

I have also tried setting the parent_ids when I create a file and am getting an error as well. See below:

Google::Apis::DriveV3::File.new(drive_id: '123456789', name: 'test.txt', parents: ['123456789'])

results in:
Google::Apis::ClientError: notFound: File not found: 123456789.

Any help here will be greatly appreciated!

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
sclem72
  • 466
  • 6
  • 16

1 Answers1

2

I believe your goal is as follows.

  • You want to upload a file to the specific folder in the shared Drive using Drive API v3.
  • You want to achieve this using googleaspif ro ruby.

In this case, how about the following modification?

Modified script:

google_file = Google::Apis::DriveV3::File.new(name: 'test.txt', parents: ['###'])
drive = Google::Apis::DriveV3::DriveService.new
drive.create_file(google_file, upload_source: "text.txt", supports_team_drives: true)
  • Here, please set the folder ID to ### of parents: ['###']. If you want to upload the file to the root folder of the shared Drive, please set the drive ID to ### of parents: ['###'].

Note:

  • This modification suppoges that your drive can be used for uploading the file to the shared Drive. Please be careful about this.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • This works very well and is almost what I want. If I wanted to put it into a folder on that drive, it is giving me an error: `teamDrivesParentLimit: A shared drive item must have exactly one parent` – sclem72 Sep 09 '22 at 04:26
  • @sclem72 Thank you for replying. About `teamDrivesParentLimit: A shared drive item must have exactly one parent`, from this error message, I'm worried that you might put bot the drive ID and the folder ID into `parents: ['###']`. In this case, such error occurs. If my understanding is correct, and If you want to put the file to the specific folder of the shared drive, please put only the folder ID to `parents: ['###']` like `parents: ['###folderId###']`. The folder ID is a unique ID. So, in this case, by `supports_team_drives: true`, the file is put into the folder in the shared drive. – Tanaike Sep 09 '22 at 04:34
  • Gotcha, in the long run, this is what it turned into and it works how I intend it to: `Google::Apis::DriveV3::File.new(name: 'test.txt', drive_id: TEAM_DRIVE_ID, parents: [FOLDER_ID])` – sclem72 Sep 09 '22 at 04:48
  • @sclem72 Thank you for replying. In this case, I think that `drive_id: TEAM_DRIVE_ID` might not be required to be used, because the folder ID is a unique ID in whole drives. – Tanaike Sep 09 '22 at 04:49