抑制Google App Engine GCS客户端库Python中cloudstorage.open()的日志输出

0 投票
1 回答
537 浏览
提问于 2025-04-18 08:16

我在我的App Engine应用中使用GCS客户端库来将文件写入Google Cloud Storage,语言是Python。

在创建文件之前,我需要确认这个文件是否已经存在,以免覆盖掉已有的文件。

为此,我在尝试创建文件之前会先检查一下文件是否存在:

import cloudstorage as gcs

try:
    gcs_file = gcs.open(filename, 'r')
    gcs_file.close()
    return "File Exists!"
except gcs.NotFoundError as e:
    return "File Does Not Exist: " + str(e)

当我尝试读取一个不存在的文件时,cloudstorage.write()会记录一个404错误(找不到文件)。如果可以的话,我希望能抑制这个日志记录。

谢谢

编辑

这是记录的内容:

12:19:32.565 暂停的生成器 _get_segment(storage_api.py:432) 抛出了 NotFoundError(期望从Google Storage获得状态[200, 206],但 得到了状态404。路径: '/cs-mailing/bde24e63-4d31-41e5-8aff-14b76b239388.html'。请求 头部:{'Range': 'bytes=0-1048575', 'x-goog-api-version': '2', 'accept-encoding': 'gzip, *'}. 响应头部: {'alternate-protocol': '443:quic', 'content-length': '127', 'via': 'HTTP/1.1 GWA', 'x-google-cache-control': 'remote-fetch', 'expires': 'Mon, 02 Jun 2014 11:19:32 GMT', 'server': 'HTTP Upload Server Built on May 19 2014 09:31:01 (1400517061)', 'cache-control': 'private, max-age=0', 'date': 'Mon, 02 Jun 2014 11:19:32 GMT', 'content-type': 'application/xml; charset=UTF-8'}. 内容: "NoSuchKey指定的 键不存在。"。额外信息:无。

1 个回答

0

在你的情况下,日志是因为 str(e) 被触发的。这个问题已经在 https://code.google.com/p/appengine-gcs-client/source/detail?r=172 这个链接中修复了。

撰写回答