将 docker 映像 运行 中的文件从 Google Compute Engine VM 复制到 Google 存储桶

Copy a file from a docker image running on a Google Compute Engine VM to a Google Storage bucket

我运行正在 docker 中安装一个 Python 3.6 应用程序,我想将输出文件直接存储在 GCS 存储桶中。在 docker 图像中进行以下操作对我来说听起来很明显:

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

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

解决方案 - 创建文件 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