34
loading...
This website collects cookies to deliver better user experience
gcloud sql instances create cloud-sql-demo \
--database-version=MYSQL_8_0 \
--cpu=NUMBER_CPUS \
--memory=MEMORY_SIZE \
--region=REGION
--network # Private IP Address
gcloud
cligcloud compute networks vpc-access connectors create CONNECTOR_NAME \
--network VPC_NETWORK \
--region REGION \
--range IP_RANGE
gcloud run services update SERVICE --vpc-connector CONNECTOR_NAME
sqlalchemy
library by configuring the database host, port, database and credentials.def init_tcp_connection_engine(db_config):
db_user = os.environ["DB_USER"]
db_pass = os.environ["DB_PASS"]
db_name = os.environ["DB_NAME"]
db_host = os.environ["DB_HOST"]
# Extract host and port from db_host
host_args = db_host.split(":")
db_hostname, db_port = host_args[0], int(host_args[1])
pool = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL.create(
drivername="mysql+pymysql",
username=db_user,
password=db_pass,
host=db_hostname,
port=db_port,
database=db_name,
),
**db_config
)
return pool
@app.route("/order", methods=["POST"])
def create_order():
data = request.get_json()
item = data['item']
requested_at = datetime.datetime.utcnow()
stmt = sqlalchemy.text(
"INSERT INTO orders (item, requested_at)" " VALUES (:item, :requested_at)"
)
try:
with db.connect() as conn:
conn.execute(stmt, item=item, requested_at=requested_at)
except Exception as e:
logger.exception(e)
return Response(
status=500,
response=jsonify({ error: "Unable to successfully cast vote! Please check the "
"application logs for more details." }),
)
return Response(
status=200,
response=jsonify({ 'msg' : 'Order Created Successfully'}),
)
gcloud run services update SERVICE_NAME \
--add-cloudsql-instances=CLOUD_SQL_CONNECTION_NAME
--update-env-vars=CLOUD_SQL_CONNECTION_NAME=CLOUD_SQL_CONNECTION_NAME_SECRET \
--update-secrets=DB_USER=DB_USER_SECRET:latest \
--update-secrets=DB_PASS=DB_PASS_SECRET:latest \
--update-secrets=DB_NAME=DB_NAME_SECRET:latest
firebase_admin
package.import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
'projectId': project_id,
})
db = firestore.client()
orders
collection to store.@app.route("/order", methods=["POST"])
def create_order():
data = request.get_json()
item = data['item']
requested_at = datetime.datetime.utcnow()
db_col = db.collection(u'orders');
db_col.add({
u'item': item,
u'requested_at': requested_at
})
return Response(
status=200,
response=jsonify({ 'msg' : 'Order Created Successfully'}),
)
import logging
import os
from flask import Flask, request
from google.cloud import storage
CLOUD_STORAGE_BUCKET = os.environ["cloud_storage_bucket"]
@app.route('/upload', methods=['POST'])
def upload():
"""Process the uploaded file and upload it to Google Cloud Storage."""
uploaded_file = request.files.get('file')
if not uploaded_file:
return 'No file uploaded.', 400
gcs = storage.Client()
# Get the bucket that the file will be uploaded to.
bucket = gcs.get_bucket(CLOUD_STORAGE_BUCKET)
# Create a new blob and upload the file's content.
blob = bucket.blob(uploaded_file.filename)
blob.upload_from_string(
uploaded_file.read(),
content_type=uploaded_file.content_type
)
return blob.public_url