I have a file in Amazon S3
in bucket ABCD
. I have 3 objects ("folderA/folderB/folderC/abcd.csv")
which are folders and in the final folder I have a .csv
file (abcd.csv)
. I have used a logic to convert it to JSON
and load it back into another file which is a .txt
file in the same folder ("folderA/folderB/folderC/abcd.txt")
. I had to download the file locally in order to do that. How would I read the file directly and write it back to the text file. The code which I have used to write to a file in S3 is below and I need to read a file from S3.
InputStream inputStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_16));
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(json.length());
PutObjectRequest request = new PutObjectRequest(bucketPut, filePut, inputStream, metadata);
s3.putObject(request);
In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.
Reading objects without downloading them Similarly, if you want to upload and read small pieces of textual data such as quotes, tweets, or news articles, you can do that using the S3 resource method put(), as demonstrated in the example below (Gist).
First you should get the object InputStream
to do your need.
S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, key));
InputStream objectData = object.getObjectContent();
Pass the InputStream
, File Name
and the path
to the below method to download 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 Download your object read the file and make it to JSON
and write it to .txt
file after that you can upload the txt
file to the desired bucket in S3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With