You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何用Python上传大文件至Google Drive?含pydrive未验证问题解决方案

Uploading Large Files to Google Drive with Python (Fixing PyDrive's "Unverified App" Warning)

Hey there! Let's tackle your two main issues: getting rid of that frustrating "app not verified" warning when using PyDrive, and successfully uploading large files to Google Drive via Python. I'll cover both PyDrive fixes and a reliable alternative using Google's official API library.


Fixing PyDrive's "App Not Verified" Warning

That warning pops up because Google hasn't verified your OAuth app for public use. Here are two solid workarounds:

1. Add Test Users to Your OAuth App (For Personal/Testing Use)

If you're just using the app for yourself or a small team, full verification isn't necessary:

  • Head to the Google Cloud Console and open your project.
  • Navigate to APIs & Services > OAuth consent screen.
  • Select "External" as the user type, then fill in the required basic info.
  • Scroll to the Test users section, add your email address (and any others who need access), then save.
  • Regenerate your OAuth credentials (download the new client_secrets.json file) and replace the old one in your PyDrive setup.

Now when you authenticate, the warning will disappear for the added test users.

2. Use a Service Account (For Server/Automated Workflows)

Service accounts skip user-facing OAuth consent entirely, making them perfect for automated scripts:

  • In the Google Cloud Console, go to IAM & Admin > Service Accounts.
  • Create a new service account, download the JSON key file (save it as service_account.json).
  • Update your PyDrive auth code to use the service account:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.auth_method = 'service'
gauth.service_account_email = 'your-service-account-email@your-project.iam.gserviceaccount.com'
gauth.service_account_key_file = 'service_account.json'
gauth.ServiceAuth()

drive = GoogleDrive(gauth)

Note: Files uploaded via service account go to the service account's own Drive. To access them from your personal Drive, share a folder with the service account email and upload files to that folder.


Uploading Large Files with PyDrive

By default, PyDrive might struggle with large files (over a few hundred MB). Enable chunked uploads to fix this:

# After setting up auth as above...
large_file = drive.CreateFile({'title': 'my_large_file.zip'})
# Set chunksize to 10MB (adjust based on your network stability)
large_file.SetContentFile('my_large_file.zip', chunksize=1024*1024*10)
large_file.Upload()

print(f"Upload complete! File ID: {large_file['id']}")

The chunksize parameter splits the file into smaller pieces, preventing timeouts and allowing resumable uploads if the connection drops.


Alternative: Use Google's Official google-api-python-client

For more control over large file uploads, Google's official library is a robust choice. It supports resumable chunked uploads natively:

Step 1: Install the Library

pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib

Step 2: Resumable Chunked Upload Code

from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.oauth2 import service_account

# Configure scopes (adjust based on your access needs)
SCOPES = ['https://www.googleapis.com/auth/drive.file']
SERVICE_ACCOUNT_FILE = 'service_account.json'

# Authenticate with service account
credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)
drive_service = build('drive', 'v3', credentials=credentials)

# Set up file metadata and media upload
file_metadata = {'name': 'my_huge_file.iso'}
# 10MB chunks, resumable=True allows pausing/resuming mid-upload
media = MediaFileUpload('my_huge_file.iso', chunksize=1024*1024*10, resumable=True)

# Start upload with progress tracking
request = drive_service.files().create(body=file_metadata, media_body=media)
response = None

while response is None:
    status, response = request.next_chunk()
    if status:
        print(f"Upload progress: {int(status.progress() * 100)}%")

print(f"Upload finished! File ID: {response.get('id')}")

This method is ideal for very large files (1GB+) and gives you real-time visibility into upload progress.


Final Notes

  • If you prefer user-facing OAuth instead of service accounts with the official library, use google-auth-oauthlib.flow.InstalledAppFlow for local authentication.
  • Double-check that your file paths are correct and you have enough storage space in your Google Drive.

内容的提问来源于stack exchange,提问作者mister

火山引擎 最新活动