def _save_file(self, path, content, format):
"""Uploads content of a generic file to GCS.
:param: path blob path.
:param: content file contents string.
:param: format the description of the input format, can be either
"text" or "base64".
:return: created :class:`google.cloud.storage.Blob`.
"""
bucket_name, bucket_path = self._parse_path(path)
bucket = self._get_bucket(bucket_name, throw=True)
if format not in {"text", "base64"}:
raise web.HTTPError(
400,
u"Must specify format of file contents as \"text\" or "
u"\"base64\"",
)
try:
if format == "text":
bcontent = content.encode("utf8")
else:
b64_bytes = content.encode("ascii")
bcontent = base64.decodebytes(b64_bytes)
except Exception as e:
raise web.HTTPError(
400, u"Encoding error saving %s: %s" % (path, e)
)
blob = bucket.blob(bucket_path)
blob.upload_from_string(bcontent)
return blob
评论列表
文章目录