我正在 docker 中运行 Python 3.6 应用程序,并且希望将输出文件直接存储在 GCS 存储桶中。在 docker 镜像中执行以下操作对我来说听起来很明显:
- 在 Docker 镜像中安装 gcloud
- 运行应用
- 切换到 2.7 虚拟环境,因为 gcloud 需要这样做
- 使用
gcloudcompute scp
将文件复制到存储桶
但是,如何在不切换到另一个 venv 的情况下复制文件?
最佳答案
解决方案 - 创建文件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
关于python - 将文件从 Google Compute Engine VM 上运行的 docker 镜像复制到 Google Storage 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48034638/