我知道我可以像这样上传单个文件:
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'
这是文件夹结构:
我认为路径有问题,但我不确定是什么。我正在执行 Untitled.ipynb
中的代码。适用于 myfile.txt
,但不适用于 myfoldername
。
我不想使用命令行函数。
答案 0 :(得分:1)
您无法在 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)