首页 > 解决方案 > 谷歌云函数抛出 404 错误

问题描述

我创建了一个 Cloud Function,它设置为由 Google Cloud Storage 存储桶上的 Finalize/Create 事件触发。

我将 10k 图像转储到存储桶中,并且大多数图像成功运行了 Cloud Function,而有些图像引发了以下异常,我只是对导致此错误的原因感到困惑。

错误返回 404,但是当我检查存储桶时,图像就在那里。有没有人有修复以下异常的经验?

由于某种原因,云功能正在检查一个不存在的目录。

它应该检查以下目录:

https://storage.googleapis.com/download/storage/v1/b/hidden-name/images%2F3683740a95fa3600d4d9a220c9e0b472.jpg

但是,它正在检查以下不存在的目录(/o文件夹不存在):

https://storage.googleapis.com/download/storage/v1/b/hidden-name/o/images%2F3683740a95fa3600d4d9a220c9e0b472.jpg

这是我得到的例外:

回溯(最后一次调用):文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 721 行,在 download_to_file 传输、file_obj、download_url、标头、开始, end, raw_download 文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 645 行,在 _do_download download.consume(transport) 文件“/env/local/ lib/python3.7/site-packages/google/resumable_media/requests/download.py”,第 153 行,在使用 self._process_response(result) 文件“/env/local/lib/python3.7/site-packages/google /resumable_media/_download.py”,第 171 行,在 _process_response 响应中,_ACCEPTABLE_STATUS_CODES,self._get_status_code 文件“/env/local/lib/python3.7/site-packages/google/resumable_media/_helpers.py”,第 96 行,在 require_status_code *status_codes google.resumable_media.common.InvalidResponse: ('Request failed with status code', 404, 'Expected one of', , ) 在处理上述异常的过程中,发生了另一个异常: Traceback(最近一次调用最后一次):文件“/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py”,第 383 行,在 run_background_function _function_handler.invoke_user_function(event_object) 文件“/env/local/lib/python3. 7/site-packages/google/cloud/functions/worker.py”,第 217 行,invoke_user_function 返回 call_user_function(request_or_event) 文件“/env/local/lib/python3.7/site-packages/google/cloud/functions/ worker.py”,第 214 行,在 call_user_function event_context.Context(**request_or_event.context)) 文件“/user_code/main.py”中,第 44 行,for_each_new_image create_thumb_move_to_thumbs_bucket(bucket_name,blob_name) 文件“/user_code/main.py”,第 56 行,create_thumb_move_to_thumbs_bucket blob_in_bucket.download_to_filename(download_tmp_filepath) 文件“/env/local/lib/python3.7/site-packages/google /cloud/storage/blob.py”,第 761 行,在 download_to_filename raw_download=raw_download,文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 724 行,在 download_to_file _raise_from_invalid_response(exc) 文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”中,第 2161 行,在 _raise_from_invalid_response 中引发 exceptions.from_http_status(response.status_code, message,响应=响应)google.api_core.exceptions.NotFound:404 GET在 for_each_new_image create_thumb_move_to_thumbs_bucket(bucket_name,blob_name) 文件“/user_code/main.py”,第 56 行,在 create_thumb_move_to_thumbs_bucket blob_in_bucket.download_to_filename(download_tmp_filepath) 文件“/env/local/lib/python3.7/site-packages/google/cloud/ storage/blob.py”,第 761 行,在 download_to_filename raw_download=raw_download,文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 724 行,在 download_to_file _raise_from_invalid_response (exc)文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 2161 行,_raise_from_invalid_response 引发 exceptions.from_http_status(response.status_code,message,response=response ) google.api_core.exceptions.NotFound: 404 GET在 for_each_new_image create_thumb_move_to_thumbs_bucket(bucket_name,blob_name) 文件“/user_code/main.py”,第 56 行,在 create_thumb_move_to_thumbs_bucket blob_in_bucket.download_to_filename(download_tmp_filepath) 文件“/env/local/lib/python3.7/site-packages/google/cloud/ storage/blob.py”,第 761 行,在 download_to_filename raw_download=raw_download,文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 724 行,在 download_to_file _raise_from_invalid_response (exc)文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 2161 行,_raise_from_invalid_response 引发 exceptions.from_http_status(response.status_code,message,response=response ) google.api_core.exceptions.NotFound: 404 GET/user_code/main.py”,第 56 行,create_thumb_move_to_thumbs_bucket blob_in_bucket.download_to_filename(download_tmp_filepath) 文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 761 行,在 download_to_filename raw_download=raw_download,文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 724 行,在 download_to_file _raise_from_invalid_response(exc) 文件“/env/local/ lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 2161 行,在 _raise_from_invalid_response 中引发 exceptions.from_http_status(response.status_code, message, response=response) google.api_core.exceptions.NotFound: 404得到/user_code/main.py”,第 56 行,create_thumb_move_to_thumbs_bucket blob_in_bucket.download_to_filename(download_tmp_filepath) 文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 761 行,在 download_to_filename raw_download=raw_download,文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 724 行,在 download_to_file _raise_from_invalid_response(exc) 文件“/env/local/ lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 2161 行,在 _raise_from_invalid_response 中引发 exceptions.from_http_status(response.status_code, message, response=response) google.api_core.exceptions.NotFound: 404得到/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 761 行,在 download_to_filename raw_download=raw_download,文件“/env/local/lib/python3.7/site- packages/google/cloud/storage/blob.py”,第 724 行,在 download_to_file _raise_from_invalid_response(exc) 文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”中,第 2161 行,在 _raise_from_invalid_response 引发 exceptions.from_http_status(response.status_code, message, response=response) google.api_core.exceptions.NotFound: 404 GET/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 761 行,在 download_to_filename raw_download=raw_download,文件“/env/local/lib/python3.7/site- packages/google/cloud/storage/blob.py”,第 724 行,在 download_to_file _raise_from_invalid_response(exc) 文件“/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”中,第 2161 行,在 _raise_from_invalid_response 引发 exceptions.from_http_status(response.status_code, message, response=response) google.api_core.exceptions.NotFound: 404 GET/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 2161 行,在 _raise_from_invalid_response 中引发异常。from_http_status(response.status_code, message, response=response) google.api_core。 exceptions.NotFound: 404 GET/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py”,第 2161 行,在 _raise_from_invalid_response 中引发异常。from_http_status(response.status_code, message, response=response) google.api_core。 exceptions.NotFound: 404 GET https://storage.googleapis.com/download/storage/v1/b/hidden-name/o/images%2F3683740a95fa3600d4d9a220c9e0b472.jpg?alt=media : ('请求失败,状态码',404,'预期之一' , , )

云函数入口方法:

def for_each_new_image(event, context):   
    blob_name = event['name'].lower()
    bucket_name = event['bucket']   
    create_thumb_move_to_thumbs_bucket(bucket_name,blob_name)

引发错误的方法:

def create_thumb_move_to_thumbs_bucket(bucket_name,blob_name):     
    bucket = _storage_client.get_bucket(bucket_name)
    blob_in_bucket = bucket.blob(blob_name)

    blob_name = blob_name.replace("ugc_images/","")    
    thumbnail_filename = blob_name.replace(".","-sm.")

    thumbnail_tmp_filepath = '/tmp/{}'.format(thumbnail_filename)       
    download_tmp_filepath = '/tmp/{}'.format(blob_name)    

    blob_in_bucket.download_to_filename(download_tmp_filepath)

引发错误的行:

blob_in_bucket.download_to_filename(download_tmp_filepath)

标签: google-cloud-platformgoogle-cloud-functionsgoogle-cloud-storage

解决方案


我有同样的错误。我试图访问存储桶中不存在的文件。


推荐阅读