How to retrieve specific byte range from Google Drive file using Python

I’m working on a web application and need to extract the final 300 bytes from a Google Drive file without downloading the complete file. I’ve been trying to use HTTP range requests with Python’s urllib2 library but keep running into issues.

Here’s my current approach:

import urllib2

request = urllib2.Request(file_url)
request.headers['Range'] = 'bytes=%s-%s' % (-300, 2)
response = urllib2.urlopen(request)
byte_range = response.headers.get('Content-Range')
print("Range: " + byte_range)
print("Data: " + response.read())

I’m using the download URL from the file’s metadata, but this throws an HTTP 401 Unauthorized error. Does Google Drive support range headers for partial file downloads? I really need to avoid downloading huge files completely.

UPDATE: Found a working solution using proper OAuth authentication:

import httplib2

credentials = GetOAuthCredentials()
file_size = int('total_file_size')
range_headers = {"Range": 'bytes=%s-%s' % (file_size-300, file_size)}
http_client = httplib2.Http()
http_client = credentials.authorize(http_client)
response, data = http_client.request(download_url, "GET", body=None, headers=range_headers)

if response.status == 206:
    print('Success: %s' % response)
    print("Retrieved data: " + data)
else:
    print('Request failed: %s' % response)

Good catch on the authentication issue. You should also handle cases where the file’s smaller than your requested range. I hit this when processing a batch - some files were tiny and my range calculation requested bytes past the file end. That throws a 416 error instead of the expected 206. Just add a simple check like range_start = max(0, file_size - 300) before the request to avoid failures. Also, Google Drive API v3 supports this natively with the alt=media parameter plus Range headers, which might be cleaner than raw download URLs depending on what you’re doing.

check the actual download endpoint, not webContentLink, since it won’t support range reqs. also, try using the requests library instead of urllib2. way easier to use and the header setup looks cleaner. made the switch a while ago and it does the job so much better.

Your OAuth solution looks solid but watch out for token expiration during long operations. I’ve hit similar issues and wrapping requests in a retry mechanism saves you when tokens die mid-process. The download URL also expires after a few hours, so you’ll need to call files.get again if you’re batch processing over time. I’d recommend the Google API client library over raw HTTP requests - it handles token refresh automatically. Performance is basically the same for range requests and the error handling is way better.