跳到正文
Skillpedia
API 与服务器(后端)48k · #4696

gcp-cloud-functions

它能做什么

这个工具可以让你创建小程序(称为“函数”)并部署到 Google Cloud。当某件事发生时,比如收到网页请求或新增文件,这些程序会自动运行。

适用场景

  • ✓需要某个程序在事件触发时自动运行时。
  • ✓为网站或应用搭建简单的“后端”时。
  • ✓处理来自其他 Google Cloud 服务的数据时。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/gcp-cloud-functions/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/gcp-cloud-functions/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“创建一个函数,当有人访问链接时回复“Hello, World!”。”

每周 5 个新技能,1 分钟读完。

工作原理

你可以用 Python 或 Node.js 等语言编写代码,然后使用特定命令将其部署到 Google Cloud。需要时 Google Cloud 会负责运行你的代码,你不必自己管理服务器。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

GCP Cloud Functions

Build and deploy event-driven serverless applications with Google Cloud Functions (Gen1 and Gen2).

When to Use

  • Processing webhooks, API endpoints, or lightweight HTTP backends
  • Reacting to events from Pub/Sub, Cloud Storage, Firestore, or Eventarc
  • Running scheduled tasks (cron) without maintaining a server
  • Building data-processing pipelines triggered by file uploads
  • Prototyping microservices before committing to Cloud Run or GKE

Prerequisites

  • Google Cloud SDK (gcloud) installed and authenticated
  • APIs enabled: Cloud Functions, Cloud Build, Artifact Registry, Cloud Run (Gen2)
  • IAM role roles/cloudfunctions.developer (or roles/run.developer for Gen2)
gcloud services enable cloudfunctions.googleapis.com cloudbuild.googleapis.com \
  artifactregistry.googleapis.com run.googleapis.com eventarc.googleapis.com

Gen1 vs Gen2 Comparison

Feature Gen1 Gen2 (recommended)
Runtime Cloud Functions infra Built on Cloud Run
Max timeout 9 minutes 60 minutes
Max memory 8 GB 32 GB
Concurrency 1 request/instance Up to 1000/instance
Traffic splitting No Yes
Eventarc triggers No Yes

Deploy an HTTP Function (Gen2)

# Python HTTP function
gcloud functions deploy hello-http \
  --gen2 --region=us-central1 --runtime=python312 \
  --trigger-http --allow-unauthenticated \
  --entry-point=hello_http \
  --memory=256Mi --timeout=60s \
  --min-instances=0 --max-instances=100 \
  --set-env-vars=APP_ENV=production --source=.

# Node.js HTTP function
gcloud functions deploy hello-node \
  --gen2 --region=us-central1 --runtime=nodejs20 \
  --trigger-http --allow-unauthenticated \
  --entry-point=helloNode --memory=256Mi --source=.

Deploy a Pub/Sub Triggered Function

gcloud pubsub topics create order-events

gcloud functions deploy process-order \
  --gen2 --region=us-central1 --runtime=python312 \
  --trigger-topic=order-events \
  --entry-point=process_order \
  --memory=512Mi --timeout=120s --retry \
  --service-account=order-processor@${PROJECT_ID}.iam.gserviceaccount.com \
  --source=.

Deploy a Cloud Storage Triggered Function

gcloud functions deploy process-upload \
  --gen2 --region=us-central1 --runtime=python312 \
  --trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
  --trigger-event-filters="bucket=my-upload-bucket" \
  --entry-point=process_upload \
  --memory=1Gi --timeout=300s --source=.

Deploy a Scheduled Function

gcloud functions deploy daily-cleanup \
  --gen2 --region=us-central1 --runtime=python312 \
  --trigger-http --no-allow-unauthenticated \
  --entry-point=daily_cleanup --source=.

gcloud scheduler jobs create http daily-cleanup-job \
  --schedule="0 2 * * *" \
  --uri="https://us-central1-${PROJECT_ID}.cloudfunctions.net/daily-cleanup" \
  --http-method=POST \
  --oidc-service-account-email=scheduler-sa@${PROJECT_ID}.iam.gserviceaccount.com \
  --location=us-central1

Python Function Examples

# main.py
import functions_framework
import base64, json
from flask import jsonify
from google.cloud import firestore

@functions_framework.http
def hello_http(request):
    """HTTP Cloud Function."""
    name = request.args.get("name", "World")
    return jsonify({"message": f"Hello, {name}!", "status": "ok"}), 200

@functions_framework.cloud_event
def process_order(cloud_event):
    """Triggered by a Pub/Sub message."""
    data = base64.b64decode(cloud_event.data["message"]["data"]).decode("utf-8")
    order = json.loads(data)
    db = firestore.Client()
    db.collection("orders").document(order["id"]).set({
        "status": "processing", "items": order["items"], "total": order["total"],
    })

@functions_framework.cloud_event
def process_upload(cloud_event):
    """Triggered when a file is uploaded to Cloud Storage."""
    data = cloud_event.data
    bucket_name, file_name = data["bucket"], data["name"]
    if not file_name.lower().endswith((".png", ".jpg", ".jpeg")):
        return
    from google.cloud import vision
    client = vision.ImageAnnotatorClient()
    image = vision.Image(source=vision.ImageSource(
        gcs_image_uri=f"gs://{bucket_name}/{file_name}"))
    labels = [l.description for l in client.label_detection(image=image).label_annotations]
    print(f"Labels for {file_name}: {labels}")
# requirements.txt
functions-framework==3.*

…
常见问题

“无服务器”是什么意思?

意思是你不必操心服务器的管理。Google Cloud 会为你处理所有基础设施。

可以使用任何编程语言吗?

可以使用 Python、Node.js、Go、Java 等语言,具体取决于服务版本。

我的程序是如何被触发的?

它可以由网页请求触发(比如访问某个链接),可以由其他 Google Cloud 服务的事件触发(比如文件被添加到存储中),也可以由定时计划触发(比如每天固定时间运行)。

详情

作者的原始描述

Deploy serverless functions on Google Cloud Functions. Configure triggers and manage deployments. Use when implementing serverless workloads on GCP.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#4696
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能