Skip to content

Commit fb7e4c3

Browse files
842234-LoadSaveAWSLambdaExample
1 parent 5d30f95 commit fb7e4c3

File tree

10 files changed

+278
-0
lines changed

10 files changed

+278
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36127.28 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication", "ConsoleApplication\ConsoleApplication.csproj", "{F20DFBF0-11CB-4F31-8A44-8CB5823CAC04}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F20DFBF0-11CB-4F31-8A44-8CB5823CAC04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F20DFBF0-11CB-4F31-8A44-8CB5823CAC04}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F20DFBF0-11CB-4F31-8A44-8CB5823CAC04}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F20DFBF0-11CB-4F31-8A44-8CB5823CAC04}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {B288F873-E9F3-4D93-AFDB-D00FAA275815}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<LangVersion>latest</LangVersion>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="AWSSDK.Core" Version="4.0.0.6" />
13+
<PackageReference Include="AWSSDK.Lambda" Version="4.0.0.4" />
14+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<None Update="Data\InputTemplate.xlsx">
19+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
</Project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Amazon.Lambda.Model;
2+
using Amazon.Lambda;
3+
using Newtonsoft.Json;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Amazon;
11+
12+
namespace ConsoleApplication
13+
{
14+
internal class Program
15+
{
16+
static async Task Main(string[] args)
17+
{
18+
//Create a new AmazonLambdaClient
19+
AmazonLambdaClient client = new AmazonLambdaClient("awsaccessKeyID", "awsSecreteAccessKey", RegionEndpoint.USEast1);
20+
21+
//Create new InvokeRequest with published function name.
22+
InvokeRequest invoke = new InvokeRequest
23+
{
24+
FunctionName = "MyNewFunction",
25+
InvocationType = InvocationType.RequestResponse,
26+
Payload = "\"Test\""
27+
};
28+
29+
// Get the InvokeResponse from client InvokeRequest.
30+
InvokeResponse response = await client.InvokeAsync(invoke);
31+
32+
//Read the response stream
33+
var stream = new StreamReader(response.Payload);
34+
JsonReader reader = new JsonTextReader(stream);
35+
var serilizer = new JsonSerializer();
36+
var responseText = serilizer.Deserialize(reader);
37+
38+
//Convert Base64String into Excel document
39+
byte[] bytes = Convert.FromBase64String(responseText.ToString());
40+
FileStream fileStream = new FileStream("Sample.xlsx", FileMode.Create);
41+
BinaryWriter writer = new BinaryWriter(fileStream);
42+
writer.Write(bytes, 0, bytes.Length);
43+
writer.Close();
44+
var psi = new System.Diagnostics.ProcessStartInfo
45+
{
46+
FileName = "Sample.xlsx",
47+
UseShellExecute = true
48+
};
49+
System.Diagnostics.Process.Start(psi);
50+
}
51+
}
52+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36127.28 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoadingandSaving", "LoadingandSaving\LoadingandSaving.csproj", "{1708DFA6-DE7C-1F3A-C633-5759BB11F4B8}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{1708DFA6-DE7C-1F3A-C633-5759BB11F4B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{1708DFA6-DE7C-1F3A-C633-5759BB11F4B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{1708DFA6-DE7C-1F3A-C633-5759BB11F4B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{1708DFA6-DE7C-1F3A-C633-5759BB11F4B8}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {D6092E92-E4CB-4BBB-B980-28B1F4F07B51}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Amazon.Lambda.Core;
2+
using static System.Net.Mime.MediaTypeNames;
3+
using Syncfusion.XlsIO;
4+
5+
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
6+
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
7+
8+
namespace LoadingandSaving;
9+
10+
public class Function
11+
{
12+
13+
/// <summary>
14+
/// A simple function that takes a string and does a ToUpper
15+
/// </summary>
16+
/// <param name="input">The event for the Lambda function handler to process.</param>
17+
/// <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param>
18+
/// <returns></returns>
19+
public string FunctionHandler(string input, ILambdaContext context)
20+
{
21+
using (ExcelEngine excelEngine = new ExcelEngine())
22+
{
23+
IApplication application = excelEngine.Excel;
24+
application.DefaultVersion = ExcelVersion.Xlsx;
25+
26+
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "Data", "InputTemplate.xlsx");
27+
FileStream excelStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
28+
IWorkbook workbook = application.Workbooks.Open(excelStream);
29+
30+
//Access first worksheet
31+
IWorksheet worksheet = workbook.Worksheets[0];
32+
33+
//Set text in cell A3
34+
worksheet.Range["A3"].Text = "Hello World";
35+
36+
//Save to MemoryStream
37+
MemoryStream outputStream = new MemoryStream();
38+
workbook.SaveAs(outputStream);
39+
outputStream.Position = 0;
40+
41+
//Return as Base64 string
42+
return Convert.ToBase64String(outputStream.ToArray());
43+
}
44+
}
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
7+
<AWSProjectType>Lambda</AWSProjectType>
8+
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
9+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
10+
<!-- Generate ready to run images during publishing to improve cold start time. -->
11+
<PublishReadyToRun>true</PublishReadyToRun>
12+
</PropertyGroup>
13+
<ItemGroup>
14+
<PackageReference Include="Amazon.Lambda.Core" Version="2.5.0" />
15+
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
16+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="29.2.7" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<None Update="Data\InputTemplate.xlsx">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"Mock Lambda Test Tool": {
4+
"commandName": "Executable",
5+
"commandLineArgs": "--port 5050",
6+
"workingDirectory": ".\\bin\\$(Configuration)\\net8.0",
7+
"executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-8.0.exe"
8+
}
9+
}
10+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# AWS Lambda Empty Function Project
2+
3+
This starter project consists of:
4+
* Function.cs - class file containing a class with a single function handler method
5+
* aws-lambda-tools-defaults.json - default argument settings for use with Visual Studio and command line deployment tools for AWS
6+
7+
You may also have a test project depending on the options selected.
8+
9+
The generated function handler is a simple method accepting a string argument that returns the uppercase equivalent of the input string. Replace the body of this method, and parameters, to suit your needs.
10+
11+
## Here are some steps to follow from Visual Studio:
12+
13+
To deploy your function to AWS Lambda, right click the project in Solution Explorer and select *Publish to AWS Lambda*.
14+
15+
To view your deployed function open its Function View window by double-clicking the function name shown beneath the AWS Lambda node in the AWS Explorer tree.
16+
17+
To perform testing against your deployed function use the Test Invoke tab in the opened Function View window.
18+
19+
To configure event sources for your deployed function, for example to have your function invoked when an object is created in an Amazon S3 bucket, use the Event Sources tab in the opened Function View window.
20+
21+
To update the runtime configuration of your deployed function use the Configuration tab in the opened Function View window.
22+
23+
To view execution logs of invocations of your function use the Logs tab in the opened Function View window.
24+
25+
## Here are some steps to follow to get started from the command line:
26+
27+
Once you have edited your template and code you can deploy your application using the [Amazon.Lambda.Tools Global Tool](https://github.com/aws/aws-extensions-for-dotnet-cli#aws-lambda-amazonlambdatools) from the command line.
28+
29+
Install Amazon.Lambda.Tools Global Tools if not already installed.
30+
```
31+
dotnet tool install -g Amazon.Lambda.Tools
32+
```
33+
34+
If already installed check if new version is available.
35+
```
36+
dotnet tool update -g Amazon.Lambda.Tools
37+
```
38+
39+
Execute unit tests
40+
```
41+
cd "LoadingandSaving/test/LoadingandSaving.Tests"
42+
dotnet test
43+
```
44+
45+
Deploy function to AWS Lambda
46+
```
47+
cd "LoadingandSaving/src/LoadingandSaving"
48+
dotnet lambda deploy-function
49+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{
3+
"Information" : [
4+
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
5+
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
6+
"dotnet lambda help",
7+
"All the command line options for the Lambda command can be specified in this file."
8+
],
9+
"profile" : "Syncfusion",
10+
"region" : "us-east-1",
11+
"configuration" : "Release",
12+
"function-architecture" : "x86_64",
13+
"function-runtime" : "dotnet8",
14+
"function-memory-size" : 512,
15+
"function-timeout" : 30,
16+
"function-handler" : "LoadingandSaving::LoadingandSaving.Function::FunctionHandler",
17+
"framework" : "net8.0",
18+
"function-name" : "MyNewFunction",
19+
"package-type" : "Zip",
20+
"function-role" : "arn:aws:iam::142887710098:role/ck_lambda_basic_execution",
21+
"function-subnets" : "",
22+
"function-security-groups" : "",
23+
"tracing-mode" : "PassThrough",
24+
"environment-variables" : "",
25+
"image-tag" : ""
26+
}

0 commit comments

Comments
 (0)