java 如何从minio下载文件夹为zip文件?

0dxa2lsx  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(1011)

我遵循的步骤是。
1.从递归对象中获取所有对象

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

1.然后获取匹配前缀的所有流

InputStream stream = minioClient.getObject(GetObjectArgs.builder()
.bucket(bucketName).object(objectName).build());

InputStream流得到的多个流的列表如何将其转换为zip文件?
尝试了下面的代码,但它的(zipOut)来作为null.下载空zip,我们如何解决这个问题?

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();

先谢谢你了。

2lpgd968

2lpgd9681#

我在C#中遇到了同样的问题,这是我如何解决的:如果您转到MinIO Jmeter 板,则可以将文件夹下载为压缩文件:enter image description here
现在如果你检查浏览器请求,它是这样的:

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

这里你应该用你自己的值替换 MINIO_URL & BUCKET_NAME,现在 TOKEN 值是你从 Jmeter 板登录中得到的东西。PREFIX 值是minio内部文件夹路径的base64转换
这是我的代码,希望对你有帮助:

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;
        }   
    }

相关问题