הדרכה בנושא Cloud Storage (דור ראשון)

במדריך הפשוט הזה נסביר איך לכתוב, לפרוס ולהפעיל פונקציית Cloud Run מבוססת-אירועים באמצעות טריגר של Cloud Storage, כדי להגיב לאירועים ב-Cloud Storage.

אם אתם מחפשים דוגמאות קוד לשימוש ב-Cloud Storage, תוכלו להיעזר בדפדפן לדוגמה של Google Cloud.

מטרות

עלויות

במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:

  • Cloud Run functions
  • Cloud Storage

כדי להעריך את ההוצאות בהתאם לתחזית השימוש שלכם, אתם יכולים להיעזר במחשבון העלויות.

משתמשים חדשים של Google Cloud ? יכול להיות שאתם זכאים לתקופת ניסיון בחינם.

לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Functions, Cloud Build, Cloud Storage, and Eventarc APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  5. התקינו את ה-CLI של Google Cloud.

  6. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  7. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Functions, Cloud Build, Cloud Storage, and Eventarc APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  11. התקינו את ה-CLI של Google Cloud.

  12. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  13. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init
  14. אם כבר התקנתם את ה-CLI של gcloud, מריצים את הפקודה הבאה כדי לעדכן אותו:

    gcloud components update
  15. מכינים את סביבת הפיתוח:

הכנת הבקשה

  1. יוצרים קטגוריה של Cloud Storage כדי להעלות קובץ בדיקה, כאשר YOUR_TRIGGER_BUCKET_NAME הוא שם קטגוריה ייחודי גלובלית:

    gcloud storage buckets create gs://YOUR_TRIGGER_BUCKET_NAME
  2. משכפלים את מאגר האפליקציה לדוגמה ומעבירים אותו למכונה המקומית:

    Node.js

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git

    אפשרות נוספת היא להוריד את הדוגמה כקובץ ZIP ולחלץ אותה.

    Python

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

    אפשרות נוספת היא להוריד את הדוגמה כקובץ ZIP ולחלץ אותה.

    המשך

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git

    אפשרות נוספת היא להוריד את הדוגמה כקובץ ZIP ולחלץ אותה.

    Java

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git

    אפשרות נוספת היא להוריד את הדוגמה כקובץ ZIP ולחלץ אותה.

    Ruby

    git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git

    אפשרות נוספת היא להוריד את הדוגמה כקובץ ZIP ולחלץ אותה.

  3. עוברים לספרייה שמכילה את הקוד לדוגמה של פונקציות 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/

פריסה והפעלה של הפונקציה

הפונקציות של Cloud Storage מבוססות על התראות Pub/Sub מ-Cloud Storage ותומכות בסוגי אירועים דומים:

בקטעים הבאים מוסבר איך פורסים פונקציה ומפעילים אותה לכל אחד מסוגי האירועים האלה.

Object Finalize

אירועי סיום של אובייקט מופעלים כשמתבצעת פעולת כתיבה של אובייקט Cloud Storage והיא מסתיימת בהצלחה. במילים אחרות, יצירה של אובייקט חדש או החלפה של אובייקט קיים מפעילה את האירוע הזה. הטריגר הזה מתעלם מפעולות של עדכון מטא-נתונים והעברה לארכיון.

Object Finalize: deploying the function

כדאי לעיין בפונקציה לדוגמה שמטפלת באירועים של Cloud Storage:

Node.js

/**
 * Generic background Cloud Function to be triggered by Cloud Storage.
 * This sample works for all Cloud Storage CRUD operations.
 *
 * @param {object} file The Cloud Storage file metadata.
 * @param {object} context The event metadata.
 */
exports.helloGCS = (file, context) => {
  console.log(`  Event: ${context.eventId}`);
  console.log(`  Event Type: ${context.eventType}`);
  console.log(`  Bucket: ${file.bucket}`);
  console.log(`  File: ${file.name}`);
  console.log(`  Metageneration: ${file.metageneration}`);
  console.log(`  Created: ${file.timeCreated}`);
  console.log(`  Updated: ${file.updated}`);
};

Python

def hello_gcs(event, context):
    """Background Cloud Function to be triggered by Cloud Storage.
       This generic function logs relevant data when a file is changed,
       and works for all Cloud Storage CRUD operations.
    Args:
        event (dict):  The dictionary with data specific to this type of event.
                       The `data` field contains a description of the event in
                       the Cloud Storage `object` format described here:
                       https://cloud.google.com/storage/docs/json_api/v1/objects#resource
        context (google.cloud.functions.Context): Metadata of triggering event.
    Returns:
        None; the output is written to Cloud Logging
    """

    print(f"Event ID: {context.event_id}")
    print(f"Event type: {context.event_type}")
    print("Bucket: {}".format(event["bucket"]))
    print("File: {}".format(event["name"]))
    print("Metageneration: {}".format(event["metageneration"]))
    print("Created: {}".format(event["timeCreated"]))
    print("Updated: {}".format(event["updated"]))

המשך


// Package helloworld provides a set of Cloud Functions samples.
package helloworld

import (
	"context"
	"fmt"
	"log"
	"time"

	"cloud.google.com/go/functions/metadata"
)

// GCSEvent is the payload of a GCS event.
type GCSEvent struct {
	Kind                    string                 `json:"kind"`
	ID                      string                 `json:"id"`
	SelfLink                string                 `json:"selfLink"`
	Name                    string                 `json:"name"`
	Bucket                  string                 `json:"bucket"`
	Generation              string                 `json:"generation"`
	Metageneration          string                 `json:"metageneration"`
	ContentType             string                 `json:"contentType"`
	TimeCreated             time.Time              `json:"timeCreated"`
	Updated                 time.Time              `json:"updated"`
	TemporaryHold           bool                   `json:"temporaryHold"`
	EventBasedHold          bool                   `json:"eventBasedHold"`
	RetentionExpirationTime time.Time              `json:"retentionExpirationTime"`
	StorageClass            string                 `json:"storageClass"`
	TimeStorageClassUpdated time.Time              `json:"timeStorageClassUpdated"`
	Size                    string                 `json:"size"`
	MD5Hash                 string                 `json:"md5Hash"`
	MediaLink               string                 `json:"mediaLink"`
	ContentEncoding         string                 `json:"contentEncoding"`
	ContentDisposition      string                 `json:"contentDisposition"`
	CacheControl            string                 `json:"cacheControl"`
	Metadata                map[string]interface{} `json:"metadata"`
	CRC32C                  string                 `json:"crc32c"`
	ComponentCount          int                    `json:"componentCount"`
	Etag                    string                 `json:"etag"`
	CustomerEncryption      struct {
		EncryptionAlgorithm string `json:"encryptionAlgorithm"`
		KeySha256           string `json:"keySha256"`
	}
	KMSKeyName    string `json:"kmsKeyName"`
	ResourceState string `json:"resourceState"`
}

// HelloGCS consumes a(ny) GCS event.
func HelloGCS(ctx context.Context, e GCSEvent) error {
	meta, err := metadata.FromContext(ctx)
	if err != nil {
		return fmt.Errorf("metadata.FromContext: %w", err)
	}
	log.Printf("Event ID: %v\n", meta.EventID)
	log.Printf("Event type: %v\n", meta.EventType)
	log.Printf("Bucket: %v\n", e.Bucket)
	log.Printf("File: %v\n", e.Name)
	log.Printf("Metageneration: %v\n", e.Metageneration)
	log.Printf("Created: %v\n", e.TimeCreated)
	log.Printf("Updated: %v\n", e.Updated)
	return nil
}

Java

import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import functions.eventpojos.GcsEvent;
import java.util.logging.Logger;

/**
 * Example Cloud Storage-triggered function.
 * This function can process any event from Cloud Storage.
 */
public class HelloGcs implements BackgroundFunction<GcsEvent> {
  private static final Logger logger = Logger.getLogger(HelloGcs.class.getName());

