0

The steps I followed are.

  1. get all objects from recursive objects
Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder()
    .bucket(bucketName).recursive(true).build());

  1. Then getting all streams of matching the prefix
 InputStream stream = minioClient.getObject(GetObjectArgs.builder()
.bucket(bucketName).object(objectName).build());

the list of multiple stream got by the InputStream stream How do we convert it into zip file ?

tried the following code but it's (zipOut) coming as null. downloading empty zip, How do we fix this ?

ByteArrayOutputStream fos = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(fos);
ZipEntry zipEntry1 = new ZipEntry(objectName);
zipEntry1.setSize(resource.contentLength());
zipEntry1.setTime(System.currentTimeMillis());
zipOut.putNextEntry(zipEntry1);
StreamUtils.copy(stream.readAllBytes(), zipOut);
zipOut.closeEntry();

Thanks in advance.

Ajay
  • 29
  • 7

1 Answers1

1

I had same problem in C#, this is how I managed to solve: If you go to the MinIO dashboard, you would be able to download the folder as a zipped file: enter image description here

Now if you inspect the browser request, it is something like this:

curl '*MINIO_URL*/api/v1/buckets/*BUCKET_NAME*/objects/download?prefix=*PREFIX*'\
  -H 'Accept: */*' \
  -H 'Accept-Language: en-US,en;q=0.9,fa;q=0.8' \
  -H 'Connection: keep-alive' \
  -H 'Cookie: token=*TOKEN*' \
  -H 'Referer: MINIO_URL/browser/BUCKET_NAME' \
  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36' \
  --compressed \
  --insecure

Where you should replace MINIO_URL & BUCKET_NAME with values of your own, now the TOKEN value is something you get from dashboard login. the PREFIX value is the base64 conversion of the folder path inside minio

This is the code & hope it helps:

public async Task<byte[]> DownloadZippedFolder(string filePath)
    {
        var httpContent = new StringContent(
            content: JsonConvert.SerializeObject(
                new 
                {
                    accessKey = _minIOSetting.DashboardUserName, 
                    secretKey = _minIOSetting.DashboardPassword
                }
            ), 
            encoding: Encoding.UTF8, 
            mediaType: "application/json"
        );
        try
        {
            var loginResponse = await _httpClient.PostAsync($"{_minIOSetting.LoginUrl}/api/v1/login", httpContent);

            string prefix = Convert.ToBase64String(Encoding.UTF8.GetBytes(filePath));

            loginResponse.Headers.TryGetValues("Set-Cookie", out var setCookie);

            _httpClient.DefaultRequestHeaders.Add("Cookie", setCookie.First());
            string url = _minIOSetting.LoginUrl
                + "/api/v1/buckets/"
                + _minIOSetting.BucketName
                + "/objects/download?prefix="
                + prefix;
            var dlreq = await _httpClient.GetAsync(url);
            var contentStream = await dlreq.Content.ReadAsStreamAsync();

            var memStream = new MemoryStream();
            await contentStream.CopyToAsync(memStream);

            var bs = memStream.ToArray();
            return bs;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"min IO error, DownloadZippedFolder: {ex.Message}");
            return null;
        }   
    }
moken
  • 3,227
  • 8
  • 13
  • 23