Google Photos API not uploading all of the content

I have a folder with 2500 folders and subfolders (700GB) of images that I want to upload to Google Photos in albums. I already created all of the albums using their API and had no problem. However, when I tried to upload the photos, some of them work and some don't. Almost every photo returns a successful token, even if it doesn't upload the image.

I'm not going over the 10k requests per day (4500 photos, 9000 requests) but the Google API console says that I use a lot less (below 2,000). The requests are delayed, with a few seconds between each photo.

Here is a snippet of the code:

for dir_name, subdir_list, file_list in os.walk(path, topdown=True):
        with open("log.txt", "a", encoding="utf-8") as f:
            f.write(f"Starting folder number {counter} ({dir_name})\n")
        for file in file_list:
            file_counter += 1

            folder_name = dir_name.split(path + '\\')[1]
            if '\\' in folder_name:
                folder_name = folder_name.split('\\')[0]
            file_path = os.path.join(dir_name, file)
            root, extension = os.path.splitext(file_path)
            if extension[1:].upper() not in EXTENSIONS:

            album_id = albums_dict.get(translated_name, None)
            if not album_id:

                token = upload(photos_service, file_path, access_token)
            createItem(photos_service, token, album_id, access_token)

def upload(service, log_file, access_token):
    f = open(log_file, 'rb').read()

    url = ''
    headers = {
        'Authorization': "Bearer " + access_token,
        'Content-Type': 'application/octet-stream',
        'X-Goog-Upload-File-Name': log_file,
        'X-Goog-Upload-Protocol': "raw",

    r =, data=f, headers=headers)
    # print(f'\nUpload token: {r.content}')
    return r.content

def createItem(service, upload_token, albumId, access_token):
    url = ''

    body = {
        'newMediaItems': [
                "simpleMediaItem": {
                    "uploadToken": upload_token.decode("utf-8")

    if albumId is not None:
        body['albumId'] = albumId

    bodySerialized = json.dumps(body)
    headers = {
        'Authorization': "Bearer " + access_token,
        'Content-Type': 'application/json',

    r =, data=bodySerialized, headers=headers)
    # print(f'\nCreate item response: {r.content}')
    return r.content``

The createItem and upload function I took from the internet. Also I have a log file with all the success and errors so I know what happened.