Search in sources :

Example 6 with S3Service

use of com.formkiq.aws.s3.S3Service in project formkiq-core by formkiq.

the class DocumentVersionsRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    SimpleDateFormat df = new SimpleDateFormat(GsonUtil.DATE_FORMAT);
    String siteId = authorizer.getSiteId();
    String documentId = event.getPathParameters().get("documentId");
    String next = getParameter(event, "next");
    String tz = getParameter(event, "tz");
    ZoneOffset offset = DateUtil.getZoneOffset(tz);
    df.setTimeZone(TimeZone.getTimeZone(offset));
    String s3key = createDatabaseKey(siteId, documentId);
    S3Service s3service = awsservice.s3Service();
    try (S3Client s3 = s3service.buildClient()) {
        ListObjectVersionsResponse response = s3service.getObjectVersions(s3, awsservice.documents3bucket(), s3key, next);
        List<ApiDocumentVersion> list = response.versions().stream().map(v -> {
            ApiDocumentVersion dv = new ApiDocumentVersion();
            dv.setVersionId(v.versionId());
            Date date = Date.from(v.lastModified());
            dv.setLastModifiedDate(df.format(date));
            return dv;
        }).collect(Collectors.toList());
        ApiDocumentVersionsResponse resp = new ApiDocumentVersionsResponse();
        resp.setNext(response.nextKeyMarker());
        resp.setVersions(list);
        return new ApiRequestHandlerResponse(SC_OK, resp);
    }
}
Also used : Date(java.util.Date) TimeZone(java.util.TimeZone) S3Client(software.amazon.awssdk.services.s3.S3Client) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) SimpleDateFormat(java.text.SimpleDateFormat) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) Collectors(java.util.stream.Collectors) SiteIdKeyGenerator.createDatabaseKey(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.createDatabaseKey) List(java.util.List) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) GsonUtil(com.formkiq.lambda.apigateway.util.GsonUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) DateUtil(com.formkiq.stacks.dynamodb.DateUtil) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) ApiDocumentVersionsResponse(com.formkiq.stacks.api.ApiDocumentVersionsResponse) S3Service(com.formkiq.aws.s3.S3Service) ApiDocumentVersion(com.formkiq.stacks.api.ApiDocumentVersion) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse) ApiDocumentVersionsResponse(com.formkiq.stacks.api.ApiDocumentVersionsResponse) ApiDocumentVersion(com.formkiq.stacks.api.ApiDocumentVersion) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) SimpleDateFormat(java.text.SimpleDateFormat) S3Service(com.formkiq.aws.s3.S3Service) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse) Date(java.util.Date) ZoneOffset(java.time.ZoneOffset)

Example 7 with S3Service

use of com.formkiq.aws.s3.S3Service in project modeldb by VertaAI.

the class ServiceSet method initializeArtifactStore.

