How to list buckets from YandexStorage using C# and AmazonS3Client

50 Views Asked by At

Recently, i wrote a C# service class that successfully works with S3 compatible services and checked that it allows me to manipulate buckets and theit contents. This code successfully works with CloudFlare.R2 and AWS S3, however i was forced to migrate to Yandex Storage, and some of my functions stopped working (all operation related to buckets).

This is my interface for S3 services, all methods that have bucket in name does not work with Yandex.Storage and work with i.e. CloudFlare.R2:

 public interface IS3CompatibleFileService
 {
     // bucket manipulation methods
     Task<IList<string>> GetBucketsAsync();
     Task<bool> CheckBucketExistsAsync(string bucketName);
     Task<bool> AddBucketAsync(string bucketName);
     Task<bool> RemoveBucketAsync(string bucketName);
     // files manipulation methods
     Task<bool> AddItemAsync(string itemName, string bucketName, string filePath);
     Task<bool> AddItemAsync(string itemName, string bucketName, byte[] content);
     Task<bool> RemoveItemAsync(string itemName, string bucketName);
     Task<bool> UpdateItemAsync(string itemName, string bucketName, string filePath);
     Task<bool> UpdateItemAsync(string itemName, string bucketName, byte[] content);
     Task<byte[]> GetItemDataAsync(string itemName, string bucketName);
}

Part of implementation of this interface:

public class AWSCompatibleFileStorageService : IS3CompatibleFileService, IDisposable
{
    public AWSCompatibleFileStorageService(ApplicationSettings settings, ILoggerFactory loggerFactory)
    {
        _settings = settings;
        _logger = loggerFactory.CreateLogger<AWSCompatibleFileStorageService>();
        try
        {
            _s3Client = new AmazonS3Client(new BasicAWSCredentials(_settings.S3Storage.AccessKey, _settings.S3Storage.SecretAccessKey),
                new AmazonS3Config
            {
                ServiceURL = _settings.S3Storage.Endpoint,
                    
            });
        }
        catch(AmazonClientException e)
        {
            _logger.LogError($"Incorrect S3 Storage configuration: {e.Message} , ensure S3Storage section has correct values");
        }
            
    }

    public void Dispose()
    {
        if (_s3Client != null)
            _s3Client.Dispose();
    }

    public async Task<IList<string>> GetBucketsAsync()
    {
        try
        {
            ListBucketsResponse bucketsResp = await _s3Client.ListBucketsAsync();
            return bucketsResp.Buckets.Select(b => b.BucketName).ToList();
        }
        catch (Exception e)
        {
            _logger.LogError($"An error occurred during getting buckets list: {e.Message}");
            return null;
        }
    }

    public async Task<bool> CheckBucketExistsAsync(string bucketName)
    {
        try
        {
            IList<string> buckets = await GetBucketsAsync();
            return buckets.Contains(bucketName);
        }
        catch (Exception e)
        {
            return false;
        }
    }
    // other methods
}

I am using https://s3.yandexcloud.net as an operations endpoint. I've created service account with all possible Roles related to yandex storage (storage.admin), see:

enter image description here

Result of bucket list exectution is - Access Denied.

According to this documentation we should pass folderId, but i don't understand how to do this techically. Actually, there are 2 questions:

  1. Is it possible to implement all bucket operations using AmazonS3Client
  2. if answer on 1 is 'Yes' how to configure code to do this.
0

There are 0 best solutions below