-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added image generation and s3 uploads
- Loading branch information
1 parent
8456a7c
commit 06463bb
Showing
18 changed files
with
562 additions
and
66 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
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
46 changes: 46 additions & 0 deletions
46
src/main/java/com/ko/footballupdater/configuration/AmazonS3Properties.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,46 @@ | ||
package com.ko.footballupdater.configuration; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.regions.Regions; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Getter | ||
@Setter | ||
@Component | ||
@ConfigurationProperties(prefix = "aws.s3") | ||
public class AmazonS3Properties { | ||
|
||
@NotNull | ||
private boolean enabled; | ||
|
||
@NotNull | ||
private String accessKey; | ||
|
||
@NotNull | ||
private String secretKey; | ||
|
||
@NotNull | ||
private String bucketName; | ||
|
||
@Bean | ||
public AmazonS3 s3Client() { | ||
// Set up S3 client | ||
log.info("Initialising S3 client"); | ||
Regions clientRegion = Regions.AP_SOUTHEAST_2; | ||
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(clientRegion) | ||
.withCredentials(new AWSStaticCredentialsProvider(credentials)) | ||
.build(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/ko/footballupdater/configuration/ImageGeneratorProperies.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,24 @@ | ||
package com.ko.footballupdater.configuration; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Getter | ||
@Setter | ||
@Component | ||
@ConfigurationProperties(prefix = "image.generator") | ||
public class ImageGeneratorProperies { | ||
|
||
@NotNull | ||
private boolean enabled; | ||
|
||
@NotNull | ||
private String inputPath; | ||
|
||
@NotNull | ||
private String outputPath; | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/ko/footballupdater/configuration/InstagramPostProperies.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,18 @@ | ||
package com.ko.footballupdater.configuration; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Getter | ||
@Setter | ||
@Component | ||
@ConfigurationProperties(prefix = "ig.post") | ||
public class InstagramPostProperies { | ||
|
||
@NotNull | ||
private int version; | ||
|
||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/ko/footballupdater/models/ImageStatEntry.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,13 @@ | ||
package com.ko.footballupdater.models; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.AllArgsConstructor; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
public class ImageStatEntry { | ||
private String name; | ||
private String value; | ||
} |
46 changes: 9 additions & 37 deletions
46
src/main/java/com/ko/footballupdater/models/InstagramPost.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 |
---|---|---|
@@ -1,50 +1,22 @@ | ||
package com.ko.footballupdater.models; | ||
|
||
import com.ko.footballupdater.utils.PostHelper; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
public class InstagramPost { | ||
|
||
private Player player; | ||
private PlayerMatchPerformanceStats playerMatchPerformanceStats; | ||
private String caption; | ||
private String imageSearchUrl; | ||
private List<String> imagesFileNames = new ArrayList<>(); | ||
private List<String> imagesS3Urls = new ArrayList<>(); | ||
|
||
public InstagramPost(Player player, PlayerMatchPerformanceStats playerMatchPerformanceStats) { | ||
this.player = player; | ||
this.playerMatchPerformanceStats = playerMatchPerformanceStats; | ||
this.caption = PostHelper.generatePostDefaultPlayerCaption(player, playerMatchPerformanceStats); | ||
this.imageSearchUrl = PostHelper.generatePostImageSearchUrl(player, playerMatchPerformanceStats); | ||
} | ||
|
||
public Player getPlayer() { | ||
return player; | ||
} | ||
|
||
public void setPlayer(Player player) { | ||
this.player = player; | ||
} | ||
|
||
public PlayerMatchPerformanceStats getPlayerMatchPerformanceStats() { | ||
return playerMatchPerformanceStats; | ||
} | ||
|
||
public void setPlayerMatchPerformanceStats(PlayerMatchPerformanceStats playerMatchPerformanceStats) { | ||
this.playerMatchPerformanceStats = playerMatchPerformanceStats; | ||
} | ||
|
||
public String getCaption() { | ||
return caption; | ||
} | ||
|
||
public void setCaption(String caption) { | ||
this.caption = caption; | ||
} | ||
|
||
public String getImageSearchUrl() { | ||
return imageSearchUrl; | ||
} | ||
|
||
public void setImageSearchUrl(String imageSearchUrl) { | ||
this.imageSearchUrl = imageSearchUrl; | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
src/main/java/com/ko/footballupdater/repositories/PlayerRepository.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
60 changes: 60 additions & 0 deletions
60
src/main/java/com/ko/footballupdater/services/AmazonS3Service.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,60 @@ | ||
package com.ko.footballupdater.services; | ||
|
||
import com.amazonaws.AmazonServiceException; | ||
import com.amazonaws.SdkClientException; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.ko.footballupdater.configuration.AmazonS3Properties; | ||
import com.ko.footballupdater.configuration.ImageGeneratorProperies; | ||
import com.ko.footballupdater.models.InstagramPost; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.File; | ||
|
||
@Slf4j | ||
@Service | ||
public class AmazonS3Service { | ||
|
||
@Autowired | ||
private ImageGeneratorProperies imageGeneratorProperies; | ||
|
||
@Autowired | ||
private AmazonS3Properties amazonS3Properties; | ||
|
||
@Autowired | ||
private AmazonS3 s3Client; | ||
|
||
public void uploadtoS3(InstagramPost post) { | ||
if (!amazonS3Properties.isEnabled()) { | ||
return; | ||
} | ||
if (!post.getImagesFileNames().isEmpty()) { | ||
try { | ||
// Upload images and save urls | ||
// Overwrites any file with the same name | ||
for (String imageFileName : post.getImagesFileNames()) { | ||
String filePath = imageGeneratorProperies.getOutputPath() + imageFileName; | ||
PutObjectRequest request = new PutObjectRequest(amazonS3Properties.getBucketName(),imageFileName, new File(filePath)) | ||
.withCannedAcl(CannedAccessControlList.PublicRead); | ||
s3Client.putObject(request); | ||
String imageUrl = s3Client.getUrl(amazonS3Properties.getBucketName(), imageFileName).toString(); | ||
log.atInfo().setMessage(post.getPlayer().getName() + " - Successfully uploaded image " + imageFileName + " to S3 @ " + imageUrl).log(); | ||
post.getImagesS3Urls().add(imageUrl); | ||
} | ||
} catch (AmazonServiceException ex) { | ||
// The call was transmitted successfully, but Amazon S3 couldn't process | ||
// it, so it returned an error response. | ||
log.warn(post.getPlayer().getName() + " - Error attempting to upload", ex); | ||
} catch (SdkClientException ex) { | ||
// Amazon S3 couldn't be contacted for a response, or the client | ||
// couldn't parse the response from Amazon S3. | ||
log.warn(post.getPlayer().getName() + " - Error attempting to upload", ex); | ||
} | ||
} else { | ||
log.atInfo().setMessage(post.getPlayer().getName() + " - No images to upload").log(); | ||
} | ||
} | ||
} |
Oops, something went wrong.