private static ArtifactStoreService initializeArtifactStore(MDBArtifactStoreConfig mdbArtifactStoreConfig) throws ModelDBException, IOException {
    // ------------- Start Initialize Cloud storage base on configuration ------------------
    ArtifactStoreService artifactStoreService;
    if (mdbArtifactStoreConfig.getArtifactEndpoint() != null) {
        System.getProperties().put("artifactEndpoint.storeArtifact", mdbArtifactStoreConfig.getArtifactEndpoint().getStoreArtifact());
        System.getProperties().put("artifactEndpoint.getArtifact", mdbArtifactStoreConfig.getArtifactEndpoint().getGetArtifact());
    }
    if (mdbArtifactStoreConfig.getNFS() != null && mdbArtifactStoreConfig.getNFS().getArtifactEndpoint() != null) {
        System.getProperties().put("artifactEndpoint.storeArtifact", mdbArtifactStoreConfig.getNFS().getArtifactEndpoint().getStoreArtifact());
        System.getProperties().put("artifactEndpoint.getArtifact", mdbArtifactStoreConfig.getNFS().getArtifactEndpoint().getGetArtifact());
    }
    switch(mdbArtifactStoreConfig.getArtifactStoreType()) {
        case "S3":
            if (!mdbArtifactStoreConfig.S3.getS3presignedURLEnabled()) {
                System.setProperty(ModelDBConstants.CLOUD_BUCKET_NAME, mdbArtifactStoreConfig.S3.getCloudBucketName());
                System.getProperties().put(SCAN_PACKAGES, "ai.verta.modeldb.artifactStore.storageservice.s3");
                startSpringServer();
                artifactStoreService = App.getInstance().applicationContext.getBean(S3Service.class);
            } else {
                artifactStoreService = new S3Service(mdbArtifactStoreConfig.S3.getCloudBucketName());
                System.getProperties().put(SCAN_PACKAGES, "dummyPackageName");
                startSpringServer();
            }
            break;
        case "NFS":
            String rootDir = mdbArtifactStoreConfig.getNFS().getNfsRootPath();
            LOGGER.trace("NFS server root path {}", rootDir);
            System.getProperties().put("file.upload-dir", rootDir);
            System.getProperties().put(SCAN_PACKAGES, "ai.verta.modeldb.artifactStore.storageservice.nfs");
            startSpringServer();
            artifactStoreService = App.getInstance().applicationContext.getBean(NFSService.class);
            break;
        default:
            throw new ModelDBException("Configure valid artifact store name in config.yaml file.");
    }
    // ------------- Finish Initialize Cloud storage base on configuration ------------------
    LOGGER.info("ArtifactStore service initialized and resolved storage dependency before server start");
    return artifactStoreService;
}
Also used : ModelDBException(ai.verta.modeldb.common.exceptions.ModelDBException) ArtifactStoreService(ai.verta.modeldb.artifactStore.storageservice.ArtifactStoreService) S3Service(ai.verta.modeldb.artifactStore.storageservice.s3.S3Service) NFSService(ai.verta.modeldb.artifactStore.storageservice.nfs.NFSService)

Example 8 with S3Service

use of com.formkiq.aws.s3.S3Service in project formkiq-core by formkiq.

the class DocumentsS3UpdateTest method beforeClass.

/**
 * Before Class.
 *
 * @throws URISyntaxException URISyntaxException
 * @throws InterruptedException InterruptedException
 * @throws IOException IOException
 */
@BeforeAll
public static void beforeClass() throws URISyntaxException, InterruptedException, IOException {
    Region region = Region.US_EAST_1;
    AwsCredentialsProvider cred = StaticCredentialsProvider.create(AwsSessionCredentials.create("ACCESSKEY", "SECRETKEY", "TOKENKEY"));
    localstack.start();
    snsBuilder = new SnsConnectionBuilder().setEndpointOverride(localstack.getEndpointOverride(Service.SNS).toString()).setRegion(region).setCredentials(cred);
    sqsBuilder = new SqsConnectionBuilder().setEndpointOverride(localstack.getEndpointOverride(Service.SQS).toString()).setRegion(region).setCredentials(cred);
    sqsService = new SqsService(sqsBuilder);
    s3Builder = new S3ConnectionBuilder().setEndpointOverride(localstack.getEndpointOverride(Service.S3).toString()).setRegion(region).setCredentials(cred);
    dbBuilder = DynamoDbTestServices.getDynamoDbConnection(null);
    service = new DocumentServiceImpl(dbBuilder, "Documents");
    if (!sqsService.exists(ERROR_SQS_QUEUE)) {
        sqsService.createQueue(ERROR_SQS_QUEUE);
    }
    if (!sqsService.exists(SNS_SQS_CREATE_QUEUE)) {
        sqsDocumentEventUrl = sqsService.createQueue(SNS_SQS_CREATE_QUEUE).queueUrl();
    }
    s3service = new S3Service(s3Builder);
    try (S3Client s3 = s3service.buildClient()) {
        s3service.createBucket(s3, "example-bucket");
    }
    snsService = new SnsService(snsBuilder);
    snsDocumentEvent = snsService.createTopic("createDocument1").topicArn();
    snsService.subscribe(snsDocumentEvent, "sqs", sqsDocumentEventUrl);
    dbHelper = new DynamoDbHelper(dbBuilder);
    if (!dbHelper.isDocumentsTableExists()) {
        dbHelper.createDocumentsTable();
        dbHelper.createCacheTable();
    }
}
Also used : DocumentServiceImpl(com.formkiq.stacks.dynamodb.DocumentServiceImpl) DynamoDbHelper(com.formkiq.stacks.dynamodb.DynamoDbHelper) SqsConnectionBuilder(com.formkiq.aws.sqs.SqsConnectionBuilder) SnsService(com.formkiq.aws.sns.SnsService) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) SqsService(com.formkiq.aws.sqs.SqsService) S3ConnectionBuilder(com.formkiq.aws.s3.S3ConnectionBuilder) Region(software.amazon.awssdk.regions.Region) SnsConnectionBuilder(com.formkiq.aws.sns.SnsConnectionBuilder) S3Client(software.amazon.awssdk.services.s3.S3Client) S3Service(com.formkiq.aws.s3.S3Service) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 9 with S3Service

