@@ -26,20 +26,20 @@ static async Task<string> Handler(ILambdaContext context)
26
26
{
27
27
var sw = Stopwatch . StartNew ( ) ;
28
28
29
- IAmazonS3 client = new AmazonS3Client ( ) ;
30
- var linkIndex = await CreateLinkIndex ( client ) ;
29
+ IAmazonS3 s3Client = new AmazonS3Client ( ) ;
30
+ var linkIndex = await CreateLinkIndex ( s3Client ) ;
31
31
if ( linkIndex == null )
32
32
return $ "Error encountered on server. getting list of objects.";
33
33
34
34
var json = LinkIndex . Serialize ( linkIndex ) ;
35
35
36
36
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 ) ;
38
38
return $ "Finished in { sw } ";
39
39
}
40
40
41
41
42
- static async Task < LinkIndex ? > CreateLinkIndex ( IAmazonS3 client )
42
+ static async Task < LinkIndex ? > CreateLinkIndex ( IAmazonS3 s3Client )
43
43
{
44
44
var request = new ListObjectsV2Request
45
45
{
@@ -53,11 +53,10 @@ static async Task<string> Handler(ILambdaContext context)
53
53
} ;
54
54
try
55
55
{
56
- var httpClient = new HttpClient ( ) ;
57
56
ListObjectsV2Response response ;
58
57
do
59
58
{
60
- response = await client . ListObjectsV2Async ( request , CancellationToken . None ) ;
59
+ response = await s3Client . ListObjectsV2Async ( request , CancellationToken . None ) ;
61
60
await Parallel . ForEachAsync ( response . S3Objects , async ( obj , ctx ) =>
62
61
{
63
62
if ( ! obj . Key . StartsWith ( "elastic/" , StringComparison . OrdinalIgnoreCase ) )
@@ -69,7 +68,7 @@ await Parallel.ForEachAsync(response.S3Objects, async (obj, ctx) =>
69
68
70
69
// TODO create a dedicated state file for git configuration
71
70
// Deserializing all of the links metadata adds significant overhead
72
- var gitReference = await ReadLinkReferenceSha ( httpClient , obj ) ;
71
+ var gitReference = await ReadLinkReferenceSha ( s3Client , obj ) ;
73
72
74
73
var repository = tokens [ 1 ] ;
75
74
var branch = tokens [ 2 ] ;
@@ -106,21 +105,18 @@ await Parallel.ForEachAsync(response.S3Objects, async (obj, ctx) =>
106
105
return linkIndex ;
107
106
}
108
107
109
- static async Task < string > ReadLinkReferenceSha ( HttpClient httpClient , S3Object obj )
108
+ static async Task < string > ReadLinkReferenceSha ( IAmazonS3 client , S3Object obj )
110
109
{
111
110
try
112
111
{
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 ) ;
120
115
return linkReference . Origin . Ref ;
121
116
}
122
- catch
117
+ catch ( Exception e )
123
118
{
119
+ Console . WriteLine ( e ) ;
124
120
// it's important we don't fail here we need to fallback gracefully from this so we can fix the root cause
125
121
// of why a repository is not reporting its git reference properly
126
122
return "unknown" ;
0 commit comments