繁体   English   中英

使用 python 将文件上传到 Google 云平台上的存储桶时出现问题

[英]Issue when uploading a file to a bucket on Google cloud platfrom using python

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

编码:

from google.cloud import storage

f = open("myText.txt", "w")
f.write("Yo\n")
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 存储桶上的文本/纯文本。

谢谢!

我能够重现您的问题,并且在关闭脚本上的文件后根据@mjkool,上传的文件具有存储桶上的大小。 这是更新的脚本:

#!/usr/bin/env python3

from google.cloud import storage

f = open("myText.txt", "w")
f.write("Yo\n")
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")   

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM