python - 将文件从 Google Compute Engine VM 上运行的 docker 镜像复制到 Google Storage 存储桶

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

我正在 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/

相关文章:

python - Django:表单验证接受首字母大写的字符串

python - 解包 n 级嵌套列表

python - QPixmap 和 QRubberBand 缩放问题

google-cloud-platform - Google 云存储网络使用情况

google-app-engine - image.ServingURL 和 Google Storage BlobKey 在开发服务器上不工作

python - 如何使用 Python 访问 Google Cloud Storage 上的文件

php - 在 MySQL 中导入和处理文本文件

python - 通过 Telethon 报告垃圾邮件或滥用行为

python - 我怎样才能使用 python 获取停用词列表

python - Django DateField 的默认值格式