-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathAWSS3StorageImageResolver.cs
More file actions
58 lines (50 loc) · 2.22 KB
/
AWSS3StorageImageResolver.cs
File metadata and controls
58 lines (50 loc) · 2.22 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
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Net.Http.Headers;
using Amazon.S3;
using Amazon.S3.Model;
namespace SixLabors.ImageSharp.Web.Resolvers.AWS;
/// <summary>
/// Provides means to manage image buffers within the AWS S3 file system.
/// </summary>
public class AWSS3StorageImageResolver : IImageResolver
{
private readonly IAmazonS3 amazonS3;
private readonly string bucketName;
private readonly string imagePath;
private readonly GetObjectMetadataResponse? metadataResponse;
/// <summary>
/// Initializes a new instance of the <see cref="AWSS3StorageImageResolver"/> class.
/// </summary>
/// <param name="amazonS3">The Amazon S3 Client</param>
/// <param name="bucketName">The bucket name.</param>
/// <param name="imagePath">The image path.</param>
/// <param name="metadataResponse">Optional metadata response.</param>
public AWSS3StorageImageResolver(IAmazonS3 amazonS3, string bucketName, string imagePath, GetObjectMetadataResponse? metadataResponse = null)
{
this.amazonS3 = amazonS3;
this.bucketName = bucketName;
this.imagePath = imagePath;
this.metadataResponse = metadataResponse;
}
/// <inheritdoc />
public async Task<ImageMetadata> GetMetaDataAsync()
{
GetObjectMetadataResponse metadata = this.metadataResponse ?? await this.amazonS3.GetObjectMetadataAsync(this.bucketName, this.imagePath);
// Try to parse the max age from the source. If it's not zero then we pass it along
// to set the cache control headers for the response.
TimeSpan maxAge = TimeSpan.MinValue;
if (CacheControlHeaderValue.TryParse(metadata.Headers.CacheControl, out CacheControlHeaderValue? cacheControl))
{
// Weirdly passing null to TryParse returns true.
if (cacheControl?.MaxAge.HasValue == true)
{
maxAge = cacheControl.MaxAge.Value;
}
}
return new ImageMetadata(metadata.LastModified ?? DateTime.UtcNow, maxAge, metadata.ContentLength);
}
/// <inheritdoc />
public Task<Stream> OpenReadAsync()
=> this.amazonS3.GetObjectStreamAsync(this.bucketName, this.imagePath, null);
}