21

I have successfully implemented the python function to upload a file to Google Cloud Storage bucket but I want to add it to a sub-directory (folder) in the bucket and when I try to add it to the bucket name the code fails to find the folder.

Thanks!

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))
seanie_oc
  • 331
  • 2
  • 5
  • 15

1 Answers1

49

You're adding the "folder" in the wrong place. Note that Google Cloud Storage doesn't have real folders or directories (see Object name considerations).

A simulated directory is really just an object with a prefix in its name. For example, rather than what you have right now:

  • bucket = bucket/folderName
  • object = objectname

You'll instead want:

  • bucket = bucket
  • object = folderName/objectname

In the case of your code, I think this should work:

bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob("folderName/" + destination_blob_name)
blob.upload_from_filename(source_file_name)
jterrace
  • 64,866
  • 22
  • 157
  • 202
  • Thanks for the answer. When I tried to implement it I got the following error: blob.upload_from_filename("logFiles/" + source_file_name) File "/usr/local/lib/python3.5/dist-packages/google/cloud/storage/blob.py", line 988, in upload_from_filename with open(filename, 'rb') as file_obj: FileNotFoundError: [Errno 2] No such file or directory: 'logFiles/serverLog.csv.gz' – seanie_oc Nov 06 '17 at 16:35
  • Presumably, you don't have a file in your current working directory named "logFiles/serverLog.csv.gz". Try specifying its absolute local filename? – Brandon Yarbrough Nov 06 '17 at 16:57
  • Sorry, I put the prefix on the wrong variable. Try it now. – jterrace Nov 06 '17 at 17:15
  • 1
    Really useful answer, thanks for this. This is probably the most relevant bit of documentation on object naming: https://cloud.google.com/storage/docs/naming-objects#object-considerations – robstarbuck Nov 18 '20 at 22:47
  • 1
    GCS foldernames don't start with "/". It's just folderName/folderName/fileName – bendecko Jun 19 '22 at 09:59