gpt4 book ai didi

python - 如何使用 Python API 在 Google Cloud Storage 上上传文件夹

转载 作者:行者123 更新时间:2023-12-03 18:16:40 26 4
gpt4 key购买 nike

我已经在Google Cloud Storage上成功上传了单个文本文件。但是当我尝试上传whole folder时,它会授予denied error.权限

filename = "d:/foldername"   #here test1 is the folder.


Error:
Traceback (most recent call last):
File "test1.py", line 142, in <module>
upload()
File "test1.py", line 106, in upload
media = MediaFileUpload(filename, chunksize=CHUNKSIZE, resumable=True)
File "D:\jatin\Project\GAE_django\GCS_test\oauth2client\util.py", line 132, in positional_wrapper
return wrapped(*args, **kwargs)
File "D:\jatin\Project\GAE_django\GCS_test\apiclient\http.py", line 422, in __init__
fd = open(self._filename, 'rb')
IOError: [Errno 13] Permission denied: 'd:/foldername'

最佳答案

这对我有用。将本地目录中的所有内容复制到谷歌云存储中的特定存储桶名称/完整路径(递归):

import glob
from google.cloud import storage

def upload_local_directory_to_gcs(local_path, bucket, gcs_path):
assert os.path.isdir(local_path)
for local_file in glob.glob(local_path + '/**'):
if not os.path.isfile(local_file):
upload_local_directory_to_gcs(local_file, bucket, gcs_path + "/" + os.path.basename(local_file))
else:
remote_path = os.path.join(gcs_path, local_file[1 + len(local_path):])
blob = bucket.blob(remote_path)
blob.upload_from_filename(local_file)


upload_local_directory_to_gcs(local_path, bucket, BUCKET_FOLDER_DIR)

关于python - 如何使用 Python API 在 Google Cloud Storage 上上传文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25599503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com