繁体   English   中英

使用 Python 将文件上传到 Google Cloud Storage Bucket 子目录

[英]Upload File to Google Cloud Storage Bucket Sub Directory using Python

我已成功实现将文件上传到 Google Cloud Storage 存储桶的 python 函数,但我想将其添加到存储桶中的子目录(文件夹)中,当我尝试将其添加到存储桶名称时,代码无法找到文件夹。

谢谢!

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 +"/folderName") #I tried to add my folder here
  blob = bucket.blob(destination_blob_name)

  blob.upload_from_filename(source_file_name)

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

您在错误的位置添加了“文件夹”。 请注意,Google Cloud Storage 没有真正的文件夹或目录(请参阅对象名称注意事项)。

模拟目录实际上只是名称中带有前缀的对象。 例如,而不是你现在拥有的:

  • 存储桶 = 存储桶/文件夹名称
  • 对象 = 对象名

你会想要:

  • 桶 = 桶
  • 对象 = 文件夹名/对象名

就您的代码而言,我认为这应该有效:

bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob("folderName/" + destination_blob_name)
blob.upload_from_filename(source_file_name)

暂无
暂无

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

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