Last active
May 16, 2023 15:15
-
-
Save bluemyria/8bf6746c5d5ef7222dfedcb11b563481 to your computer and use it in GitHub Desktop.
CGP Dev 3. Storing Image/Video Files in Cloud Storage - Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get the Bucket name from the GCLOUD_BUCKET environment variable | |
bucket_name = os.getenv('GCLOUD_BUCKET') | |
# Import the storage module | |
from google.cloud import storage | |
# Create a client for Cloud Storage | |
storage_client = storage.Client() | |
# Use the client to get the Cloud Storage bucket | |
bucket = storage_client.get_bucket(bucket_name) | |
""" | |
Uploads a file to a given Cloud Storage bucket and returns the public url | |
to the new object. | |
""" | |
def upload_file(image_file, public): | |
# Use the bucket to get a blob object | |
blob = bucket.blob(image_file.filename) | |
# Use the blob to upload the file | |
blob.upload_from_string( | |
image_file.read(), | |
content_type=image_file.content_type) | |
# Make the object public | |
if public: | |
blob.make_public() | |
# Modify to return the blob's Public URL | |
return blob.public_url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import the storage module | |
from quiz.gcp import storage, datastore | |
""" | |
uploads file into google cloud storage | |
- upload file | |
- return public_url | |
""" | |
def upload_file(image_file, public): | |
if not image_file: | |
return None | |
# Use the storage client to Upload the file | |
# The second argument is a boolean | |
public_url = storage.upload_file( | |
image_file, | |
public | |
) | |
# Return the public URL for the object | |
return public_url | |
""" | |
uploads file into google cloud storage | |
- call method to upload file (public=true) | |
- call datastore helper method to save question | |
""" | |
def save_question(data, image_file): | |
# If there is an image file, then upload it | |
# And assign the result to a new Datastore | |
# property imageUrl | |
# If there isn't, assign an empty string | |
if image_file: | |
data['imageUrl'] = unicode( | |
upload_file(image_file, True)) | |
else: | |
data['imageUrl'] = u'' | |
data['correctAnswer'] = int(data['correctAnswer']) | |
datastore.save_question(data) | |
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git clone https://github.com/GoogleCloudPlatform/training-data-analyst | |
cd ~/training-data-analyst/courses/developingapps/python/cloudstorage/start | |
# Run prepare_environment.sh, ie Create an App Engine application, | |
# export an environment variable, GCLOUD_PROJECT. | |
# Update pip, then run pip install -r requirements.txt. | |
# Create entities in Cloud Datastore. | |
# Print out the Google Cloud Platform Project ID. | |
. prepare_environment.sh | |
python run_server.py | |
gsutil mb gs://$DEVSHELL_PROJECT_ID-media | |
export GCLOUD_BUCKET=$DEVSHELL_PROJECT_ID-media |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright 2017 Google Inc. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
echo "Creating Datastore/App Engine instance" | |
gcloud app create --region "us-central" | |
echo "Exporting GCLOUD_PROJECT" | |
export GCLOUD_PROJECT=$DEVSHELL_PROJECT_ID | |
#echo "Creating virtual environment" | |
#mkdir ~/venvs | |
#virtualenv ~/venvs/developingapps | |
#source ~/venvs/developingapps/bin/activate | |
echo "Installing Python libraries" | |
sudo pip install --upgrade pip | |
sudo pip install -r requirements.txt | |
echo "Creating Datastore entities" | |
python add_entities.py | |
echo "Project ID: $DEVSHELL_PROJECT_ID" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment