微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用python将文件夹上传到谷歌云存储桶

如何解决使用python将文件夹上传到谷歌云存储桶

我知道我可以像这样上传单个文件

bucket_name = "my-bucket-name"
bucket = client.get_bucket(bucket_name)

blob_name = "myfile.txt"
blob = bucket.blob(blob_name)

blob.upload_from_filename(blob_name)

如何对文件夹执行相同操作?有没有类似 blob.upload_from_foldername 的东西? 我尝试使用相同的代码myfile.txt 替换为 myfoldername,但没有奏效。

FileNotFoundError: [Errno 2] No such file or directory: 'myfoldername'

这是文件夹结构:

enter image description here

我认为路径有问题,但我不确定是什么。我正在执行 Untitled.ipynb 中的代码。适用于 myfile.txt,但不适用于 myfoldername

我不想使用命令行函数

解决方法

您无法在 Google Cloud Storage 中上传空文件夹或目录,但可以使用客户端在 Cloud Storage 中创建空文件夹:

from google.cloud import storage

def create_newfolder(bucket_name,destination_folder_name):
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_folder_name)

    blob.upload_from_string('')

    print('Created {} .'.format(destination_folder_name))

如果您要上传整个目录,可以使用以下代码:

import glob
import os 
from google.cloud import storage

client = storage.Client()
def upload_from_directory(directory_path: str,destination_bucket_name: str,destination_blob_name: str):
    rel_paths = glob.glob(directory_path + '/**',recursive=True)
    bucket = client.get_bucket(destination_bucket_name)
    for local_file in rel_paths:
        remote_path = f'{destination_blob_name}/{"/".join(local_file.split(os.sep)[1:])}'
        if os.path.isfile(local_file):
            blob = bucket.blob(remote_path)
            blob.upload_from_filename(local_file)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。