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 15, 2024
1 parent 93669e6 commit ce125aa
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 25 deletions.
1 change: 0 additions & 1 deletion attribution-data-file-share/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ dependencies {
implementation 'software.amazon.awssdk:s3-transfer-manager:2.25.7'
implementation 'software.amazon.awssdk.crt:aws-crt:0.29.11'
implementation 'org.postgresql:postgresql:42.7.2'
implementation 'software.amazon.awssdk:s3:2.21.7'
implementation 'software.amazon.awssdk:ssm:2.25.7'
implementation 'software.amazon.awssdk:sts:2.25.6'
implementation project(path: ':lambda-lib')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
try (var dbConnection = DriverManager.getConnection(parameterStore.getDbHost(), parameterStore.getDbUser(), parameterStore.getDbPassword())) {

helper.copyDataToFile(dbConnection);
helper.mtpUpload(getAsyncS3Client(ENDPOINT, parameterStore));
helper.uploadToS3(getAsyncS3Client(ENDPOINT, parameterStore));

} catch (NullPointerException | URISyntaxException | SQLException ex) {
throwAttributionDataShareException(logger, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ void copyDataToFile(Connection connection) {
String date = new SimpleDateFormat(EFFECTIVE_DATE_PATTERN).format(new Date());
try (var stmt = connection.createStatement();
var writer = new BufferedWriter(new FileWriter(fileFullPath, true))) {
long startSelect = System.currentTimeMillis();
var rs = getExecuteQuery(stmt);
long finishSelect = System.currentTimeMillis();

logger.log("Select TIME ms: ---------- " + (finishSelect - startSelect));
var rs = getExecuteQuery(stmt);

long startWrite = System.currentTimeMillis();
writer.write(FIRST_LINE + date);
writer.newLine();
long records = 0;
Expand All @@ -51,8 +47,6 @@ void copyDataToFile(Connection connection) {
}
writer.write(LAST_LINE + date + String.format("%010d", records));

long finishWrite = System.currentTimeMillis();
logger.log("Write TIME ms: ---------- " + (finishWrite - startWrite));
} catch (SQLException | IOException ex) {
String errorMessage = "An error occurred while exporting data to a file. ";
logger.log(errorMessage + ex.getMessage());
Expand All @@ -74,11 +68,9 @@ String getResponseLine(String currentMbi, Timestamp effectiveDate, Boolean optOu
return result.toString();
}

public String mtpUpload(S3AsyncClient s3AsyncClient) {
public String uploadToS3(S3AsyncClient s3AsyncClient) {
String currentDate = new SimpleDateFormat(REQ_FILE_NAME_PATTERN).format(new Date());
var key = REQ_FILE_NAME + currentDate;
logger.log("MTPU KEY " + key);
long startUpload = System.currentTimeMillis();
S3TransferManager transferManager = S3TransferManager.builder()
.s3Client(s3AsyncClient)
.build();
Expand All @@ -92,9 +84,6 @@ public String mtpUpload(S3AsyncClient s3AsyncClient) {
FileUpload fileUpload = transferManager.uploadFile(uploadFileRequest);

CompletedFileUpload uploadResult = fileUpload.completionFuture().join();

long finishUpload = System.currentTimeMillis();
logger.log("Multipart Upload TIME ms: ---------- " + (finishUpload - startUpload));
return uploadResult.response().eTag();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ class AttributionDataShareHandlerTest {

@Test
void attributionDataShareInvoke() {
var mockParameterStore = mockStatic(AttributionParameterStore.class);
var mockParameterStore = mockStatic(AttributionParameterStore.class);
mockParameterStore
.when(AttributionParameterStore::getParameterStore)
.thenReturn(parameterStore);

Connection dbConnection = mock(Connection.class);
mockStatic(DriverManager.class)
.when(() -> DriverManager.getConnection(anyString(), anyString(), anyString())).thenReturn(dbConnection);
.when(() -> DriverManager.getConnection(anyString(), anyString(), anyString())).thenReturn(dbConnection);

when(handler.helperInit(anyString(), anyString(), any(LambdaLogger.class))).thenReturn(helper);
assertDoesNotThrow(() -> handler.handleRequest(null, System.out, new TestContext()));
Expand All @@ -49,8 +49,8 @@ void attributionDataShareExceptionTest() {
assertThrows(AttributionDataShareException.class, () -> handler.throwAttributionDataShareException(LOGGER, ex));
}

// @Test
// void getS3ClientTest() throws URISyntaxException {
// assertNotNull(handler.getS3Client(TEST_ENDPOINT, parameterStore));
// }
@Test
void getS3ClientTest() throws URISyntaxException {
assertNotNull(handler.getAsyncS3Client(TEST_ENDPOINT, parameterStore));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void getResponseLineTest() {
@Test
void writeFileToFinalDestinationTest() throws IOException {
createTestFile();
helper.writeFileToFinalDestination(S3MockAPIExtension.S3_CLIENT);
helper.uploadToS3(S3MockAPIExtension.S3_CLIENT);
assertTrue(S3MockAPIExtension.isObjectExists(FILE_NAME));
S3MockAPIExtension.deleteFile(FILE_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.findify.s3mock.S3Mock;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.model.*;

import java.net.URI;
Expand All @@ -14,7 +14,7 @@
public class S3MockAPIExtension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {

private static final S3Mock API = S3Mock.create(8001, "/tmp/s3");
public static S3Client S3_CLIENT;
public static S3AsyncClient S3_CLIENT;
private static boolean STARTED = false;

@Override
Expand All @@ -25,7 +25,7 @@ public void beforeAll(ExtensionContext context) throws Exception {

API.start();

S3_CLIENT = S3Client.builder()
S3_CLIENT = S3AsyncClient.crtBuilder()
.region(S3_REGION)
.endpointOverride(new URI(TEST_ENDPOINT))
.build();
Expand Down

0 comments on commit ce125aa

Please sign in to comment.