java触发器功能_使用Java中的云功能触发云存储

本文介绍了如何在Java中利用云功能来触发云存储操作,详细解析了相关功能的实现过程。
摘要由CSDN通过智能技术生成

java触发器功能

Google Cloud Functions is a serverless execution environment for building and connecting cloud services. With Cloud Functions, you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your function is triggered when an event being watched is fired. Your code executes in a fully managed environment. There is no need to provision any infrastructure or worry about managing any servers.

Google Cloud Functions是用于构建和连接云服务的无服务器执行环境。 使用云功能,您可以编写简单的,单一用途的功能,这些功能附加到从云基础架构和服务发出的事件。 触发正在监视的事件时,将触发您的功能。 您的代码在完全托管的环境中执行。 无需配置任何基础架构,也无需担心管理任何服务器。

  • Serverless execution environment for building and connecting cloud services

    用于构建和连接云服务的无服务器执行环境
  • Write simple, single-purpose functions that are attached to events emitted from your cloud services

    编写附加到云服务发出的事件的简单,单一用途的函数
  • These functions execute code in response to events based on triggers

    这些函数执行代码以响应基于触发器的事件

云功能中使用的关键术语 (Key Terms used in Cloud Functions)

事件 (Event)

Events are the things that happen in the cloud environment. These can be:

事件是在云环境中发生的事情。 这些可以是:

  • changes to data in a database

    对数据库中数据的更改
  • files added to a storage system

    文件添加到存储系统
  • a new virtual machine instance is created

    创建一个新的虚拟机实例

触发 (Trigger)

Declaration of interest in an event. We bind a function to a trigger and when a trigger is set, the function is executed.

对事件感兴趣的声明。 我们将函数绑定到触发器,并在设置触发器后执行该函数。

Image for post

Triggers are associated with available resources such as Cloud storage, Cloud pub-sub, HTTP, Firebase Analytics, Firebase Authentication and Firebase real-time database.

触发器与可用资源相关联,例如云存储,云发布订阅,HTTP,Firebase Analytics,Firebase身份验证和Firebase实时数据库。

运行时间 (Runtimes)

Cloud Functions supports multiple language runtime:

Cloud Functions支持多种语言运行时:

Image for post

使用云功能触发云存储 (Triggering Cloud Storage with Cloud Functions)

The idea is when an object is put in Google Cloud Storage, cloud function must be triggered and it will be logged likewise.

想法是,当将对象放入Google Cloud Storage中时,必须触发云功能,并且将同样记录该功能。

Step 1.) Create a Cloud Storage Bucket

步骤1. )创建云存储桶

Go to Navigation → Cloud Storage → Give a unique bucket name

转到导航→云存储→提供唯一的存储桶名称

Image for post

Step 2.) Create a Cloud Function

步骤2.)创建云功能

Go to Navigation →Cloud Functions →Create Function

转到导航→云功能→创建功能

Image for post

Step 3.) Selecting the Trigger and Event Type

步骤3.)选择触发器和事件类型

After clicking Create Function, you must give a proper name of the Cloud Function and select the Trigger as Cloud Storage and Event type as Finalise/Create (this determines when an object is put into the bucket, it will trigger cloud function).

单击“ 创建函数”后 ,您必须给云函数一个适当的名称,并选择“触发器”作为“ 云存储” ,然后选择“事件类型”作为“ 完成/创建” (这确定何时将对象放入存储桶中,它将触发云功能)。

Image for post

Step 4.) Select the Google Cloud Storage bucket you want to trigger.

步骤4.)选择您要触发的Google Cloud Storage存储桶。

Image for post

Step 5.) Selecting the Runtime for running the Cloud Function

步骤5.)选择运行时以运行Cloud Function

The Java programming language recently turned 25 years old, and it’s still one of the top-used languages powering today’s enterprise application customers. Google brought Java 11 to Google Cloud Functions in Q1 2020. That means you can now write Cloud Functions using your favorite JVM languages ( Java, Kotlin, Groovy, Scala, etc) with our Functions Framework for Java, and also with Spring Cloud Functions and Micronaut!

