<commit_before>package com.mobica.aws.s3.storage;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.util.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class S3Service {

    private static final String BUCKET_NAME = "opendoors17-dev-test";

    private final AmazonS3 amazonS3;

    public S3Service(AmazonS3 amazonS3) {
        this.amazonS3 = amazonS3;
    }


    public Optional<String> getFileContent(String filename) {
        try (S3Object objectPortion = amazonS3.getObject(new GetObjectRequest(BUCKET_NAME, filename))) {
            return Optional.of(IOUtils.toString(objectPortion.getObjectContent()));
        } catch (IOException ex) {
            return Optional.empty();
        }
    }

    public List<String> getAllFiles() {
        ObjectListing objectListing = amazonS3.listObjects(new ListObjectsRequest().withBucketName(BUCKET_NAME));
        return objectListing.getObjectSummaries()
                .stream()
                .map(S3ObjectSummary::getKey)
                .collect(Collectors.toList());
    }

    public void saveFile(MultipartFile multipart) throws IOException {
        String filename = multipart.getOriginalFilename();
        Path write = Files.write(Paths.get(filename), multipart.getBytes());
        File file = write.toFile();

        // Upload file
        amazonS3.putObject(new PutObjectRequest(BUCKET_NAME, filename, file));

        file.delete();
    }

}<commit_msg>[LBN][aws-s3] Updated file for workshops<commit_after>package com.mobica.aws.s3.storage;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.util.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class S3Service {

    private static final String BUCKET_NAME = "opendoors17-dev-test";

    private final AmazonS3 amazonS3;

    public S3Service(AmazonS3 amazonS3) {
        this.amazonS3 = amazonS3;
    }


    public Optional<String> getFileContent(String filename) {

        // TODO Create object of S3Object by use amazonS3.getObject and GetObjectRequest
        try (S3Object objectPortion /* = */) {
            return Optional.of(IOUtils.toString(objectPortion.getObjectContent()));
        } catch (IOException ex) {
            return Optional.empty();
        }
    }

    public List<String> getAllFiles() {

        // TODO Create object of ObjectListing by use amazonS3.listObjects
        ObjectListing objectListing /* = */;
        return objectListing.getObjectSummaries()
                .stream()
                .map(S3ObjectSummary::getKey)
                .collect(Collectors.toList());
    }

    public void saveFile(MultipartFile multipart) throws IOException {
        String fileName = multipart.getOriginalFilename();
        Path write = Files.write(Paths.get(fileName), multipart.getBytes());
        File fileToUpload = write.toFile();

        // TODO Use amazonS3.putObject with new PutObjectRequest

        fileToUpload.delete();
    }

}
{'fraction_non_alphanumeric': 0.1026497970876104, 'fraction_numerical': 0.01289090475053712, 'mean_word_length': 3.5150862068965516, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '826797', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1249, 'n_words': 235}