【问题标题】:Python: Upload file to Google Cloud Storage using URLPython:使用 URL 将文件上传到 Google Cloud Storage
【发布时间】:2019-10-24 06:38:03
【问题描述】:

我有一个文件的 URL (https://example.com/myfile.txt),我想将它上传到我在 Google Cloud Storage 上的存储桶 (gs://my-sample-bucket)

我目前正在做的是:

  1. 使用请求库将文件下载到我的系统。

  2. 使用 python 函数将该文件上传到我的存储桶。

有什么方法可以直接使用网址上传文件。

【问题讨论】:

标签: python file-upload google-cloud-platform


【解决方案1】:

您可以使用 urllib2 或 requests 库从 HTTP 获取文件,然后将现有的 python 代码上传到 Cloud Storage。像这样的东西应该可以工作:

import urllib2
from google.cloud import storage

client = storage.Client()

filedata = urllib2.urlopen('http://example.com/myfile.txt')
datatoupload = filedata.read()

bucket = client.get_bucket('bucket-id-here')
blob = Blob("myfile.txt", bucket)
blob.upload_from_string(datatoupload)

它仍会将文件下载到您系统的内存中,但我认为没有办法告诉 Cloud Storage 为您执行此操作。

【讨论】:

    【解决方案2】:

    有一种方法可以做到这一点,使用 Cloud Storage Transfer 工作,但根据您的用例,它可能值得做或不做。你需要create a transfer jobtransfer a URL list

    我将此问题标记为与this 重复。

    【讨论】:

      猜你喜欢
      • 2017-04-02
      • 2019-11-07
      • 2014-11-22
      • 2020-06-13
      • 2014-01-05
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 2018-12-23
      相关资源
      最近更新 更多