Я запускаю лямбду, которая запускается на событии S3 (написано и развернуто в c # / Visual Studio 2019).
- Есть ли способ передать
IAmazonDynamoDB
в мою функцию ? Прямо сейчас, когда я пытаюсь передать его через конструктор, он всегда равен нулю
"Из коробки" public Function(IAmazonS3 s3Client)
, очевидно, работает с s3Client.
Когда я добавляю: Function(IAmazonS3 s3Client, IAmazonDynamoDB dynamoDB)
- DynamoDB всегда нулевой). Это то же самое, если я добавлю IDynamoDBContext
.
Если нет другого способа получить экземпляр, должен ли он быть создан в самой лямбде? В этом случае у меня есть следующее ..
public class Function
{
private readonly string _accessKey;
private readonly string _secretKey;
private readonly string _serviceUrl;
IAmazonS3 S3Client { get; set; }
private IAmazonDynamoDB dynamoDb { get; set; }
private const string TableName = "xxxx";
/// <summary>
/// Default constructor. This constructor is used by Lambda to construct the instance. When invoked in a Lambda environment
/// the AWS credentials will come from the IAM role associated with the function and the AWS region will be set to the
/// region the Lambda function is executed in.
/// </summary>
public Function()
{
S3Client = new AmazonS3Client();
}
/// <summary>
/// Constructs an instance with a preconfigured S3 client. This can be used for testing the outside of the Lambda environment.
/// </summary>
/// <param name="s3Client"></param>
public Function(IAmazonS3 s3Client)
{
this.S3Client = s3Client;
_accessKey = Environment.GetEnvironmentVariable("AccessKey");
_secretKey = Environment.GetEnvironmentVariable("SecretKey");
_serviceUrl = Environment.GetEnvironmentVariable("ServiceURL");
}
} public async Task FunctionHandler(S3Event evnt, ILambdaContext context)
{
dynamoDb = new AmazonDynamoDBClient(
new BasicAWSCredentials(_accessKey, _secretKey),
new AmazonDynamoDBConfig
{
ServiceURL = _serviceUrl,
RegionEndpoint = S3Client.Config.RegionEndpoint
});}using (DynamoDBContext dynamoContext = new DynamoDBContext(dynamoDb))
{
myData.PK = PK;
myData.SK = SK;
dynamoContext.SaveAsync(mydata);
}
Было бы здорово иметь рабочий / не нулевой IDynamoDbContext
, чтобы я мог SaveAsync(myData)
.