Class S3Service

java.lang.Object
callofproject.dev.project.service.S3Service

@Service public class S3Service extends Object
Service class for managing files with Amazon S3. This class provides functionalities to upload, download, and delete files from Amazon S3 buckets.
  • Constructor Details

    • S3Service

      public S3Service(com.amazonaws.services.s3.AmazonS3 s3Client)
      Constructor for the S3Service class. It is used to inject dependencies into the service.
      Parameters:
      s3Client - The AmazonS3 object to be injected.
  • Method Details

    • uploadToS3AndGetUrl

      public String uploadToS3AndGetUrl(org.springframework.web.multipart.MultipartFile multipartFile, String fileName)
      Uploads a file to Amazon S3 and returns its URL.
      Parameters:
      multipartFile - The file to be uploaded as a MultipartFile.
      fileName - The name to be assigned to the file on S3.
      Returns:
      The URL of the uploaded file.
    • uploadToS3WithMultiPartFile

      public String uploadToS3WithMultiPartFile(org.springframework.web.multipart.MultipartFile multipartFile, String fileName)
      Uploads a file to Amazon S3 using MultipartFile and deletes any existing files with similar names.
      Parameters:
      multipartFile - The file to be uploaded as a MultipartFile.
      fileName - The name to be assigned to the file on S3.
      Returns:
      A success message upon successful upload.
    • uploadToS3WithFile

      public String uploadToS3WithFile(File file, String fileName)
      Uploads a file to Amazon S3 using a File object.
      Parameters:
      file - The file to be uploaded.
      fileName - The name to be assigned to the file on S3.
      Returns:
      A success message upon successful upload.
    • downloadFromS3

      public byte[] downloadFromS3(String fileName)
      Downloads a file from Amazon S3 and returns its byte array.
      Parameters:
      fileName - The name of the file to be downloaded from S3.
      Returns:
      The byte array of the downloaded file.
    • getImage

      @Cacheable(value="getImage", key="#fileName") public String getImage(String fileName)
      Retrieves an image from Amazon S3, encodes it in Base64, and returns the encoded string. This method is cached to improve performance for repeated requests.
      Parameters:
      fileName - The name of the image file to be retrieved from S3.
      Returns:
      The Base64 encoded string of the image.
    • deleteFromS3

      public String deleteFromS3(String fileName)
      Deletes a file from Amazon S3.
      Parameters:
      fileName - The name of the file to be deleted from S3.
      Returns:
      A success message upon successful deletion.