简体   繁体   English

从 API 下载文件并使用 python 将其保存/上传到 Google 存储

[英]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 filedatafile.upload_from_filename(filedata) filedata可以尝试用filename或所需文件的路径替换文件数据吗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python Google App Engine API将文件上传到Google存储空间 - upload file to google storage using Python Google App engine API 使用Python从Google云端存储下载多个文件 - Download multiple file from Google cloud storage using Python 使用Python从Google Cloud Storage下载大文件 - Download large file from Google Cloud Storage using Python 使用 Python API 从 Google Cloud Datalab 上传文件到 Google Cloud Storage Bucket - Upload files to Google Cloud Storage Bucket from Google Cloud Datalab using Python API 无法使用 python 代码从存储容器上传/下载文件 - Not able to upload/download files from Storage containers using python code 如何使用 Python API 在 Google Cloud Storage 上上传文件夹 - How to upload folder on Google Cloud Storage using Python API 如何使用Django和Python下载文件并将其保存在上载文件夹中 - How download file and save it inside the upload folder using Django and Python 如何从谷歌云存储中下载 Python 或 Linux 中的文件? - How to download a file in Python or Linux from Google Cloud storage? 使用 Python 将 CSV 文件上传到 Google Cloud Storage - Upload CSV file to Google Cloud Storage using Python Python:使用 URL 将文件上传到 Google Cloud Storage - Python: Upload file to Google Cloud Storage using URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM