[英]Download file from API and save/upload it to Google Storage using python
I'm trying to download file from API and save/upload it to Google Storage.我正在尝试从 API 下载文件并将其保存/上传到 Google Storage。 Can anyone help what I'm doing wrong?
谁能帮助我做错了什么?
from google.cloud import storage
import urllib
#create storage client
storage_client = storage.Client()
#Get file:
url = 'https://....'
filename = 'test.xlsx'
bucket = storage_client.get_bucket('my-data-test-bucket')
#Upload:
filedata = urllib.urlretrieve(url, filename)
datafile = bucket.blob(filename)
datafile.upload_from_filename(filedata)
Error message: TypeError: expected string or buffer错误消息: TypeError:预期的字符串或缓冲区
If I try to save it as string like this: datafile.upload_from_string(filedata)如果我尝试将其保存为这样的字符串: datafile.upload_from_string(filedata)
I got error: TypeError: ('test.xlsx', <httplib.HTTPMessage instance at 0x7f9fc927a6c8>) could not be converted to bytes我收到错误: TypeError: ('test.xlsx', <httplib.HTTPMessage instance at 0x7f9fc927a6c8>) 无法转换为字节
I don't have much experience with Python so this mighty be stupid question.我对 Python 没有太多经验,所以这可能是个愚蠢的问题。 I was keep hitting head to wall to solve this and I appreciate all help:)
我一直在努力解决这个问题,我感谢所有帮助:)
in datafile.upload_from_filename(filedata)
can you try replace filedata
with filename
or path of needed file在
datafile.upload_from_filename(filedata)
filedata
可以尝试用filename
或所需文件的路径替换文件数据吗
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.