Java编程语言最近已经有25年历史了,它仍然是为当今企业应用程序客户提供支持的最常用的语言之一。 Google在2020年第一季度将Java 11引入了Google Cloud Functions。这意味着您现在可以通过我们的Java函数框架,使用自己喜欢的JVM语言( JavaKotlinGroovyScala等)来编写Cloud Functions,还可以使用Spring Cloud FunctionsMicronaut

With Cloud Functions for Java 11, now in beta, you can use Java to build business-critical applications and integration layers, and deploy the function in a fully managed environment, complete with access to resources in a private VPC network. Java functions will scale automatically based on your load. You can write HTTP functions to respond to HTTP events, and background functions to process events sourced from various cloud and GCP services, such as Pub/Sub, Cloud Storage, Firestore, and more.

借助处于Java测试版中的Java 11云功能,您可以使用Java来构建关键业务应用程序和集成层,并在完全托管的环境中部署该功能,并可以访问专用VPC网络中的资源。 Java函数将根据您的负载自动缩放。 您可以编写HTTP函数来响应HTTP事件,并编写后台函数来处理源自各种云和GCP服务(例如发布/订阅,云存储,Firestore等)的事件。

So, you have to select Java 11 in the Runtime drop-down.

因此,您必须在Runtime下拉列表中选择Java 11。

Image for post

pom.xml (pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cloudfunctions</groupId>
<artifactId>gcs-function</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.0.1</version>
<type>jar</type>
</dependency>
</dependencies>
<!-- Required for Java 11 functions in the inline editor -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<excludes>
<exclude>.google/</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

src / main / java / com / example / example.java (src/main/java/com/example/example.java)

package com.example;
import com.example.Example.GCSEvent;
import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import java.util.logging.Logger;
public class Example implements BackgroundFunction<GCSEvent> {
private static final Logger logger = Logger.getLogger(Example.class.getName());
@Override
public void accept(GCSEvent event, Context context) {
logger.info("Processing file: " + event.name);
throw new UnsupportedOperationException("Not supported yet.");
}
public static class GCSEvent {
String bucket;
String name;
String metageneration;
}
}

Step 6.) Click the Create button for creating Cloud Function

步骤6.)单击创建按钮以创建云功能

Image for post

Step 7.) Check and confirm that the Cloud Function is successfully created.

步骤7.)检查并确认已成功创建Cloud Function。

Image for post

Step. 8) Check the View logs under Actions → 3-horizonatal-dots

步。 8)检查“ 操作”→“ 3-水平点”下的“ 查看日志

When the function is created, you can view the logs to see that the function has been created.

创建功能后,您可以查看日志以查看功能已创建。

Image for post

Step 9.) Upload a file in GCS bucket to trigger Cloud Functions

步骤9.)在GCS存储桶中上传文件以触发Cloud Functions

Image for post

Step 10.) Check the logs after uploading the file in GCS bucket

步骤10.)将文件上传到GCS存储桶后,检查日志

Image for post

结论 (Conclusion)

Cloud Functions are a great fit for serverless application backends for integrating with third-party services and APIs, or for mobile or IoT backends. You can also use functions for real-time data processing systems, like processing files as they are uploaded to Cloud Storage, or to handle real-time streams of events from Pub/Sub. Last but not least, functions can serve intelligent applications like virtual assistants and chat bots, or video, image and sentiment analysis.

云功能非常适合与第三方服务和API集成的无服务器应用程序后端,或者移动或IoT后端。 您还可以将功能用于实时数据处理系统,例如处理文件上传到Cloud Storage时的功能,或处理来自Pub / Sub的实时事件流。 最后但并非最不重要的一点是,功能可以为智能助理,聊天机器人或视频,图像和情感分析之类的智能应用程序提供服务。

翻译自: https://medium.com/swlh/triggering-cloud-storage-with-cloud-functions-in-java-177d7c3c1208

java触发器功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值