-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #603 from bounswe/feature/BE-591-backend-handle-im…
…age-upload #591 Implemented Image upload to resources using amazon s3 bucket.
- Loading branch information
Showing
14 changed files
with
174 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
resq/backend/resq/src/main/java/com/groupa1/resq/config/AmazonClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.groupa1.resq.config; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import jakarta.annotation.PostConstruct; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
@Service | ||
public class AmazonClient { | ||
|
||
private AmazonS3 s3client; | ||
@Value("${amazonProperties.endpointUrl}") | ||
private String endpointUrl; | ||
|
||
@Value("${amazonProperties.bucketName}") | ||
private String bucketName; | ||
|
||
@Value("${amazonProperties.accessKey}") | ||
private String accessKey; | ||
|
||
@Value("${amazonProperties.secretKey}") | ||
private String secretKey; | ||
|
||
@PostConstruct | ||
private void initializeAmazon() { | ||
AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey); | ||
this.s3client = new AmazonS3Client(credentials); | ||
} | ||
|
||
private File convertMultiPartToFile(MultipartFile file) throws IOException { | ||
File convFile = new File(file.getOriginalFilename()); | ||
FileOutputStream fos = new FileOutputStream(convFile); | ||
fos.write(file.getBytes()); | ||
fos.close(); | ||
return convFile; | ||
} | ||
|
||
private String generateFileName(MultipartFile multiPart) { | ||
return new Date().getTime() + "-" + multiPart.getOriginalFilename().replace(" ", "_"); | ||
} | ||
|
||
private void uploadFileTos3bucket(String fileName, File file) { | ||
s3client.putObject(new PutObjectRequest(bucketName, fileName, file) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
} | ||
|
||
public com.groupa1.resq.entity.File uploadFile(MultipartFile multipartFile) { | ||
com.groupa1.resq.entity.File fileEntity = new com.groupa1.resq.entity.File(); | ||
String fileUrl = ""; | ||
String fileName = generateFileName(multipartFile); | ||
try { | ||
File file = convertMultiPartToFile(multipartFile); | ||
fileUrl = endpointUrl + "/" + bucketName + "/" + fileName; | ||
uploadFileTos3bucket(fileName, file); | ||
file.delete(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
fileEntity.setFileName(fileName); | ||
fileEntity.setFileUrl(fileUrl); | ||
fileEntity.setFileType(multipartFile.getContentType()); | ||
return fileEntity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
resq/backend/resq/src/main/java/com/groupa1/resq/dto/FileDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.groupa1.resq.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class FileDto { | ||
private String fileName; | ||
private String fileUrl; | ||
private String fileType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
resq/backend/resq/src/main/java/com/groupa1/resq/entity/File.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.groupa1.resq.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Data; | ||
|
||
@Entity | ||
@Data | ||
public class File { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String fileName; | ||
|
||
private String fileType; | ||
|
||
private String fileUrl; | ||
|
||
@OneToOne(mappedBy = "file") | ||
private Resource resource; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
resq/backend/resq/src/main/java/com/groupa1/resq/repository/FileRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.groupa1.resq.repository; | ||
|
||
import com.groupa1.resq.entity.File; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface FileRepository extends JpaRepository<File, Long> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters