Skip to content

Instantly share code, notes, and snippets.

@piyusht007
Created March 28, 2018 12:01
Show Gist options
  • Save piyusht007/258a378a94fa8fadad67b31b69e4e60e to your computer and use it in GitHub Desktop.
Save piyusht007/258a378a94fa8fadad67b31b69e4e60e to your computer and use it in GitHub Desktop.
Writing a file to S3 bucket using pre-signed URL
public S3FileWriter {
public String write(final ResultSet resultSet) {
HttpURLConnection connection = null;
try {
StringBuilder csvContent = new StringBuilder();
connection = httpURLConnectionHelper.newHttpURLConnection("<PRESIGNED_PUT_URL_FOR_FILE>");
try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
BufferedWriter writer = new BufferedWriter(out)
){
// Iterate over result set and fetch the record and write that record.
writer.write(record);
writer.flush();
// This is mandatory for the contect to be written to file.
final int responseCode = connection.getResponseCode();
if (responseCode != HttpStatus.SC_OK) {
throw new Exception();
}
}
} catch (Exception e) {
throw e;
} finally {
if (connection != null) {
connection.disconnect();
}
}
// Writing is complete lets generate presigned URL for file download using AWS S3 client.
// Close the ResultSet
return presignedURLForDownload;
}
}
@AstroMan7
Copy link

AstroMan7 commented Jan 26, 2023

what is httpURLConnectionHelper?

I guessed thats a class that I would have to implement, but why them is it written without capital letter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment