使用Python将文件上载到Google云存储桶子目录
我已经成功实现了python函数,将文件上传到Google云存储bucket,但我想将其添加到bucket中的子目录(文件夹),当我尝试将其添加到bucket名称时,代码无法找到文件夹 谢谢使用Python将文件上载到Google云存储桶子目录,python,python-3.x,google-cloud-storage,gcloud-python,Python,Python 3.x,Google Cloud Storage,Gcloud Python,我已经成功实现了python函数,将文件上传到Google云存储bucket,但我想将其添加到bucket中的子目录(文件夹),当我尝试将其添加到bucket名称时,代码无法找到文件夹 谢谢 def upload_blob(bucket_name, source_file_name, destination_blob_name): """Uploads a file to the bucket.""" storage_client = storage.Client() bucket
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云存储没有真正的文件夹或目录(请参阅)
模拟目录实际上只是一个名称中带有前缀的对象。例如,与您现在拥有的不同:
- bucket=bucket/folderName
- object=objectname
- 铲斗=铲斗
- object=folderName/objectname
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob("folderName/" + destination_blob_name)
blob.upload_from_filename(source_file_name)
谢谢你的回答。当我试图实现它时,我遇到了以下错误:blob.upload_from_filename(“logFiles/”+source_file_name)file“/usr/local/lib/python3.5/dist packages/google/cloud/storage/blob.py”,第988行,在upload_from_filename中,以open(filename,'rb')'作为文件\u obj:FileNotFoundError:[Errno 2]没有这样的文件或目录:“logFiles/serverLog.csv.gz”可能,您当前的工作目录中没有名为“logFiles/serverLog.csv.gz”的文件。是否尝试指定其绝对本地文件名?抱歉,我将前缀放在了错误的变量上。现在就试试。非常有用的答案,谢谢。这可能是有关对象命名的最相关文档: