Cloud Functions(第 2 世代)と Eventarc を使用して Cloud Storage イベントを処理する
もっと見る
このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。
コードサンプル
C#
using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.Protobuf.Cloud.Storage.V1;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
namespace HelloGcs
{
/// <summary>
/// Example Cloud Storage-triggered function.
/// This function can process any event from Cloud Storage.
/// </summary>
public class Function : ICloudEventFunction<StorageObjectData>
{
private readonly ILogger _logger;
public Function(ILogger<Function> logger) =>
_logger = logger;
public Task HandleAsync(CloudEvent cloudEvent, StorageObjectData data, CancellationToken cancellationToken)
{
_logger.LogInformation("Event: {event}", cloudEvent.Id);
_logger.LogInformation("Event Type: {type}", cloudEvent.Type);
_logger.LogInformation("Bucket: {bucket}", data.Bucket);
_logger.LogInformation("File: {file}", data.Name);
_logger.LogInformation("Metageneration: {metageneration}", data.Metageneration);
_logger.LogInformation("Created: {created:s}", data.TimeCreated?.ToDateTimeOffset());
_logger.LogInformation("Updated: {updated:s}", data.Updated?.ToDateTimeOffset());
return Task.CompletedTask;
}
}
}
Go
// Package helloworld provides a set of Cloud Functions samples.
package helloworld
import (
"context"
"fmt"
"log"
"time"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/cloudevents/sdk-go/v2/event"
)
func init() {
functions.CloudEvent("HelloStorage", helloStorage)
}
// StorageObjectData contains metadata of the Cloud Storage object.
type StorageObjectData struct {
Bucket string `json:"bucket,omitempty"`
Name string `json:"name,omitempty"`
Metageneration int64 `json:"metageneration,string,omitempty"`
TimeCreated time.Time `json:"timeCreated,omitempty"`
Updated time.Time `json:"updated,omitempty"`
}
// helloStorage consumes a CloudEvent message and logs details about the changed object.
func helloStorage(ctx context.Context, e event.Event) error {
log.Printf("Event ID: %s", e.ID())
log.Printf("Event Type: %s", e.Type())
var data StorageObjectData
if err := e.DataAs(&data); err != nil {
return fmt.Errorf("event.DataAs: %v", err)
}
log.Printf("Bucket: %s", data.Bucket)
log.Printf("File: %s", data.Name)
log.Printf("Metageneration: %d", data.Metageneration)
log.Printf("Created: %s", data.TimeCreated)
log.Printf("Updated: %s", data.Updated)
return nil
}
Java
import com.google.cloud.functions.CloudEventsFunction;
import com.google.gson.Gson;
import functions.eventpojos.GcsEvent;
import io.cloudevents.CloudEvent;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
public class HelloGcs implements CloudEventsFunction {
private static final Logger logger = Logger.getLogger(HelloGcs.class.getName());
@Override
public void accept(CloudEvent event) {
logger.info("Event: " + event.getId());
logger.info("Event Type: " + event.getType());
if (event.getData() != null) {
String cloudEventData = new String(event.getData().toBytes(), StandardCharsets.UTF_8);
Gson gson = new Gson();
GcsEvent gcsEvent = gson.fromJson(cloudEventData, GcsEvent.class);
logger.info("Bucket: " + gcsEvent.getBucket());
logger.info("File: " + gcsEvent.getName());
logger.info("Metageneration: " + gcsEvent.getMetageneration());
logger.info("Created: " + gcsEvent.getTimeCreated());
logger.info("Updated: " + gcsEvent.getUpdated());
}
}
}
Node.js
const functions = require('@google-cloud/functions-framework');
// Register a CloudEvent callback with the Functions Framework that will
// be triggered by Cloud Storage.
functions.cloudEvent('helloGCS', cloudEvent => {
console.log(`Event ID: ${cloudEvent.id}`);
console.log(`Event Type: ${cloudEvent.type}`);
const file = cloudEvent.data;
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}`);
});
PHP
use CloudEvents\V1\CloudEventInterface;
use Google\CloudFunctions\FunctionsFramework;
// Register the function with Functions Framework.
// This enables omitting the `FUNCTIONS_SIGNATURE_TYPE=cloudevent` environment
// variable when deploying. The `FUNCTION_TARGET` environment variable should
// match the first parameter.
FunctionsFramework::cloudEvent('helloGCS', 'helloGCS');
function helloGCS(CloudEventInterface $cloudevent)
{
// This function supports all Cloud Storage event types.
$log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');
$data = $cloudevent->getData();
fwrite($log, 'Event: ' . $cloudevent->getId() . PHP_EOL);
fwrite($log, 'Event Type: ' . $cloudevent->getType() . PHP_EOL);
fwrite($log, 'Bucket: ' . $data['bucket'] . PHP_EOL);
fwrite($log, 'File: ' . $data['name'] . PHP_EOL);
fwrite($log, 'Metageneration: ' . $data['metageneration'] . PHP_EOL);
fwrite($log, 'Created: ' . $data['timeCreated'] . PHP_EOL);
fwrite($log, 'Updated: ' . $data['updated'] . PHP_EOL);
}
Python
import functions_framework
# Triggered by a change in a storage bucket
@functions_framework.cloud_event
def hello_gcs(cloud_event):
data = cloud_event.data
event_id = cloud_event["id"]
event_type = cloud_event["type"]
bucket = data["bucket"]
name = data["name"]
metageneration = data["metageneration"]
timeCreated = data["timeCreated"]
updated = data["updated"]
print(f"Event ID: {event_id}")
print(f"Event type: {event_type}")
print(f"Bucket: {bucket}")
print(f"File: {name}")
print(f"Metageneration: {metageneration}")
print(f"Created: {timeCreated}")
print(f"Updated: {updated}")
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
次のステップ
他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。