gcs.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:stress_transfer 作者: google 项目源码 文件源码
def Write(gs_path, data, suffix='.txt'):
  """Writes data to the cloud."""
  credentials = GoogleCredentials.get_application_default()
  service = discovery.build('storage', 'v1', credentials=credentials)

  bucket_name, object_name = gs_path[5:].split('/', 1)
  logging.info('Uploading file: %s/%s', bucket_name, object_name)

  # Save the data off into a temp file.
  tfile = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
  tfile.write(data)
  tfile.close()

  # Upload the data.
  logging.info('filename: %s   %s     %s', tfile.name, object_name, bucket_name)
  req = service.objects().insert(media_body=tfile.name, name=object_name,
                                 bucket=bucket_name)
  _ = req.execute()

  # Cleanup.
  os.remove(tfile.name)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号