package de.jeyp91; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3ObjectInputStream; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class S3Provider { private static final Regions AWS_DEFAULT_REGION = Regions.EU_CENTRAL_1; private static final String BUCKET_NAME = "tlw-database-tool-api-football-data"; private void writeToS3(String filename, String content) { final AmazonS3 s3 = AmazonS3ClientBuilder .standard() .withRegion(AWS_DEFAULT_REGION) .build(); try { s3.putObject(BUCKET_NAME, filename, content); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } } public void writeFixturesToS3(int league, String content) { writeToS3("fixtures/" + league + ".json", content); } public void writeRoundsToS3(int league, String content) { writeToS3("rounds/" + league + ".json", content); } private String getFileFromS3(String filename) { final AmazonS3 s3 = AmazonS3ClientBuilder .standard() .withRegion(AWS_DEFAULT_REGION) .build(); StringBuilder builder = new StringBuilder(); try { S3Object o = s3.getObject(BUCKET_NAME, filename); S3ObjectInputStream s3is = o.getObjectContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(s3is)); String line; while ((line = reader.readLine()) != null) { builder.append(line); } } catch (SdkClientException | IOException e) { e.printStackTrace(); } return builder.toString(); } String getFixturesStringFromS3(int league) { return getFileFromS3("fixtures/" + league + ".json"); } public JSONObject getFixturesJSONFromS3(int league) { String fixturesString = getFixturesStringFromS3(league); JSONParser parser = new JSONParser(); JSONObject jsonObject = null; try { jsonObject = (JSONObject) parser.parse(fixturesString); } catch (ParseException e) { /* TODO */ e.printStackTrace(); } return jsonObject; } private String getRoundsStringFromS3(int league) { return getFileFromS3("rounds/" + league + ".json"); } public JSONObject getRoundsJSONFromS3(int league) { String fixturesString = getRoundsStringFromS3(league); JSONParser parser = new JSONParser(); JSONObject jsonObject = null; try { jsonObject = (JSONObject) parser.parse(fixturesString); } catch (ParseException e) { /* TODO */ e.printStackTrace(); } return jsonObject; } }