This sample uses the Apache Maven build system. Before getting started,
be
sure to download and install it. When you use
Maven as described here, it will automatically download the needed client
libraries.
From the Service account drop-down list, select
New service account.
Enter a name into the Service account name field.
From the Role drop-down list, select Project > Owner.
Note: The Role field authorizes your service account
to access resources. You can view and change this field later using
GCP Console. If you are
developing a production application, specify more granular
permissions than Project > Owner. For more information, see
granting roles to service accounts.
Click Create. A JSON file that contains your key
downloads to your computer.
Enviroment Variables
Provide authentication credentials to your application code by
setting the environment variable GOOGLE_APPLICATION_CREDENTIALS.
Replace [PATH] with the file path of the JSON file that contains your
service account key, and [FILE_NAME] with the
filename.
Clone source
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
cd ./java-docs-samples/translate/cloud-client
Build your project with:
mvn clean package -DskipTests
You can then run a given ClassName via:
mvn exec:java -Dexec.mainClass=com.example.translate.ClassName \
-DpropertyName=propertyValue \
-Dexec.args="any arguments to the app"
Provide authentication credentials to your application code by
running the following command.
Replace [PATH] with the location of the JSON file that contains your
credentials.
Make a Translation API Request using a REST method call to the v2
translate method.
Use curl to make a request to the
https://translation.googleapis.com/language/translate/v2
endpoint.
The curl command includes JSON with the text to be
translated (q), the language to translate from
(source), and the language to translate to
(target).
The source and target languages are identified using the
iso-639-1
codes. The source language is English (en) and the target language is
Spanish (es). The format of the query is noted as "text" for plain text.
The sample curl command uses the gcloud auth print-access-token command to get an authentication token.
curl -s -X POST -H "Content-Type: application/json"\-H "Authorization: Bearer "$(gcloud auth print-access-token)\--data "{
'q': 'The Great Pyramid of Giza (also known as the Pyramid of Khufu or the
Pyramid of Cheops) is the oldest and largest of the three pyramids in
the Giza pyramid complex.',
'source': 'en',
'target': 'es',
'format': 'text'
}""https://translation.googleapis.com/language/translate/v2"
You should see a response similar to the following:
{
"data": {
"translations": [
{
"translatedText": "La Gran Pirámide de Giza (también conocida como la
Pirámide de Khufu o la Pirámide de Keops) es la más antigua y más
grande de las tres pirámides en el complejo de la pirámide de Giza."
}
]
}
}
Congratulations! You've sent your first request to the Cloud Translation API!
Clean up
To avoid unnecessary Google Cloud Platform charges, use the
GCP Console to delete your project if you do not need it.
This quickstart shows you how to create a small App Engine application that
displays a short message.
Before you begin
Before running and deploying this sample, take the following steps:
Use the GCP Console to create a new GCP project,
create an App Engine application, and enable billing:
Go
to App Engine
When prompted, select the region
where you want your App Engine application located and then enable billing.
Download and install the Google Cloud SDK and then initialize the
gcloud tool:
Download the SDK
Alternatively: You can use
Google Cloud Shell, which
comes with git and Cloud SDK already installed, and
many other features, such as language
support and code editors.
After Google Cloud Shell is launched, you can proceed with the rest
of this tutorial and execute your commands within Cloud Shell.
This quickstart demonstrates a simple Node.js application
written with the Express.js
framework.
Download the Hello World app
We've created a simple Hello World app for Node.js so you can quickly
get a feel for deploying an app to the App Engine flexible environment.
Follow these steps from a command line to download Hello World to your local
machine.
Download the sample app and navigate into the app directory:
Clone the Hello World sample app repository to your local machine:
Launch your browser and view the app at
http://YOUR_PROJECT_ID.appspot.com, by running the
following command:
gcloud app browse
This time, the page that displays the Hello World message is delivered by a
web server running on an App Engine instance.
Congratulations! You've deployed your first Node.js app to App
Engine flexible environment!
See the following sections for information about cleaning up as well as links to
the possible next steps that you can take.
Clean up
To avoid incurring charges, you can delete your GCP project to stop
billing for all the resources used within that project.
In the project list, select the project you
want to delete and click Delete project.
In the dialog, type the project ID, and then click
Shut down to delete the project.
What's next
Learn the whole platform
Now that you know what it's like to develop and deploy App Engine apps, you can
stretch out and see the rest of Google Cloud Platform. You already have the
Google Cloud SDK installed and that gives you the tools to interact with
products like Google Cloud SQL, Google Cloud Storage, Google Cloud Datastore,
and more. For a guided walkthrough that teaches you how to create an
application that uses the entire platform, not just App Engine, check out our
quickstart on creating the Bookshelf
app.
Learn about the App Engine flexible environment
Here are some topics to help continue your learning about App Engine.
Hello World is the simplest possible App Engine app, as it contains only one
service, has only one version, and all of the code is located within the app's
root directory. This section describes each of the app files in detail.
app.js
The app.js file contains the JavaScript code to start a server and respond to
requests.
The following code responds to requests from web clients by displaying the
'Hello World' message, via a server that runs on port 8080.
runtime: nodejsenv: flex
# This sample incurs costs to run on the App Engine flexible environment. # The settings below are to reduce costs during testing and are not appropriate# for production use. For more information, see:# https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yamlmanual_scaling:instances: 1resources:cpu: 1
memory_gb: 0.5
disk_size_gb: 10
Here,
app.yaml specifies the runtime used by
the app, and sets env: flex, specifying that the app
uses the flexible environment.
For more information on how the Node.js runtime works, see
The Node.js Runtime.
For more details about how to design your app to take advantage of versions
and services, see An Overview of App
Engine.
{"name":"appengine-hello-world","description":"Simple Hello World Node.js sample for Google App Engine Flexible Environment.","version":"0.0.1","private":true,"license":"Apache-2.0","author":"Google Inc.","repository":{"type":"git","url":"https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"},"engines":{"node":">=4.3.2"},"scripts":{"deploy":"gcloud app deploy","start":"node app.js","lint":"repo-tools lint","pretest":"npm run lint","system-test":"repo-tools test app","test":"npm run system-test","e2e-test":"repo-tools test deploy"},"dependencies":{"express":"4.16.2"},"devDependencies":{"@google-cloud/nodejs-repo-tools":"2.2.1"},"cloud-repo-tools":{"test":{"app":{"msg":"Hello, world!"}},"requiresKeyFile":true,"requiresProjectId":true}}
This package.json specifies that the application uses Node.js v4.3.2 or later, depends on
express, and that node app.js is
used to start the application. For more details about how your application is
installed and run, see the Node.js runtime
documentation.
In this tutorial, you'll deploy a sample Node.js application to Google App
By the end, you'll have learned how to:
Configure Deployments Learn how to edit a YAML file to edit a deployment’s configuration
Deploy apps with Google Cloud Shell Deploy with Cloud Shell, a web-based command line tool
This is a real App Engine deployment, so when the tutorial is over you can keep working on it.
Using Google Cloud Shell
Cloud Shell is a built-in command line tool for the console. We're going to use Cloud Shell to deploy our app.
Open Google Cloud Shell
Clone the sample code
Use
Cloud Shell to clone and navigate to the "Hello World" code. The sample
code is cloned from your project repository to the Cloud Shell.
In Cloud Shell enter:
Clone a sample repository:
You are now in the main directory for the sample code. We'll look at the files that configure your application.
The app.js file contains the JavaScript code to start a server and respond to requests.
The following code responds to requests from web clients by displaying the Hello World message, via a server that runs on port 8080.
Google App Engine uses YAML files to specify a deployment's configuration. app.yaml files contain information about your application, like the runtime environment, URL handlers, and more.
Testing your app
Install npm packages
We will install the npm packages on Cloud Shell in order to test run the Node.js app.
To install npm packages, enter:
npm install
Test your app on Cloud Shell
Cloud
Shell lets you test your app before deploying to make sure it's running
as intended, just like debugging on your local machine.
To test your app enter:
export PORT=8080
npm start
Preview your app with "Web preview"
Your app is now running on Cloud Shell. You can access the app by using "Web preview" to connect to port 8080.
Terminating the preview instance
Terminate the instance of the application by pressing Ctrl+C in the Cloud Shell.
Last steps
Ensuring Compute Engine is initialized
Compute Engine needs a few moments to initialize. The tutorial will continue automatically once it's ready.
Deploying with Cloud Shell
You can use Cloud Shell to deploy your app. To deploy your app enter:
gcloud app deploy --project crawler-20180523
Visit your app
Congratulations! Your app has been deployed. The default URL of your app is crawler-20180523.appspot.com Click the URL to visit it.
The app is now running in the cloud. We will show you how to delete the project to avoid potential charges.
You can check in on your app by monitoring its status on the App Engine dashboard.
Open the menu on the left side of the console.
Then, select the App Engine section.
Congratulations
You have successfully deployed an App Engine application! Here are some next steps:
Delete your tutorial project You should delete your project to avoid additional charges.