Skip to content

Commit d1cf7a2

Browse files
jwdjjjessiccaa
andauthored
Migrate java-11 sample app to use Java17 (GoogleCloudPlatform#9530)
* Migrate cloudstorage Java 11 (EOS Oct 2024) sample to be compatible with Java 17 * Migrate java-11/cloudstorage sample app to use Java17 and cleanup test cases * Fix header from 2023 to 2024 * Update pom.xml with the latest shared-configuration * Migrate datastore - app engine flex sample from Java 11 (EOS) to Java 17 and later * Migrate websocket-jetty sample in Java 11 (EOS) to Java 17 * Upgrade AppEngine maven plugin to 2.8.1 --------- Co-authored-by: Jessica <jesssica@google.com>
1 parent 34c0352 commit d1cf7a2

File tree

26 files changed

+1759
-0
lines changed

26 files changed

+1759
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Cloud Storage sample for App Engine Flex
2+
3+
<a
4+
href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=flexible/cloudstorage/README.md">
5+
<img alt="Open in Cloud Shell" src
6+
="http://gstatic.com/cloudssh/images/open-btn.png"></a>
7+
8+
This sample demonstrates how to use [Cloud
9+
Storage](https://cloud.google.com/storage/) on Google Managed VMs.
10+
11+
## Setup
12+
13+
Before you can run or deploy the sample, you will need to do the following:
14+
15+
1. Enable the Cloud Storage API in the [Google Developers
16+
Console](https://console.developers.google.com/project/_/apiui/apiview/storage/overview).
17+
1. Create a Cloud Storage Bucket. You can do this with the [Google Cloud
18+
SDK](https://cloud.google.com/sdk) using the following command:
19+
20+
```sh
21+
gsutil mb gs://[your-bucket-name]
22+
```
23+
24+
1. Set the default ACL on your bucket to public read in order to serve files
25+
directly from Cloud Storage. You can do this with the [Google Cloud
26+
SDK](https://cloud.google.com/sdk) using the following command:
27+
28+
```sh
29+
gsutil defacl set public-read gs://[your-bucket-name]
30+
```
31+
32+
1. Update the bucket name in `src/main/appengine/app.yaml`. This makes the
33+
bucket name an environment variable in deployment. You still need to set the
34+
environment variable when running locally, as shown below.
35+
36+
## Deploying
37+
38+
```sh
39+
mvn clean package appengine:deploy
40+
```

flexible/java-17/cloudstorage/pom.xml

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<!--
2+
Copyright 2024 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>jar</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.flexible</groupId>
21+
<artifactId>flexible-cloudstorage</artifactId>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.2.2</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>17</maven.compiler.target>
35+
<maven.compiler.source>17</maven.compiler.source>
36+
<failOnMissingWebXml>false</failOnMissingWebXml> <!-- REQUIRED -->
37+
<appengine.maven.plugin>2.8.1</appengine.maven.plugin>
38+
<spring.boot.version>2.7.18</spring.boot.version>
39+
</properties>
40+
41+
<!-- [START gae_flex_storage_dependencies] -->
42+
<!-- Using libraries-bom to manage versions.
43+
See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
44+
<dependencyManagement>
45+
<dependencies>
46+
<dependency>
47+
<groupId>com.google.cloud</groupId>
48+
<artifactId>libraries-bom</artifactId>
49+
<version>26.45.0</version>
50+
<type>pom</type>
51+
<scope>import</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-dependencies</artifactId>
56+
<version>${spring.boot.version}</version>
57+
<type>pom</type>
58+
<scope>import</scope>
59+
</dependency>
60+
</dependencies>
61+
</dependencyManagement>
62+
63+
<dependencies>
64+
<dependency>
65+
<groupId>com.google.cloud</groupId>
66+
<artifactId>google-cloud-storage</artifactId>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-starter-web</artifactId>
71+
</dependency>
72+
<dependency>
73+
<groupId>javax.servlet</groupId>
74+
<artifactId>javax.servlet-api</artifactId>
75+
<type>jar</type>
76+
<scope>provided</scope>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.junit.vintage</groupId>
80+
<artifactId>junit-vintage-engine</artifactId>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.junit-pioneer</groupId>
85+
<artifactId>junit-pioneer</artifactId>
86+
<version>2.2.0</version>
87+
<scope>test</scope>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.mockito</groupId>
91+
<artifactId>mockito-junit-jupiter</artifactId>
92+
<scope>test</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.mockito</groupId>
96+
<artifactId>mockito-inline</artifactId>
97+
<scope>test</scope>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.springframework.boot</groupId>
101+
<artifactId>spring-boot-starter-test</artifactId>
102+
<scope>test</scope>
103+
</dependency>
104+
<dependency>
105+
<groupId>net.bytebuddy</groupId>
106+
<artifactId>byte-buddy</artifactId>
107+
<version>1.14.17</version>
108+
</dependency>
109+
</dependencies>
110+
<!-- [END gae_flex_storage_dependencies] -->
111+
<build>
112+
<!-- for hot reload of the web application -->
113+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
114+
<plugins>
115+
<plugin>
116+
<groupId>com.google.cloud.tools</groupId>
117+
<artifactId>appengine-maven-plugin</artifactId>
118+
<version>${appengine.maven.plugin}</version>
119+
<configuration>
120+
<projectId>GCLOUD_CONFIG</projectId>
121+
<version>GCLOUD_CONFIG</version>
122+
</configuration>
123+
</plugin>
124+
125+
<plugin>
126+
<groupId>org.springframework.boot</groupId>
127+
<artifactId>spring-boot-maven-plugin</artifactId>
128+
<version>${spring.boot.version}</version>
129+
<executions>
130+
<execution>
131+
<goals>
132+
<goal>repackage</goal>
133+
</goals>
134+
</execution>
135+
</executions>
136+
</plugin>
137+
<plugin>
138+
<groupId>org.apache.maven.plugins</groupId>
139+
<artifactId>maven-surefire-plugin</artifactId>
140+
<configuration>
141+
<includes>
142+
<include>**/*Test*.java</include>
143+
</includes>
144+
</configuration>
145+
</plugin>
146+
</plugins>
147+
</build>
148+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START gae_flex_cloudstorage_yaml]
16+
runtime: java
17+
env: flex
18+
runtime_config:
19+
operating_system: ubuntu22
20+
runtime_version: 17
21+
handlers:
22+
- url: /.*
23+
script: this field is required, but ignored
24+
25+
env_variables:
26+
BUCKET_NAME: YOUR-BUCKET-NAME
27+
# [END gae_flex_cloudstorage_yaml]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.cloudstorage;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.boot.web.servlet.ServletComponentScan;
22+
import org.springframework.web.bind.annotation.GetMapping;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
@SpringBootApplication
26+
@ServletComponentScan("com.example.cloudstorage")
27+
public class Main {
28+
public static void main(String[] args) throws Exception {
29+
SpringApplication.run(Main.class, args);
30+
}
31+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.cloudstorage;
18+
19+
import com.google.cloud.storage.Acl;
20+
import com.google.cloud.storage.Blob;
21+
import com.google.cloud.storage.BlobInfo;
22+
import com.google.cloud.storage.Storage;
23+
import com.google.cloud.storage.StorageOptions;
24+
import java.io.IOException;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import javax.servlet.ServletException;
28+
import javax.servlet.annotation.MultipartConfig;
29+
import javax.servlet.annotation.WebServlet;
30+
import javax.servlet.http.HttpServlet;
31+
import javax.servlet.http.HttpServletRequest;
32+
import javax.servlet.http.HttpServletResponse;
33+
import javax.servlet.http.Part;
34+
35+
// [START gae_flex_storage_app]
36+
@SuppressWarnings("serial")
37+
@WebServlet(name = "upload", value = "/upload")
38+
@MultipartConfig()
39+
public class UploadServlet extends HttpServlet {
40+
41+
private static final String BUCKET_NAME =
42+
System.getenv().getOrDefault("BUCKET_NAME", "my-test-bucket");
43+
private static Storage storage = null;
44+
45+
public UploadServlet() {
46+
storage = StorageOptions.getDefaultInstance().getService();
47+
}
48+
49+
@Override
50+
public void doPost(HttpServletRequest req, HttpServletResponse resp)
51+
throws IOException, ServletException {
52+
final Part filePart = req.getPart("file");
53+
final String fileName = filePart.getSubmittedFileName();
54+
// Modify access list to allow all users with link to read file
55+
List<Acl> acls = new ArrayList<>();
56+
acls.add(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
57+
// the inputstream is closed by default, so we don't need to close it here
58+
Blob blob =
59+
storage.create(
60+
BlobInfo.newBuilder(BUCKET_NAME, fileName).setAcl(acls).build(),
61+
filePart.getInputStream());
62+
63+
// return the public download link
64+
resp.getWriter().print(blob.getMediaLink());
65+
}
66+
}
67+
// [END gae_flex_storage_app]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!--
2+
Copyright 2024 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<html>
18+
<title>App Engine Flex Cloud Storage Sample</title>
19+
<body>
20+
<p>Select a file to upload to your Google Cloud Storage bucket.</p>
21+
<form method="POST" action="/upload" enctype="multipart/form-data">
22+
<input type="file" name="file"> <input type="submit">
23+
</form>
24+
</body>
25+
</html>

0 commit comments

Comments
 (0)