|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Net; |
| 6 | +using Amazon.Runtime; |
| 7 | +using Amazon.S3; |
| 8 | +using Amazon.S3.Model; |
| 9 | +using Elastic.Documentation.Links; |
| 10 | + |
| 11 | +namespace Elastic.Documentation.LinkIndex; |
| 12 | + |
| 13 | +public interface ILinkIndexProvider |
| 14 | +{ |
| 15 | + Task<LinkReferenceRegistry> GetLinkIndex(Cancel cancellationToken = default); |
| 16 | + Task SaveLinkIndex(LinkReferenceRegistry registry, Cancel cancellationToken = default); |
| 17 | + Task<LinkReference> GetLinkReference(string key, Cancel cancellationToken = default); |
| 18 | + |
| 19 | + string GetLinkIndexPublicUrl(); |
| 20 | +} |
| 21 | + |
| 22 | +public class AwsS3LinkIndexProvider(IAmazonS3 s3Client, string bucketName = "elastic-docs-link-index", string registryKey = "link-index.json") : ILinkIndexProvider |
| 23 | +{ |
| 24 | + |
| 25 | + public static AwsS3LinkIndexProvider CreateAnonymous() |
| 26 | + { |
| 27 | + var credentials = new AnonymousAWSCredentials(); |
| 28 | + var config = new AmazonS3Config |
| 29 | + { |
| 30 | + RegionEndpoint = Amazon.RegionEndpoint.USEast2 |
| 31 | + }; |
| 32 | + var s3Client = new AmazonS3Client(credentials, config); |
| 33 | + return new AwsS3LinkIndexProvider(s3Client); |
| 34 | + } |
| 35 | + |
| 36 | + public async Task<LinkReferenceRegistry> GetLinkIndex(Cancel cancellationToken = default) |
| 37 | + { |
| 38 | + var getObjectRequest = new GetObjectRequest |
| 39 | + { |
| 40 | + BucketName = bucketName, |
| 41 | + Key = registryKey |
| 42 | + }; |
| 43 | + var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest, cancellationToken); |
| 44 | + await using var stream = getObjectResponse.ResponseStream; |
| 45 | + var linkIndex = LinkReferenceRegistry.Deserialize(stream); |
| 46 | + return linkIndex with { ETag = getObjectResponse.ETag }; |
| 47 | + } |
| 48 | + |
| 49 | + public async Task SaveLinkIndex(LinkReferenceRegistry registry, Cancel cancellationToken = default) |
| 50 | + { |
| 51 | + if (registry.ETag == null) |
| 52 | + // The ETag should not be null if the LinkReferenceRegistry was retrieved from GetLinkIndex() |
| 53 | + throw new InvalidOperationException($"{nameof(LinkReferenceRegistry)}.{nameof(registry.ETag)} cannot be null"); |
| 54 | + var json = LinkReferenceRegistry.Serialize(registry); |
| 55 | + var putObjectRequest = new PutObjectRequest |
| 56 | + { |
| 57 | + BucketName = bucketName, |
| 58 | + Key = registryKey, |
| 59 | + ContentBody = json, |
| 60 | + ContentType = "application/json", |
| 61 | + IfMatch = registry.ETag // Only update if the ETag matches. Meaning the object has not been changed in the meantime. |
| 62 | + }; |
| 63 | + var putResponse = await s3Client.PutObjectAsync(putObjectRequest, cancellationToken); |
| 64 | + if (putResponse.HttpStatusCode != HttpStatusCode.OK) |
| 65 | + throw new Exception($"Unable to save {nameof(LinkReferenceRegistry)} to s3://{bucketName}/{registryKey}"); |
| 66 | + } |
| 67 | + |
| 68 | + public async Task<LinkReference> GetLinkReference(string key, Cancel cancellationToken) |
| 69 | + { |
| 70 | + var getObjectRequest = new GetObjectRequest |
| 71 | + { |
| 72 | + BucketName = bucketName, |
| 73 | + Key = key |
| 74 | + }; |
| 75 | + var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest, cancellationToken); |
| 76 | + await using var stream = getObjectResponse.ResponseStream; |
| 77 | + return LinkReference.Deserialize(stream); |
| 78 | + } |
| 79 | + |
| 80 | + public string GetLinkIndexPublicUrl() => $"https://{bucketName}.s3.{s3Client.Config.RegionEndpoint.SystemName}.amazonaws.com/{registryKey}"; |
| 81 | +} |
0 commit comments