【问题标题】:Copy a file from a docker image running on a Google Compute Engine VM to a Google Storage bucket将文件从在 Google Compute Engine VM 上运行的 docker 映像复制到 Google Storage 存储桶
【发布时间】:2017-12-30 14:02:56
【问题描述】:

我正在 docker 中运行 Python 3.6 应用程序,并且希望将输出文件直接存储在 GCS 存储桶中。在 docker 映像中执行以下操作对我来说听起来很明显:

  • 在 docker 镜像中安装 gcloud
  • 运行应用程序
  • 切换到 2.7 的虚拟环境,因为 gcloud 需要这样做
  • 将带有gcloud compute scp的文件复制到存储桶中

但是,如何在不切换到另一个 venv 的情况下复制文件?

【问题讨论】:

  • 您所说的 GCP VM 是指 GCE 还是 GAE 或其他?
  • 谷歌计算引擎
  • 请不要将您的答案添加到问题本身。您可以将其发布为答案

标签: python python-3.x google-cloud-platform google-cloud-storage gcp


【解决方案1】:

解决方案-创建文件gcpupload.py

from google.cloud import storage

def main():
    client = storage.Client()
    bucket = client.get_bucket('my_bucket')
    blob = bucket.blob('myfile.txt')
    blob.upload_from_filename(filename='/path/myfile.txt')

if __name__ == "__main__":
    main()

运行脚本:

#!/bin/sh
# Need to create the api keys file through google cloud console
# API's and services -> credentials -> service account key
export GOOGLE_APPLICATION_CREDENTIALS=google-api-keys.json
python gcpupload.py 

【讨论】:

    【解决方案2】:

    您可以将文件从您的 Python 应用程序或第二个 Python 脚本存储到 GCS,这非常简单 - https://pypi.python.org/pypi/google-cloud-storage

    【讨论】:

    • 这确实很容易 - 谢谢。我在编辑中包含了代码
    猜你喜欢
    • 2015-09-27
    • 2017-11-29
    • 2018-06-18
    • 2022-01-22
    • 1970-01-01
    • 2014-08-29
    • 2014-07-10
    • 1970-01-01
    • 2013-12-24
    相关资源
    最近更新 更多