The open blogging platform. Say no to algorithms and paywalls.

How to Create a CSV File on an Amazon S3 bucket without storing it on a local machine?

A guide to creating a CSV file on an Amazon S3 bucket without storing it on a local machine.

Amazon S3

Many times you need to create and write CSV file at runtime.

You can do this with the help of AWS SDK.

Java example:

public void writeFileToS3(String s3BucketName, String dataToWrite, String resFileKey) {   
    try {  
        ObjectMetadata objectMetadata = new ObjectMetadata();  
        objectMetadata.setContentType("test/csv");  
  
        PutObjectResult putObjectResult = s3Client.putObject(s3BucketName,   
                resFileKey,  
                new ByteArrayInputStream(dataToWrite.getBytes(StandardCharsets.UTF_8))  
                , objectMetadata);  
  
    } catch (AmazonS3Exception ex) {  
        // handle exception  
    }  
}

s3BucketName = AWS S3 bucket name. Example: sample-csv-files-bucket.

dataToWrite = Data string you want to write in CSV file.

resFileKey = It is complete path of your file location to save. Example: sample-csv-files-bucket/csv-files-1/{your_file_name}.csv

Thank you for reading.

Subscribe to get notified about upcoming articles.




Continue Learning