Search code examples
rubygoogle-drive-apigoogle-drive-shared-drivegoogle-api-ruby-client

How to upload a file to a shared drive with Google API Drive v3 ruby gem


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!


Solution

  • 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: