# GCP - Cloud Function with Spring Cloud ###### tags: `Cloud` > 此筆記使用spring boot專案,連接到Cloud SQL。拿取Cloud SQL的資料,並轉換成csv檔案傳送至Cloud Storage > Referrer: https://hackmd.io/@Momentary/SJSF3hQjs ## 1. 下載專案 ``` git clone https://github.com/MomentaryChen/CloudFuntionDemo.git ``` ## 2. 更改Cloud SQL & Cloud Storage Config /src/main/resources - aplication.properties - gcs.properties ### aplication.properties ``` # dev spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect spring.jpa.hibernate.ddl-auto=none spring.jpa.hibernate.show-sql=true # prd spring.datasource.url=${postgresql-url} ex: 'jdbc:postgresql://10.71.160.3:5432/demo' spring.datasource.username=${postgresql-username} ex: 'mysql', 'root' spring.datasource.password=${postgresql-password} ``` ### gcs.properties ``` projectId=keen-mission-374307 credentials=${file-path}/keen-mission-374307-72d817f1d437.json # 本地測試需要使用 bucketName=momentary.bucket ``` ## 本地測試 ``` mvn function:run curl localhost:8080 -d 'AAAAaaa' #result is AAAAAAA ``` 創立Cloud Function ``` gcloud functions deploy cloud-function-demo \ --entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \ --runtime java11 \ --trigger-http \ --region=asia-east1 \ --vpc-connector cloud-run-demo-vpc00 \ --source target/deploy \ --memory 256MB \ --set-secrets postgresql-username=cloud-run-demo-secret-postgresql-username:1 \ --set-secrets postgresql-password=cloud-run-demo-secret-postgresql-password:1 \ --update-env-vars postgresql-url=jdbc:postgresql://10.71.160.3:5432/demo ``` 結果: 1. 上傳資料檔案 ![](https://hackmd.io/_uploads/rkJNrEOD2.png) 2. 從cloud sql中拿取資料,儲存到GCS中 ![](https://hackmd.io/_uploads/ry5rr4ODh.png) 3. GCS下載檔案,查看最終結果 ![](https://hackmd.io/_uploads/Hk15rEdDn.png)