robinrodricks / FluentStorage

A polycloud .NET cloud storage abstraction layer. Provides Blob storage (AWS S3, GCP, FTP, SFTP, Azure Blob/File/Event Hub/Data Lake) and Messaging (AWS SQS, Azure Queue/ServiceBus). Supports .NET 5+ and .NET Standard 2.0+. Pure C#.
MIT License
263 stars 33 forks source link

AwsS3BlobStorage wants to create a new bucket on read. #27

Closed AntMaster7 closed 1 year ago

AntMaster7 commented 1 year ago

I want to read an object from an S3 storage with ReadBytesAsync. However, I get an exception saying that I am not authorized to perform create-bucket. Why do I need that permission for reading?

image

Edit: I found the issue in the implementation. The method GetClientAsync in AwsS3BlobStorage makes a PutBucket request:

https://github.com/robinrodricks/FluentStorage/blob/1dd683ac51ee896c77b055ee6e23a05d23a46e6d/FluentStorage.AWS/Blobs/AwsS3BlobStorage.cs#L113

Why does it do that? It should at least check if the bucket already exists.

You can fix it like so:

private async Task<AmazonS3Client> GetClientAsync() {
    if (!_initialised) {
        var bucketExists = await AmazonS3Util.DoesS3BucketExistV2Async(_client, _bucketName);
        if (!bucketExists)  {
            var request = new PutBucketRequest { BucketName = _bucketName };

            await _client.PutBucketAsync(request).ConfigureAwait(false);
        }

        _initialised = true;
    }

    return _client;
}

I can do a PR if you want?

robinrodricks commented 1 year ago

Yes please make a PR with a fix, it would be highly appreciated!

robinrodricks commented 1 year ago

Thanks a lot!