You must first get an InputStream object to do your needs.
S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, key)); InputStream objectData = object.getObjectContent();
Pass the InputStream , File Name and path below method to load your stream.
public void saveFile(String fileName, String path, InputStream objectData) throws Exception { DataOutputStream dos = null; OutputStream out = null; try { File newDirectory = new File(path); if (!newDirectory.exists()) { newDirectory.mkdirs(); } File uploadedFile = new File(path, uploadFileName); out = new FileOutputStream(uploadedFile); byte[] fileAsBytes = new byte[inputStream.available()]; inputStream.read(fileAsBytes); dos = new DataOutputStream(out); dos.write(fileAsBytes); } catch (IOException io) { io.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (dos != null) { dos.close(); } } catch (IOException e) { e.printStackTrace(); } } }
After you upload your object, read the file and go to JSON and write it to the .txt file after that, you can load the txt file into the desired bucket in S3
source share