Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload File to Google Cloud Storage Bucket Sub Directory using Python

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))
like image 768
seanie_oc Avatar asked Nov 06 '17 16:11

seanie_oc


People also ask

How do I upload files to Google Cloud Python?

In the Google Cloud console, go to the Cloud Storage Buckets page. In the list of buckets, click on the name of the bucket that you want to upload an object to. In the Objects tab for the bucket, either: Drag and drop the desired files from your desktop or file manager to the main pane in the console.

How do I import a CSV file into a GCP bucket?

Select All Settings > Raw Data Export > CSV Upload. Select Google Cloud Storage from the dropdown menu. Upload your Service Account Key credential file. This is the JSON file created in the Google Cloud Console. Enter your Google Cloud Storage bucket name.

Can I upload files to Google Cloud Storage from URL?

Cloud Storage provides the Signed URL feature to let individual end users perform specific actions. Signed URL makes it possible to generate temporary credentials valid only for a specific end user to securely upload a file. The Google Cloud official client library makes it easy to generate a Signed URL.


1 Answers

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)
like image 56
jterrace Avatar answered Sep 28 '22 05:09

jterrace