├── .gitignore ├── LICENSE.md ├── README.md ├── demo ├── .gitignore ├── README.md ├── package.json ├── pages │ ├── _app.js │ └── index.js ├── public │ ├── delete.png │ ├── favicon.ico │ └── vercel.svg └── styles │ └── styles.css ├── mvnw ├── mvnw.cmd ├── pom.xml ├── screenshots ├── demo.gif ├── do-spaces-dashboard.png └── gallery.png └── src └── main ├── java └── io │ └── thepro │ └── dospaces │ ├── DoSpacesApplication.java │ ├── configs │ ├── DoConfig.java │ └── RestSecurityFilter.java │ ├── controllers │ ├── ImageStorageController.java │ └── ViewController.java │ ├── entities │ └── Image.java │ ├── repositories │ └── ImageRepository.java │ └── services │ ├── ImageStorageService.java │ └── ImageStorageServiceImpl.java └── resources ├── application.yaml └── data.sql /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | .project 3 | .classpath 4 | /.settings/ 5 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Gladius Thayalarajan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot AWS S3 / Digital Ocean Object Spaces 2 | 3 | DigitalOcean Spaces is one of the cheapest object storage alternatives to other popular cloud services like AWS S3 and Google cloud storage. To integrate with the DO spaces API uses AWS's S3 SDK, meaning this tutorial is interchangeable. This demo works with AWS S3 and DigitalOcean Spaces. 4 | 5 | This **Spring Boot Starter** is perfect for implementing file/image management feature in an application using DigitalOcean Spaces or AWS S3. 6 | 7 | Read the full post at 8 | 9 | [![Open with ThePro](https://thepro.io/button.svg)](https://thepro.io/post/spring-boot-and-digitalocean-spaces-for-file-storage-Q1) 10 | 11 | ### Demo UI 12 | 13 | ![Image of demo UI Png](https://raw.githubusercontent.com/gladius-thayalarajan/spring-boot-digital-ocean-spaces/master/screenshots/gallery.png) 14 | 15 | ### Digital Ocean Spaces Dashboard 16 | 17 | ![Image of DO spaces dashboard](https://raw.githubusercontent.com/gladius-thayalarajan/spring-boot-digital-ocean-spaces/master/screenshots/do-spaces-dashboard.png) 18 | 19 | ![Image of demo UI Gif](https://raw.githubusercontent.com/gladius-thayalarajan/spring-boot-digital-ocean-spaces/master/screenshots/demo.gif) 20 | 21 | ## Author 22 | 23 | 👤 **Gladius Thayalarajan** 24 | 25 | - Website: [thepro.io/@/gladius](https://thepro.io/@/gladius) 26 | - Github: [github.com/gladius](https://github.com/gladius) 27 | 28 | ## Show your support 29 | 30 | Give a ⭐️ if this project helped you! 31 | 32 | ## License 33 | 34 | This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details 35 | -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env.local 29 | .env.development.local 30 | .env.test.local 31 | .env.production.local 32 | 33 | # vercel 34 | .vercel 35 | 36 | yarn.lock 37 | -------------------------------------------------------------------------------- /demo/README.md: -------------------------------------------------------------------------------- 1 | This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). 2 | 3 | ## Getting Started 4 | 5 | First, run the development server: 6 | 7 | ```bash 8 | npm run dev 9 | # or 10 | yarn dev 11 | ``` 12 | 13 | Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. 14 | 15 | You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file. 16 | 17 | ## Learn More 18 | 19 | To learn more about Next.js, take a look at the following resources: 20 | 21 | - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. 22 | - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. 23 | 24 | You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! 25 | 26 | ## Deploy on Vercel 27 | 28 | The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/import?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. 29 | 30 | Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. 31 | -------------------------------------------------------------------------------- /demo/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spaces-demo", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start" 9 | }, 10 | "dependencies": { 11 | "axios": "^0.21.1", 12 | "next": "^11.0.0", 13 | "react": "^17.0.2", 14 | "react-dom": "^17.0.2" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /demo/pages/_app.js: -------------------------------------------------------------------------------- 1 | import "../styles/styles.css"; 2 | 3 | function MyApp({ Component, pageProps }) { 4 | return ; 5 | } 6 | 7 | export default MyApp; 8 | -------------------------------------------------------------------------------- /demo/pages/index.js: -------------------------------------------------------------------------------- 1 | import Head from "next/head"; 2 | import { useEffect, useState } from "react"; 3 | import axios from "axios"; 4 | 5 | const Gallery = () => { 6 | const [loading, setLoading] = useState(true); 7 | const [images, setImages] = useState([]); 8 | const [progress, setProgress] = useState(0); 9 | 10 | const SERVER_URL = process.env.NEXT_PUBLIC_SERVER_URL; 11 | 12 | useEffect(() => { 13 | loadImages(); 14 | }, []); 15 | 16 | const loadImages = () => { 17 | setLoading(true); 18 | axios 19 | .get(`${SERVER_URL}/get/images`) 20 | .then((response) => { 21 | setImages(response.data); 22 | setLoading(false); 23 | }) 24 | .catch((error) => { 25 | console.log(error); 26 | }); 27 | }; 28 | 29 | const deleteImage = (id) => { 30 | setLoading(true); 31 | axios 32 | .delete(`${SERVER_URL}/delete/image/${id}`) 33 | .then((response) => { 34 | loadImages(); 35 | }) 36 | .catch((error) => { 37 | console.log(error); 38 | }); 39 | }; 40 | 41 | const uploadFile = (image) => { 42 | var formData = new FormData(); 43 | formData.append("image", image); 44 | let config = { 45 | onUploadProgress: (progressEvent) => { 46 | setProgress( 47 | Math.round((progressEvent.loaded * 100) / progressEvent.total) 48 | ); 49 | }, 50 | }; 51 | axios 52 | .put(`${SERVER_URL}/save/image`, formData, config) 53 | .then((response) => { 54 | loadImages(); 55 | setProgress(0); 56 | }) 57 | .catch((error) => { 58 | console.log(error); 59 | }); 60 | }; 61 | 62 | return ( 63 |
64 | 65 | Digital Ocean gallery 66 | 71 | 72 |
73 |
74 |
75 |
76 |
77 | uploadFile(e.target.files[0])} 81 | id="customFile" 82 | name="filename" 83 | /> 84 | 87 |
88 |
89 |
90 |
91 | {progress != 0 && ( 92 |
93 |
94 |
102 | {progress}% 103 |
104 |
105 |
106 | )} 107 |
108 |
109 | {loading ? ( 110 |
111 |
116 | Loading... 117 |
118 |
119 | ) : ( 120 |
121 | {images.length == 0 ? ( 122 |
123 |

Gallery is Empty

124 |
125 | ) : ( 126 |
127 | {images.map((image, key) => ( 128 |
129 | 138 | {image.name} 143 |
144 | {image.name} 145 |
146 |
147 | ))} 148 |
149 | )} 150 |
151 | )} 152 |
153 |
154 |
155 | ); 156 | }; 157 | 158 | export default Gallery; 159 | -------------------------------------------------------------------------------- /demo/public/delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gladius/spring-boot-digital-ocean-spaces/93c648cd1c2888c6c3d5859edce2c7b87324ee61/demo/public/delete.png -------------------------------------------------------------------------------- /demo/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gladius/spring-boot-digital-ocean-spaces/93c648cd1c2888c6c3d5859edce2c7b87324ee61/demo/public/favicon.ico -------------------------------------------------------------------------------- /demo/public/vercel.svg: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /demo/styles/styles.css: -------------------------------------------------------------------------------- 1 | body{ 2 | margin: 0; 3 | font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji"; 4 | font-size: 1rem; 5 | font-weight: 400; 6 | line-height: 1.5; 7 | color: #212529; 8 | text-align: left; 9 | background-color: #fff; 10 | } 11 | 12 | .image-container{ 13 | margin-top: 15px; 14 | margin-bottom: 15px; 15 | } 16 | 17 | .image { 18 | border: none; 19 | margin: auto; 20 | width: 100%; 21 | border-radius: 3px; 22 | height: 200px; 23 | object-fit: cover; 24 | } 25 | .image-delete { 26 | position: absolute; 27 | top: 0; 28 | right: 15px; 29 | } 30 | 31 | .image-caption { 32 | position: absolute; 33 | bottom: 0; 34 | left: 50%; 35 | transform: translate(-50%, 0); 36 | color: #ffffff; 37 | background: rgb(0, 0, 0, 0.5); 38 | border-radius: 5px; 39 | padding: 2px 10px; 40 | font-weight: 700; 41 | text-transform: capitalize; 42 | } 43 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Migwn, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 204 | echo $MAVEN_PROJECTBASEDIR 205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 206 | 207 | # For Cygwin, switch paths to Windows format before running java 208 | if $cygwin; then 209 | [ -n "$M2_HOME" ] && 210 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 211 | [ -n "$JAVA_HOME" ] && 212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 213 | [ -n "$CLASSPATH" ] && 214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 215 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 217 | fi 218 | 219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 220 | 221 | exec "$JAVACMD" \ 222 | $MAVEN_OPTS \ 223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 226 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 84 | @REM Fallback to current working directory if not found. 85 | 86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 88 | 89 | set EXEC_DIR=%CD% 90 | set WDIR=%EXEC_DIR% 91 | :findBaseDir 92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 93 | cd .. 94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 95 | set WDIR=%CD% 96 | goto findBaseDir 97 | 98 | :baseDirFound 99 | set MAVEN_PROJECTBASEDIR=%WDIR% 100 | cd "%EXEC_DIR%" 101 | goto endDetectBaseDir 102 | 103 | :baseDirNotFound 104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 105 | cd "%EXEC_DIR%" 106 | 107 | :endDetectBaseDir 108 | 109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 110 | 111 | @setlocal EnableExtensions EnableDelayedExpansion 112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 114 | 115 | :endReadAdditionalConfig 116 | 117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 118 | 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 123 | if ERRORLEVEL 1 goto error 124 | goto end 125 | 126 | :error 127 | set ERROR_CODE=1 128 | 129 | :end 130 | @endlocal & set ERROR_CODE=%ERROR_CODE% 131 | 132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 136 | :skipRcPost 137 | 138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 140 | 141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 142 | 143 | exit /B %ERROR_CODE% 144 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.thepro.digitalocean.spaces 8 | do-object-storage 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | do-object-storage 13 | Demo project for digital ocean spaces 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 2.5.1 19 | 20 | 21 | 22 | 23 | UTF-8 24 | UTF-8 25 | 1.8 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-data-jpa 36 | compile 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-devtools 41 | runtime 42 | 43 | 44 | com.amazonaws 45 | aws-java-sdk-s3 46 | 1.12.9 47 | 48 | 49 | org.projectlombok 50 | lombok 51 | true 52 | 53 | 54 | commons-io 55 | commons-io 56 | 2.8.0 57 | 58 | 59 | com.h2database 60 | h2 61 | runtime 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-starter-test 66 | test 67 | 68 | 69 | 70 | 71 | 72 | 73 | org.springframework.boot 74 | spring-boot-maven-plugin 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /screenshots/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gladius/spring-boot-digital-ocean-spaces/93c648cd1c2888c6c3d5859edce2c7b87324ee61/screenshots/demo.gif -------------------------------------------------------------------------------- /screenshots/do-spaces-dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gladius/spring-boot-digital-ocean-spaces/93c648cd1c2888c6c3d5859edce2c7b87324ee61/screenshots/do-spaces-dashboard.png -------------------------------------------------------------------------------- /screenshots/gallery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gladius/spring-boot-digital-ocean-spaces/93c648cd1c2888c6c3d5859edce2c7b87324ee61/screenshots/gallery.png -------------------------------------------------------------------------------- /src/main/java/io/thepro/dospaces/DoSpacesApplication.java: -------------------------------------------------------------------------------- 1 | package io.thepro.dospaces; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DoSpacesApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(DoSpacesApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/io/thepro/dospaces/configs/DoConfig.java: -------------------------------------------------------------------------------- 1 | package io.thepro.dospaces.configs; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | import com.amazonaws.auth.AWSStaticCredentialsProvider; 8 | import com.amazonaws.auth.BasicAWSCredentials; 9 | import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration; 10 | import com.amazonaws.services.s3.AmazonS3; 11 | import com.amazonaws.services.s3.AmazonS3ClientBuilder; 12 | 13 | @Configuration 14 | public class DoConfig { 15 | 16 | @Value("${do.space.key}") 17 | private String doSpaceKey; 18 | 19 | @Value("${do.space.secret}") 20 | private String doSpaceSecret; 21 | 22 | @Value("${do.space.endpoint}") 23 | private String doSpaceEndpoint; 24 | 25 | @Value("${do.space.region}") 26 | private String doSpaceRegion; 27 | 28 | @Bean 29 | public AmazonS3 getS3() { 30 | BasicAWSCredentials creds = new BasicAWSCredentials(doSpaceKey, doSpaceSecret); 31 | return AmazonS3ClientBuilder.standard() 32 | .withEndpointConfiguration(new EndpointConfiguration(doSpaceEndpoint, doSpaceRegion)) 33 | .withCredentials(new AWSStaticCredentialsProvider(creds)).build(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/io/thepro/dospaces/configs/RestSecurityFilter.java: -------------------------------------------------------------------------------- 1 | package io.thepro.dospaces.configs; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.Filter; 6 | import javax.servlet.FilterChain; 7 | import javax.servlet.ServletException; 8 | import javax.servlet.ServletRequest; 9 | import javax.servlet.ServletResponse; 10 | import javax.servlet.http.HttpServletRequest; 11 | import javax.servlet.http.HttpServletResponse; 12 | 13 | import org.springframework.core.Ordered; 14 | import org.springframework.core.annotation.Order; 15 | import org.springframework.stereotype.Component; 16 | 17 | @Component 18 | @Order(Ordered.HIGHEST_PRECEDENCE) 19 | public class RestSecurityFilter implements Filter { 20 | 21 | public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 22 | throws IOException, ServletException { 23 | HttpServletResponse response = (HttpServletResponse) res; 24 | HttpServletRequest request = (HttpServletRequest) req; 25 | response.setHeader("Access-Control-Allow-Methods", "*"); 26 | response.setHeader("Access-Control-Allow-Origin", "*"); 27 | response.setHeader("Access-Control-Allow-Credentials", "true"); 28 | response.setHeader("Access-Control-Allow-Headers", "*"); 29 | if ("OPTIONS".equalsIgnoreCase(request.getMethod())) { 30 | response.setStatus(HttpServletResponse.SC_OK); 31 | } else { 32 | chain.doFilter(req, res); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/io/thepro/dospaces/controllers/ImageStorageController.java: -------------------------------------------------------------------------------- 1 | package io.thepro.dospaces.controllers; 2 | 3 | import java.io.IOException; 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.DeleteMapping; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.PutMapping; 11 | import org.springframework.web.bind.annotation.RequestParam; 12 | import org.springframework.web.bind.annotation.RestController; 13 | import org.springframework.web.multipart.MultipartFile; 14 | 15 | import io.thepro.dospaces.entities.Image; 16 | import io.thepro.dospaces.services.ImageStorageService; 17 | 18 | @RestController 19 | public class ImageStorageController { 20 | 21 | @Autowired 22 | ImageStorageService service; 23 | 24 | @GetMapping("/get/images") 25 | public List getImages() { 26 | return service.getImage(); 27 | } 28 | 29 | @PutMapping("/save/image") 30 | public void saveImage(@RequestParam(value = "image", required = true) MultipartFile image) throws IOException { 31 | service.saveFile(image); 32 | } 33 | 34 | @DeleteMapping("/delete/image/{fileId}") 35 | public void deleteById(@PathVariable("fileId") Long fileId) throws Exception { 36 | service.deleteFile(fileId); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/io/thepro/dospaces/controllers/ViewController.java: -------------------------------------------------------------------------------- 1 | package io.thepro.dospaces.controllers; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | 6 | @Controller 7 | public class ViewController { 8 | 9 | @GetMapping("/") 10 | public String index() { 11 | return "index.html"; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/io/thepro/dospaces/entities/Image.java: -------------------------------------------------------------------------------- 1 | package io.thepro.dospaces.entities; 2 | 3 | import java.sql.Timestamp; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | 10 | import lombok.Data; 11 | 12 | @Entity 13 | @Data 14 | public class Image { 15 | 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.IDENTITY) 18 | private Long id; 19 | private String name; 20 | private String ext; 21 | private Timestamp createdtime; 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/io/thepro/dospaces/repositories/ImageRepository.java: -------------------------------------------------------------------------------- 1 | package io.thepro.dospaces.repositories; 2 | 3 | import org.springframework.data.repository.PagingAndSortingRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import io.thepro.dospaces.entities.Image; 7 | 8 | @Repository 9 | public interface ImageRepository extends PagingAndSortingRepository{ 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/io/thepro/dospaces/services/ImageStorageService.java: -------------------------------------------------------------------------------- 1 | package io.thepro.dospaces.services; 2 | 3 | import java.io.IOException; 4 | import java.util.List; 5 | 6 | import org.springframework.web.multipart.MultipartFile; 7 | 8 | import io.thepro.dospaces.entities.Image; 9 | 10 | public interface ImageStorageService { 11 | 12 | void saveFile(MultipartFile multipartFile) throws IOException; 13 | 14 | void deleteFile(Long id) throws Exception; 15 | 16 | List getImage(); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/io/thepro/dospaces/services/ImageStorageServiceImpl.java: -------------------------------------------------------------------------------- 1 | package io.thepro.dospaces.services; 2 | 3 | import java.io.IOException; 4 | import java.sql.Timestamp; 5 | import java.util.Date; 6 | import java.util.List; 7 | import java.util.Optional; 8 | 9 | import org.apache.commons.io.FilenameUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.beans.factory.annotation.Value; 12 | import org.springframework.stereotype.Service; 13 | import org.springframework.web.multipart.MultipartFile; 14 | 15 | import com.amazonaws.services.s3.AmazonS3; 16 | import com.amazonaws.services.s3.model.CannedAccessControlList; 17 | import com.amazonaws.services.s3.model.DeleteObjectRequest; 18 | import com.amazonaws.services.s3.model.ObjectMetadata; 19 | import com.amazonaws.services.s3.model.PutObjectRequest; 20 | 21 | import io.thepro.dospaces.entities.Image; 22 | import io.thepro.dospaces.repositories.ImageRepository; 23 | 24 | @Service 25 | public class ImageStorageServiceImpl implements ImageStorageService { 26 | 27 | @Autowired 28 | ImageRepository imageRepo; 29 | 30 | @Autowired 31 | AmazonS3 s3Client; 32 | 33 | 34 | @Value("${do.space.bucket}") 35 | private String doSpaceBucket; 36 | 37 | String FOLDER = "files/"; 38 | 39 | @Override 40 | public void saveFile(MultipartFile multipartFile) throws IOException { 41 | String extension = FilenameUtils.getExtension(multipartFile.getOriginalFilename()); 42 | String imgName = FilenameUtils.removeExtension(multipartFile.getOriginalFilename()); 43 | String key = FOLDER + imgName + "." + extension; 44 | saveImageToServer(multipartFile, key); 45 | Image image = new Image(); 46 | image.setName(imgName); 47 | image.setExt(extension); 48 | image.setCreatedtime(new Timestamp(new Date().getTime())); 49 | imageRepo.save(image); 50 | } 51 | 52 | @Override 53 | public void deleteFile(Long fileId) throws Exception { 54 | Optional imageOpt = imageRepo.findById(fileId); 55 | if (imageOpt.get() != null) { 56 | Image image = imageOpt.get(); 57 | String key = FOLDER + image.getName() + "." + image.getExt(); 58 | s3Client.deleteObject(new DeleteObjectRequest(doSpaceBucket, key)); 59 | imageRepo.delete(image); 60 | } 61 | } 62 | 63 | private void saveImageToServer(MultipartFile multipartFile, String key) throws IOException { 64 | ObjectMetadata metadata = new ObjectMetadata(); 65 | metadata.setContentLength(multipartFile.getInputStream().available()); 66 | if (multipartFile.getContentType() != null && !"".equals(multipartFile.getContentType())) { 67 | metadata.setContentType(multipartFile.getContentType()); 68 | } 69 | s3Client.putObject(new PutObjectRequest(doSpaceBucket, key, multipartFile.getInputStream(), metadata) 70 | .withCannedAcl(CannedAccessControlList.PublicRead)); 71 | } 72 | 73 | @Override 74 | public List getImage() { 75 | return (List) imageRepo.findAll(); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | ## DO properties 2 | do: 3 | spaces: 4 | key: ${DO_SPACE_KEY} 5 | secret: ${DO_SPACE_SECRET} 6 | endpoint: ${DO_SPACE_ENDPOINT} 7 | region: ${DO_SPACE_REGION} 8 | bucket: ${DO_SPACE_BUCKET} 9 | 10 | 11 | ## Database Properties 12 | spring: 13 | datasource: 14 | url: jdbc:h2:file:${PATH_TO_DB_FILE};DB_CLOSE_ON_EXIT=FALSE 15 | driverClassName: org.h2.Driver 16 | username: sa 17 | password: password 18 | jpa: 19 | database-platform: org.hibernate.dialect.H2Dialect 20 | hibernate: 21 | ddl-auto: update 22 | h2: 23 | console: 24 | enabled: true -------------------------------------------------------------------------------- /src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS files; 2 |   3 | CREATE TABLE files ( 4 |   id INT AUTO_INCREMENT  PRIMARY KEY, 5 |   name VARCHAR(250) NOT NULL, 6 |   ext VARCHAR(50) NOT NULL, 7 |   createdtime timestamp NULL 8 | ); --------------------------------------------------------------------------------