  @Override
  public void accept(GcsEvent event, Context context) {
    logger.info("Event: " + context.eventId());
    logger.info("Event Type: " + context.eventType());
    logger.info("Bucket: " + event.getBucket());
    logger.info("File: " + event.getName());
    logger.info("Metageneration: " + event.getMetageneration());
    logger.info("Created: " + event.getTimeCreated());
    logger.info("Updated: " + event.getUpdated());
  }
}

Ruby

require "functions_framework"

FunctionsFramework.cloud_event "hello_gcs" do |event|
  # This function supports all Cloud Storage events.
  # The `event` parameter is a CloudEvents::Event::V1 object.
  # See https://cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event/V1.html
  payload = event.data

  logger.info "Event: #{event.id}"
  logger.info "Event Type: #{event.type}"
  logger.info "Bucket: #{payload['bucket']}"
  logger.info "File: #{payload['name']}"
  logger.info "Metageneration: #{payload['metageneration']}"
  logger.info "Created: #{payload['timeCreated']}"
  logger.info "Updated: #{payload['updated']}"
end

כדי לפרוס את הפונקציה, מריצים את הפקודה הבאה בספרייה שבה נמצא קוד הדוגמה:

Node.js

gcloud functions deploy helloGCS \
--runtime nodejs22 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize

משתמשים בדגל --runtime כדי לציין את מזהה סביבת זמן הריצה של גרסת Node.js נתמכת להרצת הפונקציה.

Python

gcloud functions deploy hello_gcs \
--runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Python נתמכת להרצת הפונקציה.

המשך

gcloud functions deploy HelloGCS \
--runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.finalize

כדי לציין את מזהה זמן הריצה של גרסת Go נתמכת להרצת הפונקציה, משתמשים בדגל --runtime.

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

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Java נתמכת להרצת הפונקציה.

Ruby

gcloud functions deploy hello_gcs --runtime ruby33 \
-
-trigger-resource YOUR_TRIGGER_BUCKET_NAME \
-
-trigger-event google.storage.object.finalize

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Ruby נתמכת להרצת הפונקציה.

כאשר YOUR_TRIGGER_BUCKET_NAME הוא השם של קטגוריה של Cloud Storage שמפעילה את הפונקציה.

סיום אובייקט: הפעלת הפונקציה

כדי להפעיל את הפונקציה:

  1. יוצרים קובץ gcf-test.txt ריק בספרייה שבה נמצא קוד הדוגמה.

  2. מעלים את הקובץ ל-Cloud Storage כדי להפעיל את הפונקציה:

    gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME

    כאשר YOUR_TRIGGER_BUCKET_NAME הוא השם של הקטגוריה של Cloud Storage שאליה תעלו קובץ בדיקה.

  3. בודקים ביומנים שההפעלות הסתיימו:

    gcloud functions logs read --limit 50
    

מחיקת אובייקט

אירועי מחיקת אובייקטים מופעלים כשמתבצעת מחיקה רכה של אובייקט. זה קורה כשקובץ נמחק או נכתב מחדש בקטגוריה שבה לא מופעל ניהול גרסאות של אובייקטים. מחיקת אובייקט על ידי ציון מספר הדור שלו גורמת גם למחיקה רכה של האובייקט.

מחיקת אובייקט: פריסת הפונקציה

באמצעות אותו קוד לדוגמה כמו בדוגמה של השלמת הפעולה, פורסים את הפונקציה עם מחיקת האובייקט כאירוע שמפעיל את הפונקציה. מריצים את הפקודה הבאה בספרייה שבה נמצא קוד לדוגמה:

Node.js

gcloud functions deploy helloGCS \
--runtime nodejs22 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete

משתמשים בדגל --runtime כדי לציין את מזהה סביבת זמן הריצה של גרסת Node.js נתמכת להרצת הפונקציה.

Python

gcloud functions deploy hello_gcs \
--runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Python נתמכת להרצת הפונקציה.

המשך

gcloud functions deploy HelloGCS \
--runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.delete

כדי לציין את מזהה זמן הריצה של גרסת Go נתמכת להרצת הפונקציה, משתמשים בדגל --runtime.

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

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Java נתמכת להרצת הפונקציה.