use of com.formkiq.aws.s3.S3Service in project formkiq-core by formkiq.

the class AbstractAwsTest method beforeClass.

/**
 * beforeclass.
 *
 * @throws IOException IOException
 */
@BeforeClass
public static void beforeClass() throws IOException {
    awsregion = Region.of(System.getProperty("testregion"));
    String awsprofile = System.getProperty("testprofile");
    appenvironment = System.getProperty("testappenvironment");
    final SqsConnectionBuilder sqsConnection = new SqsConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    final SsmConnectionBuilder ssmBuilder = new SsmConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    final S3ConnectionBuilder s3Builder = new S3ConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    final SnsConnectionBuilder snsBuilder = new SnsConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    sqsService = new SqsService(sqsConnection);
    s3Service = new S3Service(s3Builder);
    ssmService = new SsmServiceImpl(ssmBuilder);
    snsService = new SnsService(snsBuilder);
    sesbucketname = ssmService.getParameterValue("/formkiq/" + appenvironment + "/s3/DocumentsSesS3Bucket");
    documentsbucketname = ssmService.getParameterValue("/formkiq/" + appenvironment + "/s3/DocumentsS3Bucket");
    stagingdocumentsbucketname = ssmService.getParameterValue("/formkiq/" + appenvironment + "/s3/DocumentsStageS3Bucket");
    snsDocumentEventArn = ssmService.getParameterValue("/formkiq/" + appenvironment + "/sns/DocumentEventArn");
    String documentsTable = ssmService.getParameterValue("/formkiq/" + appenvironment + "/dynamodb/DocumentsTableName");
    DynamoDbConnectionBuilder dbConnection = new DynamoDbConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    documentService = new DocumentServiceImpl(dbConnection, documentsTable);
}
Also used : DocumentServiceImpl(com.formkiq.stacks.dynamodb.DocumentServiceImpl) SsmServiceImpl(com.formkiq.aws.ssm.SsmServiceImpl) SqsConnectionBuilder(com.formkiq.aws.sqs.SqsConnectionBuilder) SsmConnectionBuilder(com.formkiq.aws.ssm.SsmConnectionBuilder) SnsService(com.formkiq.aws.sns.SnsService) S3ConnectionBuilder(com.formkiq.aws.s3.S3ConnectionBuilder) SqsService(com.formkiq.aws.sqs.SqsService) SnsConnectionBuilder(com.formkiq.aws.sns.SnsConnectionBuilder) S3Service(com.formkiq.aws.s3.S3Service) DynamoDbConnectionBuilder(com.formkiq.stacks.dynamodb.DynamoDbConnectionBuilder) BeforeClass(org.junit.BeforeClass)

