JAVA ON GOOGLE CLOUD PLATFORM

Try It Free View Documentation

Build, Deploy, Debug, and Monitor Highly Scalable Java Applications

  • Build Java apps on GCP using Eclipse, IntelliJ, or browser based command-line
  • Deploy and scale Spring Boot or Spark Java apps
  • Use Maven or Gradle build tool plugins to build and deploy to GCP
  • Query fully-managed MySQL, PostgreSQL, or NoSQL databases on GCP using Java
  • Deploy Java apps to a managed Kubernetes cluster
  • Run apps on Google maintained OpenJDK or OpenJDK+Jetty9 Docker images
A Broad Set Of Idiomatic Java APIs & Libraries
1
Set up a Cloud Platform Console project
  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Set up a Cloud Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Cloud Storage API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the Cloud Console.

2
Set up a Cloud Storage Bucket

Create a Cloud Storage Bucket:

gsutil mb gs://[YOUR-BUCKET-NAME]

Make the bucket publicly readable so it can serve file:

gsutil defacl set public-read gs://[YOUR_BUCKET_NAME]
3
Add maven dependencies to pom.xml
<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-storage</artifactId>
  <version>1.22.0</version>
</dependency>
4
Add environment variables to app.yaml
env_variables:
  BUCKET_NAME: YOUR-BUCKET-NAME
5
Write your code
@SuppressWarnings("serial")
@WebServlet(name = "upload", value = "/upload")
@MultipartConfig()
public class UploadServlet extends HttpServlet {

  private static final String BUCKET_NAME = System.getenv("BUCKET_NAME");
  private static Storage storage = null;

  @Override
  public void init() {
    storage = StorageOptions.getDefaultInstance().getService();
  }

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,
      ServletException {
    final Part filePart = req.getPart("file");
    final String fileName = filePart.getSubmittedFileName();

    // Modify access list to allow all users with link to read file
    List<Acl> acls = new ArrayList<>();
    acls.add(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
    // the inputstream is closed by default, so we don't need to close it here
    Blob blob =
        storage.create(
            BlobInfo.newBuilder(BUCKET_NAME, fileName).setAcl(acls).build(),
            filePart.getInputStream());

    // return the public download link
    resp.getWriter().print(blob.getMediaLink());
  }
}
JAVA QUICKSTARTS
Find and Fix Production Issues

Google Ops Management provides tracing, debugging logging, monitoring, and error reporting capabilities. It equips you with insights into the health, performance, and availability of cloud-powered applications, enabling you to find and fix issues production faster.

Learn more
JAVA ON GCP COMMUNITIES

Got something to say? Join our community to ask questions, or just chat with the experts at Google who help build the support for Java on Google Cloud Platform.