bechtleav360 / tusdotnet.Storage.S3

Extension to support S3 as a storage backend for the tusdotnet TUS implemntation
European Union Public License 1.2
2 stars 2 forks source link

the S3 storage key prefix should be configurable #3

Open Driedas opened 3 months ago

Driedas commented 3 months ago

Hi, first of all, thanks a lot for taking the time to implement this library!

The TusS3Api class currently implements the calculation of the S3 key as follows:

    private string GetFileKey(string key)
    {
      string str = "files/";
      if (!string.IsNullOrWhiteSpace(str) && !str.EndsWith("/"))
        str += "/";
      return str + key;
    }

would it be possible to make the hardcoded "files/"prefix configurable? We have an already existing directory structure in place that I would like to connect this store to, the logic of that structure can nicely be encoded in an implementation of ITusFileIdProvider, except this prefix... Happy to submit a PR if you agree...

Driedas commented 2 months ago

as a (hopefully) temporary workaround, one can use the ctor

    public TusS3Store(
        ILogger<TusS3Store> logger,
        TusS3StoreConfiguration configuration,
        IAmazonS3 s3Client,
        ITusFileIdProvider? fileIdProvider = null)

and provide a custom implementation of the IAmazonS3 interface that strips the files/ prefix and replaces it with anything else

internal partial class TusAmazonS3
    : IAmazonS3
{
    private readonly IAmazonS3 _client;

    public TusAmazonS3(IAmazonS3 client)
    {
        _client = client;
    }

    public Task<UploadPartResponse> UploadPartAsync(UploadPartRequest request, CancellationToken cancellationToken = default)
    {
        request.Key = StripFilesPrefix(request.Key);
        return _client.UploadPartAsync(request, cancellationToken);
    }

    public Task<AbortMultipartUploadResponse> AbortMultipartUploadAsync(string bucketName, string key, string uploadId,
        CancellationToken cancellationToken = default)
    {
        key = StripFilesPrefix(key);
        return _client.AbortMultipartUploadAsync(bucketName, key, uploadId, cancellationToken);
    }

    public Task<AbortMultipartUploadResponse> AbortMultipartUploadAsync(AbortMultipartUploadRequest request,
        CancellationToken cancellationToken = default)
    {
        request.Key = StripFilesPrefix(request.Key);
        return _client.AbortMultipartUploadAsync(request, cancellationToken);
    }

    public Task<CompleteMultipartUploadResponse> CompleteMultipartUploadAsync(CompleteMultipartUploadRequest request,
        CancellationToken cancellationToken = default)
    {
        request.Key = StripFilesPrefix(request.Key);
        return _client.CompleteMultipartUploadAsync(request, cancellationToken);
    }

    public Task<GetObjectResponse> GetObjectAsync(GetObjectRequest request, CancellationToken cancellationToken = default)
    {
        request.Key = StripFilesPrefix(request.Key);
        return _client.GetObjectAsync(request, cancellationToken);
    }

    public Task<GetObjectMetadataResponse> GetObjectMetadataAsync(string bucketName, string key,
        CancellationToken cancellationToken = default)
    {
        key = StripFilesPrefix(key);
        return _client.GetObjectMetadataAsync(bucketName, key, cancellationToken);
    }

    public Task<InitiateMultipartUploadResponse> InitiateMultipartUploadAsync(InitiateMultipartUploadRequest request,
        CancellationToken cancellationToken = default)
    {
        request.Key = StripFilesPrefix(request.Key);
        return _client.InitiateMultipartUploadAsync(request, cancellationToken);
    }

    public Task<PutObjectResponse> PutObjectAsync(PutObjectRequest request, CancellationToken cancellationToken = default)
    {
        request.Key = StripFilesPrefix(request.Key);
        return _client.PutObjectAsync(request, cancellationToken);
    }

    private string StripFilesPrefix(string key)
    {
        return StripPrefixRegex().Replace(key, string.Empty);
    }

    [GeneratedRegex("^files/")]
    private static partial Regex StripPrefixRegex();