{GCP Cloud Run functions}Cloud Storage のチュートリアル

 


https://cloud.google.com/functions/docs/tutorials/storage?hl=ja


-- 1. 前作業

gcloud init
gcloud auth list

gcloud --version

gcloud projects create project01-9999999 \
--name="project01"

gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet

 

gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111

gcloud services enable compute.googleapis.com --project project01-9999999

 

-- 2. Cloud Functions, Cloud Build, Artifact Registry, Eventarc, Logging, and Pub/Sub APIs を有効化

gcloud services list --enabled


gcloud services enable cloudfunctions.googleapis.com \
run.googleapis.com \
cloudbuild.googleapis.com \
artifactregistry.googleapis.com \
eventarc.googleapis.com \
logging.googleapis.com \
pubsub.googleapis.com \
--project project01-9999999


-- 3. バケット作成

 

gcloud storage buckets create gs://bucket99999999 \
--default-storage-class=Standard \
--no-enable-autoclass \
--location=asia-northeast1 \
--public-access-prevention \
--uniform-bucket-level-access

gcloud storage ls

 

-- 4. Cloud Storage サービス アカウントに pubsub.publisher ロールを付与


PROJECT_ID=$(gcloud config get-value project)
PROJECT_NUMBER=$(gcloud projects list --filter="project_id:$PROJECT_ID" --format='value(project_number)')

SERVICE_ACCOUNT=$(gcloud storage service-agent --project=$PROJECT_ID)

echo $PROJECT_ID
echo $PROJECT_NUMBER
echo $SERVICE_ACCOUNT


gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$SERVICE_ACCOUNT \
--role roles/pubsub.publisher


-- 5. アプリケーションを準備する


git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
cd python-docs-samples/functions/v2/storage/

ls -l
cat main.py


-- 6. 関数をデプロイしてトリガーする

gcloud functions deploy func01 \
--gen2 \
--runtime=python311 \
--region=asia-northeast1 \
--source=. \
--entry-point=hello_gcs \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=bucket99999999"

gcloud functions list

 

echo "Hello World" > test-finalize.txt
gcloud storage cp test-finalize.txt gs://bucket99999999/test-finalize.txt

gcloud storage ls --long gs://bucket99999999/*

 

gcloud functions logs read func01 --region asia-northeast1 --gen2 --limit=10

 

 

-- 7. クリーンアップ


gcloud functions delete func01 --gen2 --region asia-northeast1 -q

gcloud functions list

gcloud storage rm gs://bucket99999999 --recursive
gcloud storage ls


gcloud projects list

gcloud projects delete project01-9999999 -q


gcloud beta billing projects unlink project01-9999999