Example 10 with S3Service

use of com.formkiq.aws.s3.S3Service in project formkiq-core by formkiq.

the class EmailSendingTest method beforeClass.

/**
 * beforeclass.
 *
 * @throws IOException IOException
 */
@BeforeClass
public static void beforeClass() throws IOException {
    Region awsregion = Region.of(System.getProperty("testregion"));
    String awsprofile = System.getProperty("testprofile");
    final SqsConnectionBuilder sqsConnection = new SqsConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    final SsmConnectionBuilder ssmBuilder = new SsmConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    final SnsConnectionBuilder snsBuilder = new SnsConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    final S3ConnectionBuilder s3Builder = new S3ConnectionBuilder().setCredentials(awsprofile).setRegion(awsregion);
    s3Service = new S3Service(s3Builder);
    sqsService = new SqsService(sqsConnection);
    SsmService ssmService = new SsmServiceImpl(ssmBuilder);
    snsService = new SnsService(snsBuilder);
    String app = System.getProperty("testappenvironment");
    snsDocumentEmailArn = ssmService.getParameterValue("/formkiq/" + app + "/sns/DocumentsEmailNotificationArn");
    stagingdocumentsbucketname = ssmService.getParameterValue("/formkiq/" + app + "/s3/DocumentsStageS3Bucket");
}
Also used : SsmService(com.formkiq.aws.ssm.SsmService) SsmServiceImpl(com.formkiq.aws.ssm.SsmServiceImpl) SqsConnectionBuilder(com.formkiq.aws.sqs.SqsConnectionBuilder) SsmConnectionBuilder(com.formkiq.aws.ssm.SsmConnectionBuilder) SnsService(com.formkiq.aws.sns.SnsService) S3ConnectionBuilder(com.formkiq.aws.s3.S3ConnectionBuilder) SqsService(com.formkiq.aws.sqs.SqsService) Region(software.amazon.awssdk.regions.Region) SnsConnectionBuilder(com.formkiq.aws.sns.SnsConnectionBuilder) S3Service(com.formkiq.aws.s3.S3Service) BeforeClass(org.junit.BeforeClass)

Aggregations

S3Service (com.formkiq.aws.s3.S3Service)11 S3Client (software.amazon.awssdk.services.s3.S3Client)8 S3ConnectionBuilder (com.formkiq.aws.s3.S3ConnectionBuilder)6 SnsConnectionBuilder (com.formkiq.aws.sns.SnsConnectionBuilder)4 SnsService (com.formkiq.aws.sns.SnsService)4 SqsConnectionBuilder (com.formkiq.aws.sqs.SqsConnectionBuilder)4 SqsService (com.formkiq.aws.sqs.SqsService)4 SsmServiceImpl (com.formkiq.aws.ssm.SsmServiceImpl)4 BeforeClass (org.junit.BeforeClass)4 Region (software.amazon.awssdk.regions.Region)4 SsmConnectionBuilder (com.formkiq.aws.ssm.SsmConnectionBuilder)3 DocumentServiceImpl (com.formkiq.stacks.dynamodb.DocumentServiceImpl)3 AwsCredentialsProvider (software.amazon.awssdk.auth.credentials.AwsCredentialsProvider)3 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)2 ArtifactStoreService (ai.verta.modeldb.artifactStore.storageservice.ArtifactStoreService)1 NFSService (ai.verta.modeldb.artifactStore.storageservice.nfs.NFSService)1 S3Service (ai.verta.modeldb.artifactStore.storageservice.s3.S3Service)1 ModelDBException (ai.verta.modeldb.common.exceptions.ModelDBException)1 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)1 S3ObjectMetadata (com.formkiq.aws.s3.S3ObjectMetadata)1