How to create new empty files in Google Cloud Storage using Python with client libraries available?
Or how to upload a new file to a selected bucket using blob function "upload_from_filename()" ? To initialize the blob object we should have file already in the cloud bucket, but I want to create a new file name, and copy the content from the file stored locally. I want to do this using python.
Thanks in advance
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.
In the Google Cloud console, go to the Cloud Storage Buckets page. Navigate to the bucket. Click on Create folder to create an empty new folder, or Upload folder to upload an existing folder.
First you need to load the library
import google.cloud.storage
Assuming you have a service account created and JSON file loaded somewhere, you need to create a client object
storage_client = google.cloud.storage.Client.from_service_account_json('JSON filepath')
Then you need to provide the bucket object
bucket = storage_client.get_bucket('bucket_name')
Now define the path within your bucket and the file name
d = 'path/name'
The blob method creates the new file, also an object.
d = bucket.blob(d)
The upload_from_string method add the contents of the file. There are other methods allowing you to perform different tasks.
d.upload_from_string('V')
The first few steps take the longest to be ready the first time.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With