【问题标题】:400 Error while uploading blob on Cloud Storage在 Cloud Storage 上上传 Blob 时出现 400 错误
【发布时间】:2019-10-14 19:00:46
【问题描述】:

我在 Cloud Function 中创建了一个 csv 文件。现在,我需要将它上传到 Cloud Storage。我将它存储在 /tmp 文件中。从那里,我必须将其上传到 Cloud Storage。

使用的代码 -

def upload_blob(bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)
    blob.upload_from_file(source_file_name)
    print('File {} uploaded to {}.'.format(source_file_name,destination_blob_name))

final_df.to_csv('/tmp/'+file_name)
with open('/tmp/'+file_name, 'r') as file_obj:
    upload_blob('test-bucket',file_obj,file_name)

但是,我面临以下错误 -

BadRequest: 400 PUT https://www.googleapis.com/upload/storage/v1/b/test-bucket/o?uploadType=resumable&upload_id=ABC: ('Request failed with status code', 400, 'Expected one of', <HTTPStatus.OK: 200>, 308)

文件存储在 /tmp 文件夹中。从那里,它没有被上传。究竟是什么错误?

【问题讨论】:

    标签: python-3.x csv google-cloud-platform google-cloud-functions google-cloud-storage


    【解决方案1】:

    400 error Bad request(不是 404)有很多可能的原因。也许您应该指定完整路径并且您没有打开文件并将其传递给 source_file_name。这是我尝试成功将对象(使用 mp3 文件尝试)上传到 GCS 的内容

    from google.cloud import storage
    
    def upload_blob(bucket_name, source_file_name, destination_blob_name):
        """Uploads a file to the bucket."""
        storage_client = storage.Client()
        bucket = storage_client.get_bucket(bucket_name)
        blob = bucket.blob(destination_blob_name)
    
        blob.upload_from_filename(source_file_name)
    
        print('File {} uploaded to {}.'.format(
            source_file_name,
            destination_blob_name))
    
    
    if __name__ == '__main__':
        upload_blob ("[bucketname]", "/home/[username]/[filename]", "[filename]")
    

    【讨论】:

    • 这有帮助。我没有打开文件上传,而是直接上传。有效。谢谢:)
    • 不用担心.. :)
    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 2013-06-16
    • 2015-03-17
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    相关资源
    最近更新 更多