Hello world!
This is the second article about my experience at Makers building a social network clone. Link to the first article - https://dev.to/olnov/working-with-http-requests-in-spring-boot-5el0. The current one is dedicated to storing images for users' profiles.
Step #1. Planning.
As a part of our MVP, we decided to implement the following user story:
As a user,
I want to update my profile to include my photo,
So that other users can easily find me in the app
We also agreed on non-functional requirements.
- Image files are PNG or JPG only.
- File size should not exceed 1 MB.
Step #2. Analyze and Design.
We came up with different options for where to store images - remote server folder, database and object storage. Even though our application was not highly loaded, we decided to use S3 as the most appropriate solution for distributed systems. (P.S. and I had an existing iDrive e2 subscription from a previous project).
In the Database, there were only links to the public URL of iDrive storage.
Here is the DB diagram:
High-level app diagram:
Step #3. Build.
The backend part needed the following to be completed:
- Add AWS SDK to the project.
- Implement Image Service to work with external integration.
- Implement an Image Controller to put images into a bucket using the Image Service and save the corresponding record in the DB.
In pom.xml I added the following:
<dependency> <groupid>software.amazon.awssdk</groupid> <artifactid>s3</artifactid> <version>2.28.18</version> </dependency>
ImageService Class:
package com.makersacademy.acebook.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutObjectRequest; import software.amazon.awssdk.services.s3.model.PutObjectResponse; import java.io.InputStream; import java.net.URI; import java.util.UUID; @Service public class ImageService { private final S3Client s3Client; @Value("${idrive.e2.bucket-name}") private String bucketName; @Value("${idrive.e2.endpoint}") private String endpoint; @Value("${idrive.e2.public_endpoint}") private String public_endpoint; public ImageService(@Value("${idrive.e2.access-key}") String accessKey, @Value("${idrive.e2.secret-key}") String secretKey, @Value("${idrive.e2.endpoint}") String endpoint) { AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey); this.s3Client = S3Client.builder() .credentialsProvider(StaticCredentialsProvider.create(credentials)) .endpointOverride(URI.create(endpoint)) .region(Region.AWS_GLOBAL) // I dind't see exact instructions from iDrive, so I set this parameter to AWS_GLOBAL .build(); } public String uploadImage(InputStream imageStream, long contentLength, String contentType) { String key = "media/" + UUID.randomUUID(); PutObjectRequest putObjectRequest = PutObjectRequest.builder() .bucket(bucketName) .key(key) .contentType(contentType) .contentLength(contentLength) // Specify the content length .build(); s3Client.putObject(putObjectRequest, software.amazon.awssdk.core.sync.RequestBody.fromInputStream(imageStream, contentLength)); // Return the image URL return public_endpoint + "/" + bucketName + "/" + key; } }
Idrive connection variables are stored in application.properties file:
#S3 storage settings idrive.e2.access-key=${E2_ACCESS_KEY} idrive.e2.secret-key=${E2_SECRET_KEY} idrive.e2.endpoint=${E2_ENDPOINT} idrive.e2.bucket-name=${E2_BUCKET_NAME} idrive.e2.public_endpoint=${E2_PUBLIC_ENDPOINT}
Ok, now ImageController Class:
package com.makersacademy.acebook.controller; import com.makersacademy.acebook.model.Post; import com.makersacademy.acebook.repository.PostRepository; import com.makersacademy.acebook.service.ImageService; import com.makersacademy.acebook.model.User; import com.makersacademy.acebook.repository.UserRepository; import jakarta.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.view.RedirectView; import java.io.IOException; @Controller public class ImageController { @Autowired private ImageService imageService; @Autowired private UserRepository userRepository; @Autowired private PostRepository postRepository; @PostMapping("/upload/{userId}") public RedirectView uploadImage(@PathVariable Long userId, @RequestParam("file") MultipartFile file) { try { // Check if the user exists User user = userRepository.findById(userId).orElseThrow(()->new RuntimeException("User not found")); // Upload image to IDrive e2 and get image URL String imageUrl = imageService.uploadImage(file.getInputStream(), file.getSize(), file.getContentType()); // Save the image URL in the user's profile user.setUser_photo(imageUrl); userRepository.save(user); return new RedirectView("/media/"+userId); } catch (IOException e) { throw new RuntimeException("Error uploading file:"+e.getMessage()); } } @PostMapping("/upload/post") public RedirectView uploadImageAsPost(@RequestParam("file") MultipartFile file, HttpSession session){ try { Long userId = (Long) session.getAttribute("user_id"); if (userId == null) { throw new RuntimeException("Not authorized"); } String imageUrl = imageService.uploadImage(file.getInputStream(), file.getSize(), file.getContentType()); User user = userRepository.findById(userId) .orElseThrow(() -> new RuntimeException("User not found")); Post post = new Post(imageUrl,user,true); postRepository.save(post); return new RedirectView("/"); } catch (IOException e) { throw new RuntimeException("Error uploading file:"+e.getMessage()); } } }
On the front end to upload the profile image I added the following:
<div class="mb-3"> <form id="image-form" th:action="@{/api/images/upload/{id}(id=${user.id})}" method="post" enctype="multipart/form-data"> Upload Image <input class="form-control" type="file" name="file" id="formFile" accept=".jpg,.jpeg,.png" onchange="autoSubmitImage()" hidden> </form> </div> <script th:inline="javascript"> /*<![CDATA[*/ const autoSubmitImage = ()=> { document.getElementById("image-form").submit(); } /*]]>*/ </script>
And the result.
This is it.
Cheers!
以上がSpring Boot と iDrive E3 を使用して UGC アプリの画像を保存するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

この記事では、Javaプロジェクト管理、自動化の構築、依存関係の解像度にMavenとGradleを使用して、アプローチと最適化戦略を比較して説明します。

この記事では、MavenやGradleなどのツールを使用して、適切なバージョン化と依存関係管理を使用して、カスタムJavaライブラリ(JARファイル)の作成と使用について説明します。

この記事では、カフェインとグアバキャッシュを使用してJavaでマルチレベルキャッシュを実装してアプリケーションのパフォーマンスを向上させています。セットアップ、統合、パフォーマンスの利点をカバーし、構成と立ち退きポリシー管理Best Pra

この記事では、キャッシュや怠zyなロードなどの高度な機能を備えたオブジェクトリレーショナルマッピングにJPAを使用することについて説明します。潜在的な落とし穴を強調しながら、パフォーマンスを最適化するためのセットアップ、エンティティマッピング、およびベストプラクティスをカバーしています。[159文字]

Javaのクラスロードには、ブートストラップ、拡張機能、およびアプリケーションクラスローダーを備えた階層システムを使用して、クラスの読み込み、リンク、および初期化が含まれます。親の委任モデルは、コアクラスが最初にロードされ、カスタムクラスのLOAに影響を与えることを保証します


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター
