def _read_file(self, blob, format):
"""Reads a non-notebook file.
blob: instance of :class:`google.cloud.storage.Blob`.
format:
If "text", the contents will be decoded as UTF-8.
If "base64", the raw bytes contents will be encoded as base64.
If not specified, try to decode as UTF-8, and fall back to base64
"""
bcontent = blob.download_as_string()
if format is None or format == "text":
# Try to interpret as unicode if format is unknown or if unicode
# was explicitly requested.
try:
return bcontent.decode("utf8"), "text"
except UnicodeError:
if format == "text":
raise web.HTTPError(
400, "%s is not UTF-8 encoded" %
self._get_blob_path(blob),
reason="bad format",
)
return base64.encodebytes(bcontent).decode("ascii"), "base64"
评论列表
文章目录