gpt4 book ai didi

python - 使用 Python api-client-library 将文本文件上传到 Google Cloud Storage。适用于图像,不适用于文本

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:01 25 4
gpt4 key购买 nike

我正在尝试将文本文件从我的非 Google 服务器上传到 Google 存储空间。当文件是图像 (png) 时它有效,但当它是文本文件时则无效。我明白

<HttpError 400 when requesting https://www.googleapis.com/upload/storage/v1/b/my_bucket_name/o?uploadType=resumable&alt=json&predefinedAcl=publicRead&name=media%2Fmy_file.txt returned "Bad Request"

.

credentials = GoogleCredentials.get_application_default()
google_service = build('storage', 'v1', credentials=credentials)

bucket = "my_bucket_name"

filename = "/home/path/my_image.png"
filename_new = "media/my_image.png"

# Fails with txt file instead of image
#filename = "/home/path/my_file.txt"
#filename_new = "media/my_file.txt"

media = MediaFileUpload(filename, chunksize=4194304, resumable=True)
req = google_service.objects().insert(bucket=bucket,
name=filename_new ,
media_body=media,
body={"cacheControl": "public,max-age=31536000"},
predefinedAcl='publicRead')
resp = None
while resp is None:
status, resp = req.next_chunk()

最佳答案

关键是要包含 mimetype:

filename = "/home/path/my_file.txt"
media = MediaFileUpload(filename, chunksize=4194304, mimetype='plain/text', resumable=True)

其他:

mimetype='image/png'

mimetype='application/gzip'

关于python - 使用 Python api-client-library 将文本文件上传到 Google Cloud Storage。适用于图像,不适用于文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32685401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com