Ruby

gcloud functions deploy hello_gcs --runtime ruby33 \
-
-trigger-resource YOUR_TRIGGER_BUCKET_NAME \
-
-trigger-event google.storage.object.delete

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Ruby נתמכת להרצת הפונקציה.

כאשר YOUR_TRIGGER_BUCKET_NAME הוא השם של קטגוריה של Cloud Storage שמפעילה את הפונקציה.

מחיקת אובייקט: הפעלת הפונקציה

כדי להפעיל את הפונקציה:

  1. יוצרים קובץ gcf-test.txt ריק בספרייה שבה נמצא קוד הדוגמה.

  2. מוודאים שאין ניהול גרסאות ב-bucket:

    gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --no-versioning
  3. מעלים את הקובץ ל-Cloud Storage:

    gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME

    כאשר YOUR_TRIGGER_BUCKET_NAME הוא השם של הקטגוריה של Cloud Storage שאליה תעלו קובץ בדיקה. בשלב הזה הפונקציה עדיין לא אמורה לפעול.

  4. כדי להפעיל את הפונקציה, מוחקים את הקובץ:

    gcloud storage rm gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
  5. בודקים ביומנים שההפעלות הסתיימו:

    gcloud functions logs read --limit 50
    

שימו לב: יכול להיות שיעבור זמן עד שהפונקציה תסיים את ההרצה.

ארכיון אובייקטים

אירועים של ארכיון אובייקטים מופעלים כשגרסה פעילה של אובייקט הופכת לגרסה לא עדכנית. זה קורה כשמבצעים פעולת כתיבה על אובייקט או מחיקה של אובייקט בקטגוריה שבה מופעלת התכונה ניהול גרסאות של אובייקטים.

ארכיון אובייקטים: פריסת הפונקציה

משתמשים באותו קוד לדוגמה כמו בדוגמה של השלמת הפעולה, ופורסים את הפונקציה עם ארכיון האובייקט כאירוע ההפעלה. מריצים את הפקודה הבאה בספרייה שבה נמצא קוד לדוגמה:

Node.js

gcloud functions deploy helloGCS \
--runtime nodejs22 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive

משתמשים בדגל --runtime כדי לציין את מזהה סביבת זמן הריצה של גרסת Node.js נתמכת להרצת הפונקציה.

Python

gcloud functions deploy hello_gcs \
--runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Python נתמכת להרצת הפונקציה.

המשך

gcloud functions deploy HelloGCS \
--runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.archive

כדי לציין את מזהה זמן הריצה של גרסת Go נתמכת להרצת הפונקציה, משתמשים בדגל --runtime.

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

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Java נתמכת להרצת הפונקציה.

Ruby

gcloud functions deploy hello_gcs --runtime ruby33 \
-
-trigger-resource YOUR_TRIGGER_BUCKET_NAME \
-
-trigger-event google.storage.object.archive

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Ruby נתמכת להרצת הפונקציה.

כאשר YOUR_TRIGGER_BUCKET_NAME הוא השם של קטגוריה של Cloud Storage שמפעילה את הפונקציה.

ארכיון אובייקטים: הפעלת הפונקציה

כדי להפעיל את הפונקציה:

  1. יוצרים קובץ gcf-test.txt ריק בספרייה שבה נמצא קוד הדוגמה.

  2. מוודאים שהאפשרות 'ניהול גרסאות' מופעלת בדלי:

    gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --versioning
  3. מעלים את הקובץ ל-Cloud Storage:

    gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME

    כאשר YOUR_TRIGGER_BUCKET_NAME הוא השם של הקטגוריה של Cloud Storage שאליה תעלו קובץ בדיקה. בשלב הזה הפונקציה עדיין לא אמורה לפעול.

  4. מעבירים את הקובץ לארכיון כדי להפעיל את הפונקציה:

    gcloud storage rm gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt
  5. כדי לוודא שההרצות הסתיימו, אפשר לבדוק את היומנים:

    gcloud functions logs read --limit 50
    

עדכון מטא-נתונים של אובייקט

