Tutorial sederhana ini menunjukkan penulisan, deployment, dan pemicuan fungsi Cloud Run Berbasis Peristiwa dengan pemicu Cloud Storage untuk merespons peristiwa Cloud Storage.
Jika Anda mencari contoh kode untuk menggunakan Cloud Storage, buka browser contoh Google Cloud.
Menyiapkan aplikasi
Buat bucket Cloud Storage untuk mengupload file pengujian, dengan
YOUR_TRIGGER_BUCKET_NAME
adalah nama bucket yang unik secara global:gcloud storage buckets create gs://YOUR_TRIGGER_BUCKET_NAME
Clone repositori aplikasi contoh ke komputer lokal Anda:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Atau, Anda dapat mendownload contoh sebagai file ZIP dan mengekstraknya.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
Atau, Anda dapat mendownload contoh dalam file ZIP dan mengekstraknya.
Ubah ke direktori yang berisi kode contoh fungsi Cloud Run:
Node.js
cd nodejs-docs-samples/functions/helloworld/
Python
cd python-docs-samples/functions/helloworld/
Go
cd golang-samples/functions/helloworld/
Java
cd java-docs-samples/functions/helloworld/hello-gcs/
Ruby
cd ruby-docs-samples/functions/helloworld/storage/
Men-deploy dan memicu fungsi
Fungsi Cloud Storage didasarkan pada notifikasi Pub/Sub dari Cloud Storage dan mendukung jenis peristiwa serupa:
Bagian berikut menjelaskan cara men-deploy dan memicu fungsi untuk setiap jenis peristiwa ini.
Finalisasi Objek
Peristiwa penyelesaian objek dipicu saat "penulisan" Objek Cloud Storage berhasil diselesaikan. Secara khusus, hal ini berarti bahwa membuat objek baru atau menimpa objek yang ada akan memicu peristiwa ini. Operasi update metadata dan arsip akan diabaikan oleh pemicu ini.
Finalisasi Objek: men-deploy fungsi
Lihat fungsi sampel, yang menangani peristiwa Cloud Storage:
Node.js
Python
Go
Java
Ruby
Untuk men-deploy fungsi, jalankan perintah berikut di direktori tempat kode contoh berada:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs22 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Gunakan flag --runtime
untuk menentukan ID runtime dari versi Node.js yang didukung untuk menjalankan fungsi Anda.
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Gunakan flag --runtime
untuk menentukan ID runtime
versi Python yang didukung untuk menjalankan
fungsi Anda.
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Gunakan flag --runtime
untuk menentukan ID runtime versi Go yang didukung untuk menjalankan fungsi Anda.
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Gunakan flag --runtime
untuk menentukan ID runtime
versi Java yang didukung guna menjalankan
fungsi Anda.
Ruby
gcloud functions deploy hello_gcs --runtime ruby33 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize
Gunakan flag --runtime
untuk menentukan ID runtime
versi Ruby yang didukung untuk menjalankan
fungsi Anda.
dengan YOUR_TRIGGER_BUCKET_NAME
adalah nama bucket Cloud Storage yang memicu fungsi.
Finalisasi Objek: memicu fungsi
Untuk memicu fungsi:
Buat file
gcf-test.txt
kosong di direktori tempat kode contoh berada.Upload file ke Cloud Storage untuk memicu fungsi:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
dengan
YOUR_TRIGGER_BUCKET_NAME
adalah nama bucket Cloud Storage tempat Anda akan mengupload file pengujian.Periksa log untuk memastikan eksekusi telah selesai:
gcloud functions logs read --limit 50
Penghapusan Objek
Peristiwa penghapusan objek dipicu saat objek dihapus sementara. Hal ini terjadi saat objek ditimpa atau dihapus di bucket tanpa mengaktifkan Pembuatan Versi Objek. Menghapus objek dengan menentukan nomor generasinya juga akan menyebabkan objek dihapus sementara.
Penghapusan Objek: men-deploy fungsi
Dengan menggunakan kode contoh yang sama seperti dalam contoh penyelesaian, deploy fungsi dengan penghapusan objek sebagai peristiwa pemicu. Jalankan perintah berikut di direktori tempat kode contoh berada:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs22 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Gunakan flag --runtime
untuk menentukan ID runtime dari versi Node.js yang didukung untuk menjalankan fungsi Anda.
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Gunakan flag --runtime
untuk menentukan ID runtime
versi Python yang didukung untuk menjalankan
fungsi Anda.
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Gunakan flag --runtime
untuk menentukan ID runtime versi Go yang didukung untuk menjalankan fungsi Anda.
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Gunakan flag --runtime
untuk menentukan ID runtime
versi Java yang didukung guna menjalankan
fungsi Anda.
Ruby
gcloud functions deploy hello_gcs --runtime ruby33 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete
Gunakan flag --runtime
untuk menentukan ID runtime
versi Ruby yang didukung untuk menjalankan
fungsi Anda.
dengan YOUR_TRIGGER_BUCKET_NAME
adalah nama bucket Cloud Storage yang memicu fungsi.
Penghapusan Objek: memicu fungsi
Untuk memicu fungsi:
Buat file
gcf-test.txt
kosong di direktori tempat kode contoh berada.Pastikan bucket Anda tidak membuat versi:
gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --no-versioning
Upload file ke Cloud Storage:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
dengan
YOUR_TRIGGER_BUCKET_NAME
adalah nama bucket Cloud Storage tempat Anda akan mengupload file pengujian. Pada tahap ini, fungsi seharusnya belum dijalankan.Hapus file untuk memicu fungsi:
gcloud storage rm gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
Periksa log untuk memastikan eksekusi telah selesai:
gcloud functions logs read --limit 50
Perhatikan bahwa mungkin perlu waktu beberapa saat hingga fungsi selesai dijalankan.
Pengarsipan Objek
Peristiwa pengarsipan objek dipicu saat versi aktif sebuah objek menjadi versi lama. Hal ini terjadi saat objek ditimpa atau dihapus di bucket yang mengaktifkan Pembuatan Versi Objek.
Arsip Objek: men-deploy fungsi
Dengan menggunakan kode contoh yang sama seperti dalam contoh penyelesaian, deploy fungsi dengan arsip objek sebagai peristiwa pemicu. Jalankan perintah berikut di direktori tempat kode contoh berada:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs22 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Gunakan flag --runtime
untuk menentukan ID runtime dari versi Node.js yang didukung untuk menjalankan fungsi Anda.
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Gunakan flag --runtime
untuk menentukan ID runtime
versi Python yang didukung untuk menjalankan
fungsi Anda.
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Gunakan flag --runtime
untuk menentukan ID runtime versi Go yang didukung untuk menjalankan fungsi Anda.
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Gunakan flag --runtime
untuk menentukan ID runtime
versi Java yang didukung guna menjalankan
fungsi Anda.
Ruby
gcloud functions deploy hello_gcs --runtime ruby33 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive
Gunakan flag --runtime
untuk menentukan ID runtime
versi Ruby yang didukung untuk menjalankan
fungsi Anda.
dengan YOUR_TRIGGER_BUCKET_NAME
adalah nama bucket Cloud Storage yang memicu fungsi.
Arsip Objek: memicu fungsi
Untuk memicu fungsi:
Buat file
gcf-test.txt
kosong di direktori tempat kode contoh berada.Pastikan bucket Anda mengaktifkan pembuatan versi:
gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --versioning
Upload file ke Cloud Storage:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
dengan
YOUR_TRIGGER_BUCKET_NAME
adalah nama bucket Cloud Storage tempat Anda akan mengupload file pengujian. Pada tahap ini, fungsi seharusnya belum dijalankan.Arsipkan file untuk memicu fungsi:
gcloud storage rm gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
Perhatikan log untuk memastikan eksekusi telah selesai:
gcloud functions logs read --limit 50
Update Metadata Objek
Peristiwa pembaruan metadata dipicu saat metadata objek yang ada diperbarui.
Pembaruan Metadata Objek: men-deploy fungsi
Dengan menggunakan kode contoh yang sama seperti pada contoh penyelesaian, deploy fungsi dengan pembaruan metadata sebagai peristiwa pemicu. Jalankan perintah berikut di direktori tempat kode contoh berada:
Node.js
gcloud functions deploy helloGCS \ --runtime nodejs22 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Gunakan flag --runtime
untuk menentukan ID runtime dari versi Node.js yang didukung untuk menjalankan fungsi Anda.
Python
gcloud functions deploy hello_gcs \ --runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Gunakan flag --runtime
untuk menentukan ID runtime
versi Python yang didukung untuk menjalankan
fungsi Anda.
Go
gcloud functions deploy HelloGCS \ --runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Gunakan flag --runtime
untuk menentukan ID runtime versi Go yang didukung untuk menjalankan fungsi Anda.
Java
gcloud functions deploy java-gcs-function \ --entry-point functions.HelloGcs \ --runtime java17 \ --memory 512MB \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Gunakan flag --runtime
untuk menentukan ID runtime
versi Java yang didukung guna menjalankan
fungsi Anda.
Ruby
gcloud functions deploy hello_gcs --runtime ruby33 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate
Gunakan flag --runtime
untuk menentukan ID runtime
versi Ruby yang didukung untuk menjalankan
fungsi Anda.
dengan YOUR_TRIGGER_BUCKET_NAME
adalah nama bucket Cloud Storage yang memicu fungsi.
Pembaruan Metadata Objek: memicu fungsi
Untuk memicu fungsi:
Buat file
gcf-test.txt
kosong di direktori tempat kode contoh berada.Pastikan bucket Anda tidak membuat versi:
gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --no-versioning
Upload file ke Cloud Storage:
gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME
dengan
YOUR_TRIGGER_BUCKET_NAME
adalah nama bucket Cloud Storage tempat Anda akan mengupload file pengujian. Pada tahap ini, fungsi seharusnya belum dijalankan.Perbarui metadata file:
gcloud storage objects update gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt --content-type=text/plain
Perhatikan log untuk memastikan eksekusi telah selesai:
gcloud functions logs read --limit 50