Почему upload_from_file Функция облачного хранилища Google выдает ошибку времени ожидания? - PullRequest
0 голосов
/ 03 апреля 2020

Я создал функцию, которая работает для меня, она загружает файл в Google Cloud Storage.

Проблема в том, что мой друг пытается загрузить тот же файл в тот же контейнер, используя тот же код из своего локального машина, он получает ошибка тайм-аута . Его inte rnet очень хороший, и он должен быть в состоянии загрузить файл без проблем в его соединениях.

Есть идеи, почему это происходит?

def upload_to_cloud(file_path):
    """
        saves a file in the google storage. As Google requires audio files greater than 60 seconds to be saved on cloud before processing
        It always saves in 'audio-files-bucket' (folder)
        Input:
            Path of file to be saved
        Output:
            URI of the saved file
    """
    print("Uploading to cloud...")
    client = storage.Client().from_service_account_json(KEY_PATH)
    bucket = client.get_bucket('audio-files-bucket')
    file_name = str(file_path).split('\\')[-1]
    print(file_name)
    blob = bucket.blob(file_name)
    f = open(file_path, 'rb')
    blob.upload_from_file(f)
    f.close()
    print("uploaded at: ", "gs://audio-files-bucket/{}".format(file_name))
    return "gs://audio-files-bucket/{}".format(file_name)

Это истекло время ожидания исключение в upload_from_file (f) строке.

Мы попытались использовать функцию upload_from_filename, но та же ошибка все еще происходит.

1 Ответ

1 голос
/ 05 апреля 2020

Проблема решается уменьшением размера фрагмента BLOB-объекта. Код изменен на:

def upload_to_cloud(file_path):
    """
        saves a file in the google storage. As Google requires audio files greater than 60 seconds to be saved on cloud before processing
        It always saves in 'audio-files-bucket' (folder)
        Input:
            Path of file to be saved
        Output:
            URI of the saved file
    """
    print("Uploading to cloud...")
    client = storage.Client().from_service_account_json(KEY_PATH)
    bucket = client.get_bucket('audio-files-bucket')
    file_name = str(file_path).split('\\')[-1]
    print(file_name)
    blob = bucket.blob(file_name)

    ## For slow upload speed
    storage.blob._DEFAULT_CHUNKSIZE = 2097152 # 1024 * 1024 B * 2 = 2 MB
    storage.blob._MAX_MULTIPART_SIZE = 2097152 # 2 MB

    with open(file_path, 'rb') as f:
        blob.upload_from_file(f)
    print("uploaded at: ", "gs://audio-files-bucket/{}".format(file_name))
    return "gs://audio-files-bucket/{}".format(file_name)
...