【问题标题】: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)。
我目前正在做的是:
使用请求库将文件下载到我的系统。
使用 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 为您执行此操作。