Created
March 1, 2023 13:17
-
-
Save Dreamescaper/c0ecac849ce41eb8d8633185c6891e49 to your computer and use it in GitHub Desktop.
dotnet lambda CDK Custom AssetHash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static Code GetLambdaCode(string zipPath) | |
| { | |
| return Code.FromAsset(zipPath, new AssetOptions | |
| { | |
| AssetHash = HashForZipContent(zipPath), | |
| AssetHashType = Amazon.CDK.AssetHashType.CUSTOM | |
| }); | |
| } | |
| private static string HashForZipContent(string zipPath) | |
| { | |
| // See https://github.com/aws/aws-extensions-for-dotnet-cli/issues/195 | |
| using var sha256 = SHA256.Create(); | |
| IEnumerable<byte> fileHashes = Array.Empty<byte>(); | |
| using var archive = ZipFile.OpenRead(zipPath); | |
| foreach (var entry in archive.Entries.OrderBy(e => e.FullName)) | |
| { | |
| using var entryStream = entry.Open(); | |
| var entryHash = sha256.ComputeHash(entryStream); | |
| fileHashes = fileHashes.Concat(entryHash); | |
| } | |
| var zipHash = sha256.ComputeHash(fileHashes.ToArray()); | |
| return Convert.ToBase64String(zipHash); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment