如何修复Colab上传自定义数据集时的HttpError 404问题
Looks like you hit a common PyDrive gotcha in Google Colab—let’s break down the issue and fix it right away:
The Root Cause
Your 404 error occurs because you’re passing the full Google Drive share URL to drive.CreateFile({'id': zip_id}), but PyDrive only expects the unique file ID, not the entire link. The error message even calls this out clearly: it’s trying to look up a file with the ID https://drive.google.com/open?id=1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW, which is obviously not a valid file ID.
Step-by-Step Fix
Extract the actual file ID
From your share linkhttps://drive.google.com/open?id=1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW, the valid file ID is the string directly afterid=—that's1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW.Update your code’s
zip_idline
Replace the full URL with just the extracted file ID:zip_id = '1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW'Full corrected code
Here’s the complete working version of your snippet:!pip install -U -q PyDrive # Insert your file ID # Get it by generating a share URL for the file # An example : https://drive.google.com/file/d/1iz5JmTB4YcBvO7amj3Sy2_scSeAsN4gd/view?usp=sharing zip_id = '1RqLx88tx2FCV0Z3CHsqVtx7S3_ffE-UW' from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive from google.colab import auth from oauth2client.client import GoogleCredentials import zipfile, os # 1. Authenticate and create the PyDrive client. auth.authenticate_user() gauth = GoogleAuth() gauth.credentials = GoogleCredentials.get_application_default() drive = GoogleDrive(gauth) if not os.path.exists('MODEL'): os.makedirs('MODEL') # 2. Download Zip print ("Downloading zip file") myzip = drive.CreateFile({'id': zip_id}) myzip.GetContentFile('model.zip') # 3. Unzip print ("Uncompressing zip file") zip_ref = zipfile.ZipFile('model.zip', 'r') zip_ref.extractall('MODEL/') zip_ref.close()
Extra Tips
- Ensure your Google Drive file is set to publicly accessible (or shared with the Colab service account if using restricted access). If it’s private, you’ll run into permission errors even with the correct ID.
- For files stored in shared folders, you may need to traverse the folder structure using its ID, but for single uploaded files, the direct file ID method works flawlessly.
内容的提问来源于stack exchange,提问作者fetulhak abdurahman

