Skip to content

Slow Uploads using Google Drive API with Python #1852

Open
@nammonjv

Description

@nammonjv

Expected Behavior

Automatically upload large files daily to Google Drive using Python, with upload speeds matching my internet connection.

Actual Behavior

The upload speed is stuck at around 20 Mbps, even when I use a faster network or adjust the chunk size in the script. I initially thought the issue was a limit within the Python code, but after testing the network speed in the same script, it showed speeds over 100 Mbps.

Here is my script:

file_name = os.path.basename(file_path)
file_size = os.path.getsize(file_path) / (1024 * 1024)

media = MediaFileUpload(file_path, mimetype='application/octet-stream', chunksize=256*1024 * 1024, resumable=True) 

file_metadata = {
    'name': file_name,
    'parents': [folder_id] if folder_id else []
}

try:
    request = service.files().create(media_body=media, body=file_metadata)
    start_time = time.time()
    response = request.execute()
    print(f"Uploaded '{file_name}' successfully at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
    elapsed_time = time.time() - start_time
    upload_speed = file_size / elapsed_time
    print(f"Time used: {elapsed_time}\nUpload speed: {upload_speed:.2f} MB/s or {upload_speed*8:.2f} Mbps")
    return True
except HttpError as error:
    print(f"Failed to upload '{file_name}': {error}")
    return False

I also tried changing the response part to:

response = None
while response is None:
    status, response = request.next_chunk()
    if status:
        print("Uploaded %d%%." % int(status.progress() * 100))

but it showed almost the same results.

Specifications

  • Python version: 3.12.7
  • OS: Windows

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions