Skip to content

Commit

Permalink
multi part upload
Browse files Browse the repository at this point in the history
  • Loading branch information
smirnovaae committed Mar 14, 2024
1 parent f39ab70 commit e5253fd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
1 change: 1 addition & 0 deletions attribution-data-file-share/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repositories {
dependencies {
implementation 'com.amazonaws:aws-lambda-java-core:1.2.2'
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.529'
implementation 'software.amazon.awssdk:s3-transfer-manager:2.25.7'
implementation 'org.postgresql:postgresql:42.7.2'
implementation 'software.amazon.awssdk:s3:2.21.7'
implementation 'software.amazon.awssdk:ssm:2.25.7'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
long start = System.currentTimeMillis();

var content = AttributionDataShareHelper.getFileContent(dbConnection, logger);
AttributionDataShareHelper.writeFileToS3Bucket(content, fileName, getS3Client(ENDPOINT, parameterStore), logger);
AttributionDataShareHelper.multiPartUploadFileToS3Bucket(content, fileName, getS3Client(ENDPOINT, parameterStore), logger);

long finish = System.currentTimeMillis();
logger.log("TIME ms: ---------- " + (finish - start));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,43 @@
import com.amazonaws.services.s3.model.AmazonS3Exception;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.*;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static gov.cms.ab2d.attributionDataShare.AttributionDataShareConstants.*;

public class AttributionDataShareHelper {

static String getFileContent(Connection connection, LambdaLogger logger) {
static ArrayList<String> getFileContent(Connection connection, LambdaLogger logger) {
var result = new ArrayList<String>();
var date = new SimpleDateFormat(EFFECTIVE_DATE_PATTERN).format(new Date());
var content = new StringBuilder();
try (var stmt = connection.createStatement()) {
var rs = getExecuteQuery(stmt);
content.append(FIRST_LINE).append(date).append(LINE_SEPARATOR);
long records = 0;
result.add(FIRST_LINE + date + LINE_SEPARATOR);
int records = 0;
long totalRecords = 0;
String chunkLine = "";
while (rs.next()) {
var line = getResponseLine(rs.getString(1), rs.getTimestamp(2), rs.getBoolean(3));
content.append(line).append(LINE_SEPARATOR);
var line = getResponseLine(
rs.getString(1), rs.getTimestamp(2), rs.getBoolean(3)) + LINE_SEPARATOR;
chunkLine += line;
records++;
totalRecords++;
if (records == 32768) {
result.add(chunkLine);
chunkLine = "";
records = 0;
}
}
content.append(LAST_LINE).append(date).append(String.format("%010d", records));
return content.toString();
result.add(chunkLine);
result.add(LAST_LINE + date + String.format("%010d", totalRecords));
// content.append(LAST_LINE).append(date).append(String.format("%010d", totalRecords));
return result;
} catch (SQLException ex) {
String errorMessage = "An error occurred while exporting data to a file. ";
logger.log(errorMessage + ex.getMessage());
Expand Down Expand Up @@ -64,6 +77,48 @@ public static void writeFileToS3Bucket(String fileContent, String fileName, S3Cl
}
}

public static void multiPartUploadFileToS3Bucket(ArrayList<String> fileContent, String fileName, S3Client s3Client, LambdaLogger logger) {

// Initiate the multipart upload.
var createMultipartUploadResponse = s3Client.createMultipartUpload(b -> b
.bucket(getBucketName())
.key(getUploadPath() + fileName));
var uploadId = createMultipartUploadResponse.uploadId();
// Upload the parts of the file.

List<CompletedPart> completedParts = new ArrayList<>();
for (int partNumber = 1; partNumber < fileContent.size(); partNumber++) {

UploadPartRequest uploadPartRequest = UploadPartRequest.builder()
.bucket(getBucketName())
.key(getUploadPath() + fileName)
.uploadId(uploadId)
.partNumber(partNumber)
.build();

UploadPartResponse partResponse = s3Client.uploadPart(
uploadPartRequest,
RequestBody.fromString(fileContent.get(partNumber)));

CompletedPart part = CompletedPart.builder()
.partNumber(partNumber)
.eTag(partResponse.eTag())
.build();
completedParts.add(part);
}
CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder()
.bucket(getBucketName())
.key(getUploadPath() + fileName)
.uploadId(uploadId)
.multipartUpload(completedMultipartUpload -> completedMultipartUpload.parts(completedParts))
.build();

// Complete the multipart upload.
s3Client.completeMultipartUpload(completeMultipartUploadRequest);
}



public static String getBucketName() {
return System.getenv(BUCKET_NAME_PROP);
}
Expand Down

0 comments on commit e5253fd

Please sign in to comment.