使用python将文件上传到谷歌云平台上的bucket时出现问题



当我将文本文件上传到GCP中的bucket时(使用python(,该文件为0KB,并且该文件不包含任何内容。

代码:

from google.cloud import storage
f = open("myText.txt", "w")
f.write("Yon")
f.write("awe")
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""

storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)

print('File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))

upload_blob('dev_kfp', 'myText.txt', "myTextOnline.txt")

上载该文件时出现0个错误,但文件大小为0Kb,并且该文件不包含任何内容。请耐心等待。当我在文件资源管理器上打开文件时,它确实包含内容。文件的类型是GCP bucket上的text/plain。

谢谢!

我可以重现您的问题,根据@mjkool,在关闭脚本上的文件后,上传的文件具有bucket上的大小。这是更新后的脚本:

#!/usr/bin/env python3
from google.cloud import storage
f = open("myText.txt", "w")
f.write("Yon")
f.write("awe")
f.close()
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""

storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)

print('File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))

upload_blob('my_bucket', 'myText.txt', "myTextOnline.txt")   

相关内容

  • 没有找到相关文章

最新更新