When I am attempting to upload images to a google drive folder through my Python Flask web server using Google API, setting their permissions, and creating a spreadsheet with simple attributes that I have collected from the image, I get a read operation timed out error. It happens randomly for random images every time and for various requests from google API, most commonly when setting permissions for the image file. However, typically the permissions get set anyway regardless of whether they timed out or not. The big issue is that the program just stops for a while until it times out and then continues like normal. Here is the relevant code:
drive_service = build('drive', 'v3', credentials=credentials)
sheets_service = build('sheets', 'v4', credentials=credentials)
spreadsheet_id = find_spreadsheet(drive_service, master_folder_id, master_folder_name)
if not spreadsheet_id:
spreadsheet_id = create_spreadsheet(sheets_service, master_folder_id, master_folder_name)
headers = ['Image', 'Image ID'] + data_points_names
app.logger.debug(f"Headers: {headers}") # Log headers
add_headers_if_empty(sheets_service, spreadsheet_id, headers)
data = [] # Spreadsheet data
for image_id, file in files.items():
file_stream = io.BytesIO(file.read())
file_metadata = {
'name': file.filename,
'parents': [folder_id]
}
media = MediaIoBaseUpload(file_stream, mimetype=file.mimetype, resumable=True)
try:
file_drive = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
file_id = file_drive.get("id")
app.logger.debug(f'File ID: {file_id}')
permission = {
'type': 'anyone',
'role': 'reader'
}
drive_service.permissions().create(fileId=file_id, body=permission).execute()
app.logger.debug(f'Permissions set for file ID: {file_id}')
file_link = f"https://drive.usercontent.google.com/download?id={file_id}&authuser=0"
real_image_id = str(image_id.split('[')[1].split(']')[0])
# app.logger.debug(f"Real image ID: {real_image_id}")
# collect data points for this image
row = [file_link, file_id]
if real_image_id in data_points_values:
# app.logger.debug(f"length of data point names: {len(data_points_names)}")
for i in range(len(data_points_names)):
# app.logger.debug(f"data: {data_points_values[real_image_id].get(str(i))}")
row.append(data_points_values[real_image_id].get(str(i)))
app.logger.debug(f"Row: {row}")
data.append(row)
except Exception as e:
app.logger.error(f'Error uploading file: {e}')
And here are the screenshots of the error messages:
When uploading files and setting permissions lead to read operation timed out
When creating a spreadsheet leads to read operation timed out
Tried running the extra google requests through a separate thread, but was running into I/O error for some reason. Not sure if I was just implementing it wrong but regardless I don’t understand why it can’t work without threading. It has worked perfectly fine until this web server has grown in complexity and size, so I’m wondering if it’s because of that? Please help!
Nate Sorvino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.