0

I am using MinIO Client

I am doing a pet-project for cloud storage. I want to create a new empty folder in an existing bucket. How do i do this?

I have already tried doing it this way:

    public async Task CreateFolderAsync(string path, string folderName)
    {
        var args = new PutObjectArgs()
            .WithBucket(_configuration["MinIO:Bucket"])
            .WithObject(path + folderName + "/")
            .WithStreamData(new MemoryStream(new byte[] { }))
            .WithObjectSize(0);

        await _minioClient.PutObjectAsync(args).ConfigureAwait(false);
    }

but this code returns an error - System.InvalidOperationException: ObjectSize must be set

I haven't found any implementation of this one in c#, but I noticed that similar code works in java:

minioClient.putObject(
            PutObjectArgs.builder()
                    .bucket(bucket)
                    .stream(new ByteArrayInputStream(new byte[]{}), 0, -1)
                    .object("files/testFoler/")
                    .build());
3
  • What is the purpose of this? Folders aren't really a thing in object storage, since everything is based on keys. Commented Mar 5, 2024 at 15:47
  • @ProgrammingLlama I want to have nested directories, maybe I should just use a database with tables: "files" and "folders", and s3 just as file storage, but I'm not sure that this way I can create the concept of a root folder Commented Mar 5, 2024 at 17:29
  • You don't really need to create a folder. You can just put a file to a/b/c/d and another file to a/b/c/e, and it will be handled for you. Things like S3, GCS, etc. use the / to demark folders, but in reality there are no folders, and it's just how the UI presents them. Commented Mar 6, 2024 at 2:01

1 Answer 1

0

Today I faced the same problem and the workaround I used was to upload an empty file:

public async Task CreateFolderAsync(string path, string folderName)
{
    var args = new PutObjectArgs()
        .WithBucket(_configuration["MinIO:Bucket"])
        .WithObject(path + folderName + "/")
        .WithFileName("Empty.txt");

    await _minioClient.PutObjectAsync(args).ConfigureAwait(false);
}

In my case, Empty.txt is an empty file which the application creates before starting. I looked at the code of PutObjectArgs class and I think that this line is what blocks creating a folder starting from an empty stream:

if (ObjectStreamData is not null && ObjectSize == 0)
    throw new InvalidOperationException($"{nameof(ObjectSize)} must be set");

Specifying object size as zero, as shown in the question, does not work because size is compared to zero in order to check if it has been specified. I also tried setting a fake size, such as one, but that does not work either, because it is detected that stream length is not equal to object size.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.