Skip to content

Instantly share code, notes, and snippets.

@Blind-Striker
Last active August 13, 2019 20:45
Show Gist options
  • Save Blind-Striker/39ce4733ec309eb0ef0c34e9f5cbfb69 to your computer and use it in GitHub Desktop.
Save Blind-Striker/39ce4733ec309eb0ef0c34e9f5cbfb69 to your computer and use it in GitHub Desktop.
Sample for LocalStack article
public async Task<APIGatewayProxyResponse> CreateProfileAsync(APIGatewayProxyRequest request, ILambdaContext context)
{
WriteVariables(context);
string requestBody = request.Body;
context.Logger.LogLine($"Request Body\n {requestBody}");
AddProfileModel addProfileModel = JsonConvert.DeserializeObject<AddProfileModel>(requestBody);
context.Logger.LogLine($"Decoding Base64 image");
var bytes = Convert.FromBase64String(addProfileModel.ProfilePicBase64);
var fileTransferUtility = new TransferUtility(_awsS3Client);
using (var ms = new MemoryStream(bytes))
{
context.Logger.LogLine($"Uploading {addProfileModel.ProfilePicName} to {BucketName}");
await fileTransferUtility.UploadAsync(ms, BucketName, addProfileModel.ProfilePicName);
}
context.Logger.LogLine($"Adding profile to DynamoDb");
await _awsDynamoDbClient.PutItemAsync(TableName, new Dictionary<string, AttributeValue>()
{
{nameof(AddProfileModel.Id), new AttributeValue(addProfileModel.Id)},
{nameof(AddProfileModel.Name), new AttributeValue(addProfileModel.Name)},
{nameof(AddProfileModel.Email), new AttributeValue(addProfileModel.Email)},
{nameof(AddProfileModel.ProfilePicName), new AttributeValue(addProfileModel.ProfilePicName)}
});
var response = new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Body = "Created",
Headers = new Dictionary<string, string> { { "Content-Type", "text/plain" } }
};
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment