为什么upload_from_file Google云存储功能会引发超时错误?

时间:2020-04-02 21:29:43

标签: python google-cloud-platform google-cloud-storage

我创建了一个对我有用的功能,它将文件上传到Google Cloud Storage。

问题是,当我的朋友尝试使用本地计算机上的相同代码将相同文件上传到相同存储桶时,他会收到超时错误。他的互联网非常好,他应该能够在他的连接范围内毫无问题地上传文件。

知道为什么会这样吗?

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 个答案:

答案 0 :(得分:1)

该问题通过减小Blob的块大小来解决。代码更改为:

DisplayGraph