אירועי עדכון מטא-נתונים מופעלים כשמעדכנים את המטא-נתונים של אובייקט קיים.

עדכון מטא-נתונים של אובייקט: פריסת הפונקציה

באמצעות אותו קוד לדוגמה כמו בדוגמה של השלמת הפעולה, פורסים את הפונקציה עם עדכון המטא-נתונים כאירוע ההפעלה. מריצים את הפקודה הבאה בספרייה שבה נמצא קוד לדוגמה:

Node.js

gcloud functions deploy helloGCS \
--runtime nodejs22 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate

משתמשים בדגל --runtime כדי לציין את מזהה סביבת זמן הריצה של גרסת Node.js נתמכת להרצת הפונקציה.

Python

gcloud functions deploy hello_gcs \
--runtime python312 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Python נתמכת להרצת הפונקציה.

המשך

gcloud functions deploy HelloGCS \
--runtime go121 \
--trigger-resource YOUR_TRIGGER_BUCKET_NAME \
--trigger-event google.storage.object.metadataUpdate

כדי לציין את מזהה זמן הריצה של גרסת Go נתמכת להרצת הפונקציה, משתמשים בדגל --runtime.

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

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Java נתמכת להרצת הפונקציה.

Ruby

gcloud functions deploy hello_gcs --runtime ruby33 \
-
-trigger-resource YOUR_TRIGGER_BUCKET_NAME \
-
-trigger-event google.storage.object.metadataUpdate

משתמשים בדגל --runtime כדי לציין את מזהה זמן הריצה של גרסת Ruby נתמכת להרצת הפונקציה.

כאשר YOUR_TRIGGER_BUCKET_NAME הוא השם של קטגוריה של Cloud Storage שמפעילה את הפונקציה.

עדכון מטא-נתונים של אובייקט: הפעלת הפונקציה

כדי להפעיל את הפונקציה:

  1. יוצרים קובץ gcf-test.txt ריק בספרייה שבה נמצא קוד הדוגמה.

  2. מוודאים שאין ניהול גרסאות ב-bucket:

    gcloud storage buckets update gs://YOUR_TRIGGER_BUCKET_NAME --no-versioning
  3. מעלים את הקובץ ל-Cloud Storage:

    gcloud storage cp gcf-test.txt gs://YOUR_TRIGGER_BUCKET_NAME

    כאשר YOUR_TRIGGER_BUCKET_NAME הוא השם של הקטגוריה של Cloud Storage שאליה תעלו קובץ בדיקה. בשלב הזה הפונקציה עדיין לא אמורה לפעול.

  4. מעדכנים את המטא-נתונים של הקובץ:

    gcloud storage objects update gs://YOUR_TRIGGER_BUCKET_NAME/gcf-test.txt --content-type=text/plain
  5. כדי לוודא שההרצות הסתיימו, אפשר לבדוק את היומנים:

    gcloud functions logs read --limit 50
    

הסרת המשאבים

כדי להימנע מחיובים בחשבון Google Cloud בגלל השימוש במשאבים שנעשה במסגרת המדריך הזה, אפשר למחוק את הפרויקט שמכיל את המשאבים, או להשאיר את הפרויקט ולמחוק את המשאבים בנפרד.

מחיקת הפרויקט

הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך.

כדי למחוק את הפרויקט:

  1. במסוף Google Cloud , נכנסים לדף Manage resources.

    כניסה לדף Manage resources

  2. ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
  3. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.

מחיקת הפונקציה

מחיקה של פונקציות Cloud Run לא מסירה משאבים שמאוחסנים ב-Cloud Storage.

כדי למחוק את הפונקציה שיצרתם במדריך הזה, מריצים את הפקודה הבאה:

Node.js

gcloud functions delete helloGCS 

Python

gcloud functions delete hello_gcs 

Go

gcloud functions delete HelloGCS 

Java

gcloud functions delete java-gcs-function 

Ruby

gcloud functions delete hello_gcs 

אפשר גם למחוק פונקציות Cloud Run ממסוףGoogle Cloud .

המאמרים הבאים