I am trying to upload a file to google cloud storage from within a cloud function. I can't import the cloud storage library into my function, though.
Can cloud storage be used from within cloud functions in this manner?
from google.cloud import storage def upload_blob(bucket_name, blob_text, destination_blob_name): """Uploads a file to the bucket.""" storage_client = storage.Client() bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_string(blob_text) print('File {} uploaded to {}.'.format( source_file_name, destination_blob_name)) def log_data(request): request_json = request.get_json() BUCKET_NAME = 'my-bucket' BLOB_NAME = 'test-blob' BLOB_STR = '{"blob": "some json"}' upload_blob(BUCKET_NAME, BLOB_STR, BLOB_NAME) return f'Success!'
Deployment failure: Function load error: Code in file main.py can't be loaded. File "/user_code/main.py", line 1, in <module> from google.cloud import storage ImportError: cannot import name 'storage' from 'google.cloud' (unknown location)
Go to the Google Cloud Console and the Cloud Functions dashboard. Select Create Function. Fill out the name of the function to use in the URL for the HTTP Trigger. Also choose the Trigger to use.
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.
You need to add the google-cloud-storage
package to your requirements.txt
file to install it in the Cloud Functions environment.
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