Files
query-orchestration/internal/serviceconfig/textract/config.go
T
Michael McGuinness 752fb2e2c0 Merged in feature/chc_1 (pull request #120)
Text Extraction Clean Up + Parallelization

* merged_Call

* directchildren

* bitofimprovements

* go

* parallel

* fixfullsuite

* pondforconcurrency

* comments

* muchdone

* bitsofclean

* stabilisedtests

* snappy

* threadpooltests

* childelements

* testspassed
2025-04-25 17:02:50 +00:00

86 lines
2.4 KiB
Go

package textract
import (
"context"
"log/slog"
awsc "queryorchestration/internal/serviceconfig/aws"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/textract"
)
type ConfigProvider interface {
awsc.ConfigProvider
SetTextractClient(context.Context) error
SetTextractClientWithCfg(context.Context, aws.Config)
SetTextractClientWithProfile(context.Context, awsc.Profile) error
GetTextractClient() TextractClient
SetTextractEndpoint(string)
GetTextractEndpoint() string
}
type TextractConfig struct {
AWSEndpointUrlTextract string `env:"AWS_ENDPOINT_URL_TEXTRACT"`
AWSTextractAccessKeyId string `env:"AWS_ACCESS_KEY_ID_TEXTRACT"`
AWSTextractSecretAccessKey string `env:"AWS_SECRET_ACCESS_KEY_TEXTRACT"`
AWSTextractSessionToken string `env:"AWS_SESSION_TOKEN_TEXTRACT"`
AWSTextractRegion string `env:"AWS_REGION_TEXTRACT"`
TextractClient TextractClient
}
func (c *TextractConfig) SetTextractClientWithCfg(ctx context.Context, cfg aws.Config) {
c.TextractClient = textract.NewFromConfig(cfg)
}
func (c *TextractConfig) SetTextractClientWithProfile(ctx context.Context, profile awsc.Profile) error {
cfg, err := awsc.GetAWSConfigWithProfile(ctx, profile)
if err != nil {
return err
}
c.SetTextractClientWithCfg(ctx, cfg)
return nil
}
func (c *TextractConfig) SetTextractClient(ctx context.Context) error {
cfg, err := awsc.GetAWSConfigWithOpts(ctx, func(lo *config.LoadOptions) error {
if c.AWSEndpointUrlTextract != "" {
lo.BaseEndpoint = c.AWSEndpointUrlTextract
}
return nil
})
if err != nil {
return err
}
if c.AWSTextractAccessKeyId != "" && c.AWSTextractSecretAccessKey != "" {
cfg.Credentials = credentials.NewStaticCredentialsProvider(c.AWSTextractAccessKeyId, c.AWSTextractSecretAccessKey, c.AWSTextractSessionToken)
if c.AWSTextractRegion != "" {
cfg.Region = c.AWSTextractRegion
}
slog.Debug("textract specific credentials", "key_id", c.AWSTextractAccessKeyId, "region", c.AWSTextractRegion)
}
c.SetTextractClientWithCfg(ctx, cfg)
return nil
}
func (c *TextractConfig) GetTextractClient() TextractClient {
return c.TextractClient
}
func (c *TextractConfig) GetTextractEndpoint() string {
return c.AWSEndpointUrlTextract
}
func (c *TextractConfig) SetTextractEndpoint(e string) {
c.AWSEndpointUrlTextract = e
}