@@ -26,20 +26,20 @@ static async Task<string> Handler(ILambdaContext context)
2626{
2727 var sw = Stopwatch . StartNew ( ) ;
2828
29- IAmazonS3 client = new AmazonS3Client ( ) ;
30- var linkIndex = await CreateLinkIndex ( client ) ;
29+ IAmazonS3 s3Client = new AmazonS3Client ( ) ;
30+ var linkIndex = await CreateLinkIndex ( s3Client ) ;
3131 if ( linkIndex == null )
3232 return $ "Error encountered on server. getting list of objects.";
3333
3434 var json = LinkIndex . Serialize ( linkIndex ) ;
3535
3636 using var stream = new MemoryStream ( Encoding . UTF8 . GetBytes ( json ) ) ;
37- await client . UploadObjectFromStreamAsync ( bucketName , "link-index.json" , stream , new Dictionary < string , object > ( ) , CancellationToken . None ) ;
37+ await s3Client . UploadObjectFromStreamAsync ( bucketName , "link-index.json" , stream , new Dictionary < string , object > ( ) , CancellationToken . None ) ;
3838 return $ "Finished in { sw } ";
3939}
4040
4141
42- static async Task < LinkIndex ? > CreateLinkIndex ( IAmazonS3 client )
42+ static async Task < LinkIndex ? > CreateLinkIndex ( IAmazonS3 s3Client )
4343{
4444 var request = new ListObjectsV2Request
4545 {
@@ -53,11 +53,10 @@ static async Task<string> Handler(ILambdaContext context)
5353 } ;
5454 try
5555 {
56- var httpClient = new HttpClient ( ) ;
5756 ListObjectsV2Response response ;
5857 do
5958 {
60- response = await client . ListObjectsV2Async ( request , CancellationToken . None ) ;
59+ response = await s3Client . ListObjectsV2Async ( request , CancellationToken . None ) ;
6160 await Parallel . ForEachAsync ( response . S3Objects , async ( obj , ctx ) =>
6261 {
6362 if ( ! obj . Key . StartsWith ( "elastic/" , StringComparison . OrdinalIgnoreCase ) )
@@ -69,7 +68,7 @@ await Parallel.ForEachAsync(response.S3Objects, async (obj, ctx) =>
6968
7069 // TODO create a dedicated state file for git configuration
7170 // Deserializing all of the links metadata adds significant overhead
72- var gitReference = await ReadLinkReferenceSha ( httpClient , obj ) ;
71+ var gitReference = await ReadLinkReferenceSha ( s3Client , obj ) ;
7372
7473 var repository = tokens [ 1 ] ;
7574 var branch = tokens [ 2 ] ;
@@ -106,21 +105,18 @@ await Parallel.ForEachAsync(response.S3Objects, async (obj, ctx) =>
106105 return linkIndex ;
107106}
108107
109- static async Task < string > ReadLinkReferenceSha ( HttpClient httpClient , S3Object obj )
108+ static async Task < string > ReadLinkReferenceSha ( IAmazonS3 client , S3Object obj )
110109{
111110 try
112111 {
113- // can not use client getobject since CRT checksum validation requires native code not available in AOT.
114- var tokens = obj . Key . Split ( '/' ) ;
115- var path = Path . Join ( tokens [ 0 ] , tokens [ 1 ] , "tree" , tokens [ 2 ] , string . Join ( "/" , tokens [ 3 ..] ) ) ;
116- var url = "https://docs-v3-preview.elastic.dev/" + path ;
117- var json = await httpClient . GetStringAsync ( new Uri ( url ) ) ;
118-
119- var linkReference = LinkReference . Deserialize ( json ) ;
112+ var contents = await client . GetObjectAsync ( obj . Key , obj . Key , CancellationToken . None ) ;
113+ await using var s = contents . ResponseStream ;
114+ var linkReference = LinkReference . Deserialize ( s ) ;
120115 return linkReference . Origin . Ref ;
121116 }
122- catch
117+ catch ( Exception e )
123118 {
119+ Console . WriteLine ( e ) ;
124120 // it's important we don't fail here we need to fallback gracefully from this so we can fix the root cause
125121 // of why a repository is not reporting its git reference properly
126122 return "unknown" ;
0 commit comments