Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python将文件上载到Google云存储桶子目录_Python_Python 3.x_Google Cloud Storage_Gcloud Python - Fatal编程技术网

使用Python将文件上载到Google云存储桶子目录

使用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

我已经成功实现了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 = 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”的文件。是否尝试指定其绝对本地文件名?抱歉,我将前缀放在了错误的变量上。现在就试试。非常有用的答案,谢谢。这可能是有关对象命名的最相关文档: