# 2024-12-3 ## Cloud Function **把環境包裝成Docker 放在Google 雲端中執行** ### 最簡單的測試 * 簡單的測試   * 先不修改,直接部屬,然後測試  ### Iris **先不用Cloud Function 做一次** * clone 專案 [gcp_serverless_ml](https://github.com/saedhussain/gcp_serverless_ml) * 建立模型 `cd gcp_serverless_ml/Iris_model/` ```bash $ python create_iris_model.py 1.0 #代表完成 ``` * 下載訓練完的模型  * 上傳到Cloud Storage  * 建立Function 把[main.py](https://github.com/saedhussain/gcp_serverless_ml/blob/main/Iris_http_cloud_func/main.py) 跟[requirements.txt](https://github.com/saedhussain/gcp_serverless_ml/blob/main/Iris_http_cloud_func/requirements.txt) 放進來  **Code 裡面的 BUCKET_NAME、PROJECT_ID、MODEL_FILE_NAME.pkl 要修改** **Entrypoint要改**  ### 檔案大小分類 * 建立兩個bucket  * 基本的bucket  * requirements.txt ```txt= functions-framework==3.* google-cloud-storage google-cloud ``` * main.py ```python= import functions_framework from google.cloud import storage from google.cloud.storage import Blob # Triggered by a change in a storage bucket @functions_framework.cloud_event def hello_gcs(cloud_event): data = cloud_event.data event_id = cloud_event["id"] event_type = cloud_event["type"] bucket = data["bucket"] name = data["name"] metageneration = data["metageneration"] timeCreated = data["timeCreated"] updated = data["updated"] print("="*30) print(f"Event ID: {event_id}") print(f"Event type: {event_type}") print(f"Bucket: {bucket}") print(f"File: {name}") print(f"Metageneration: {metageneration}") print(f"Created: {timeCreated}") print(f"Updated: {updated}") print(f"Processing file: {name}.") storage_client = storage.Client(project='CHANGE_PROJECT_ID') source_bucket=storage_client.get_bucket('BUCKET_SRC') destination_bucket=storage_client.get_bucket('BUCKET_DIS') blobs=list(source_bucket.list_blobs(prefix='')) print(blobs) for blob in blobs: if blob.size > 1000000 and blob.name == name: source_blob = source_bucket.blob(blob.name) new_blob = source_bucket.copy_blob(source_blob, destination_bucket, blob.name) blob.delete(if_generation_match=None) print(f'File moved from {source_blob} to {new_blob}') else: print("File size is below 1MB") ``` * 到bucket 上傳檔案看成果  --- ## SQL **Cloud Shell** * 建立資料庫 ```bash gcloud sql instances create mydb \ --database-version=MYSQL_5_7 --cpu=2 --memory=4GB \ --root-password=admin1234 --assign-ip \ --zone=asia-east1-a --availability-type=zonal --no-backup ``` **跑完之後**   * 建立資料表 ``` gcloud sql databases create demo-db --instance=mydb, -i mydb ```  * 連線到資料庫 ``` gcloud sql connect mydb --user=root ``` 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up