-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathLinkIndexReader.cs
More file actions
61 lines (54 loc) · 2.05 KB
/
LinkIndexReader.cs
File metadata and controls
61 lines (54 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using Elastic.Documentation.Links;
namespace Elastic.Documentation.LinkIndex;
public class Aws3LinkIndexReader(IAmazonS3 s3Client, string bucketName = "elastic-docs-link-index", string registryKey = "link-index.json") : ILinkIndexReader, IDisposable
{
// <summary>
// Using <see cref="AnonymousAWSCredentials"/> to access the link index
// allows reading from the link index without the need to provide AWS credentials.
// </summary>
public static Aws3LinkIndexReader CreateAnonymous()
{
var credentials = new AnonymousAWSCredentials();
var config = new AmazonS3Config
{
RegionEndpoint = Amazon.RegionEndpoint.USEast2
};
var s3Client = new AmazonS3Client(credentials, config);
return new AwsS3LinkIndexReaderWriter(s3Client);
}
public async Task<LinkRegistry> GetRegistry(Cancel cancellationToken = default)
{
var getObjectRequest = new GetObjectRequest
{
BucketName = bucketName,
Key = registryKey
};
var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest, cancellationToken);
await using var stream = getObjectResponse.ResponseStream;
var linkIndex = LinkRegistry.Deserialize(stream);
return linkIndex with { ETag = getObjectResponse.ETag };
}
public async Task<RepositoryLinks> GetRepositoryLinks(string key, Cancel cancellationToken)
{
var getObjectRequest = new GetObjectRequest
{
BucketName = bucketName,
Key = key
};
var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest, cancellationToken);
await using var stream = getObjectResponse.ResponseStream;
return RepositoryLinks.Deserialize(stream);
}
public string RegistryUrl { get; } = $"https://{bucketName}.s3.{s3Client.Config.RegionEndpoint.SystemName}.amazonaws.com/{registryKey}";
public void Dispose()
{
s3Client.Dispose();
GC.SuppressFinalize(this);
}
}