Android MinIO OSS客户端实现文件下载操作

minio 官方的sdk 只有java 版本的不兼容android,可以尝试使用 amazon s3 的oss sdk 来实现功能。两者协议是兼容的。

gradle 依赖

// https://mvnrepository.com/artifact/com.amazonaws/aws-android-sdk-s3
implementation 'com.amazonaws:aws-android-sdk-s3:2.22.5'
// https://mvnrepository.com/artifact/com.amazonaws/aws-android-sdk-mobile-client
implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.22.5'

工具类MinIoOssHelper.java

package com.example.zhstplayer.utils;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

//import io.minio.MinioClient;
//import io.minio.ObjectStat;

public class MinIoOssHelper {
    private static final String TAG = MinIoOssHelper.class.getSimpleName();

    private static MinIoOssHelper sMinIoOssHelper;

    private String ossUrl;
    private String accessKey;
    private String secretKey;

    private long transferedbytes;

    public interface MinIoCallback {
        void progress(int progress);
    }

    private MinIoOssHelper(String ossUrl, String accessKey, String secretKey) {
        this.ossUrl = ossUrl;
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public static MinIoOssHelper getInstance(String ossUrl, String accessKey, String secretKey) {
        if(sMinIoOssHelper == null) {
            sMinIoOssHelper = new MinIoOssHelper(ossUrl, accessKey, secretKey);
        }

        return sMinIoOssHelper;
    }

    /*
        文件下载
     */
    public boolean downloadOssObject(String bucketName, String objectName, String savePath, MinIoCallback callback) throws Exception {
        AmazonS3 s3 = new AmazonS3Client(new AWSCredentials() {
            @Override
            public String getAWSAccessKeyId() {
                return MinIoOssHelper.this.accessKey;//minio的key
            }
            @Override
            public String getAWSSecretKey() {
                return MinIoOssHelper.this.secretKey;//minio的密钥
            }
        }, Region.getRegion(Regions.CN_NORTH_1),new ClientConfiguration());
        //服务器地址
        s3.setEndpoint(this.ossUrl);

        ObjectMetadata metadata = s3.getObjectMetadata(bucketName, objectName);
        LogUtils.d(TAG, "object length = " + metadata.getContentLength() + " type = " + metadata.getContentType());
        S3Object object = s3.getObject(bucketName, objectName);
        InputStream stream = object.getObjectContent();

        // 读取输入流直到EOF并打印到控制台。
        byte[] buf = new byte[1024 * 10];
        int bytesRead;
        long currentBytes = 0;
        int prevProcess = 0;
        long totalBytes = metadata.getContentLength();
        FileOutputStream fileOutputStream = new FileOutputStream(savePath);
        while ((bytesRead = stream.read(buf, 0, buf.length)) >= 0) {
            //System.out.println(new String(buf, 0, bytesRead));
            currentBytes += bytesRead;
            int process = (int)(currentBytes * 100 / totalBytes);
            if(callback != null && process != prevProcess) {
                callback.progress(process);
                prevProcess = process;
            }
            fileOutputStream.write(buf, 0, bytesRead);
        }
        fileOutputStream.flush();
        fileOutputStream.close();
        return true;
    }


    /*
        文件上传
     */
    public boolean uploadOssObject(String bucketName, String objectName, String filePath, MinIoCallback callback) {
        AmazonS3 s3 = new AmazonS3Client(new AWSCredentials() {
            @Override
            public String getAWSAccessKeyId() {
                return MinIoOssHelper.this.accessKey;//minio的key
            }
            @Override
            public String getAWSSecretKey() {
                return MinIoOssHelper.this.secretKey;//minio的密钥
            }
        }, Region.getRegion(Regions.CN_NORTH_1),new ClientConfiguration());
        //服务器地址
        s3.setEndpoint(this.ossUrl);

        File file = new File(filePath);
        transferedbytes = 0;
        s3.putObject(new PutObjectRequest(bucketName, objectName, file).withGeneralProgressListener(progressEvent -> {
            if(callback != null) {
                transferedbytes += progressEvent.getBytesTransferred();
                int process = (int) (transferedbytes * 100 / file.length());
                callback.progress(process);
            }
        }));

        return true;
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
你可以使用MinIO Java客户端实现文件上传和下载。MinIO是一个基于对象存储的开源项目,可以提供高性能的文件存储和访问。 首先,你需要在你的Java项目中添加MinIO Java客户端的依赖。你可以在Maven或者Gradle配置文件中加入以下依赖: Maven: ```xml <dependencies> <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.1.0</version> </dependency> </dependencies> ``` Gradle: ```groovy dependencies { implementation 'io.minio:minio:8.1.0' } ``` 接下来,你需要连接到MinIO服务器并进行认证。你可以使用以下代码完成: ```java import io.minio.MinioClient; public class MinioExample { public static void main(String[] args) { try { // 创建MinIO客户端对象 MinioClient minioClient = new MinioClient("http://your-minio-server-url", "access-key", "secret-key"); // 检查存储桶是否存在,如果不存在则创建 boolean isExist = minioClient.bucketExists("your-bucket"); if (!isExist) { minioClient.makeBucket("your-bucket"); } // 上传文件 minioClient.putObject("your-bucket", "your-object-name", "/path/to/your-file"); // 下载文件 minioClient.getObject("your-bucket", "your-object-name", "/path/to/save-file"); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码中,你需要替换以下参数: - `your-minio-server-url`:MinIO服务器的URL地址。 - `access-key`:你的MinIO访问密钥。 - `secret-key`:你的MinIO密钥。 - `your-bucket`:存储桶的名称。 - `your-object-name`:对象的名称。 - `/path/to/your-file`:待上传的文件路径。 - `/path/to/save-file`:下载文件保存的路径。 通过上述代码,你可以使用MinIO Java客户端实现文件上传和下载。希望对你有帮助!如有任何问题,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值