Get google drive file ID from google colab

Viewed 345

I'm trying in google colab to get the file ID of a file stored on my google drive.

When the file is created by a function inside google colab, I get a file ID in the form of "local-xxx" instead of the actual file ID. When the file is manually uploaded to google drive, I get the correct ID.

Could you please help me fix this? Posting my code below

  image_drive_url = "/content/gdrive/MyDrive/"+file_name+".png"
  image_id = getoutput("xattr -p 'user.drive.id' '%s'" % image_drive_url) 

  print(image_id)

prints: local-702 instead of the actual file ID

1 Answers

You might want to check out this answer to a related question.

It contains the implementation of a workaround based on the following observation (from another answer in the same thread):

Note: If you are using this in some type of script that is creating new files/folders and quickly reading the 'user.drive.id' afterwards, be aware that it can take many seconds for the "real" file id to be generated. If you read the value of 'user.drive.id' and it starts with 'local', this means that it has not yet generated an actual file id. In my opinion, the best way to deal with this is to create an asynchronous loop that sleeps between checks, and then returns the file id once it no longer starts with 'local'.

ps. I would rather post this as a comment, but I don't have enough rep points yet ;-)

Related