package com.izouma.nineth.utils; import com.aliyun.oss.OSSClient; import com.aliyun.oss.model.OSSObject; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.TypeFactory; import com.izouma.nineth.config.AliyunProperties; import java.io.IOException; import java.io.InputStream; import java.util.List; public class OSSClientUtil { /** * 通过阿里云OSSClient客户端读取指定objectKey的文件流,利用Jackson将文件流反序列化成List类型的数据模型并返回。 * * @param properties 用于配置阿里云OSS客户端的属性对象。 * @param objectKey 阿里云OSS存储桶中的文件对象的唯一标识符。 * @param clazz 要反序列化的目标类型。 * @return 返回反序列化后的List类型数据模型。 * @throws IOException 当出现IO异常时,抛出IOException。 */ public static List getDataModel(AliyunProperties properties, String objectKey, Class clazz) throws IOException { // 从阿里云OSS客户端配置对象中获取必要的参数 String endpoint = properties.getOssEndPoint(); String accessKeyId = properties.getAccessKeyId(); String accessKeySecret = properties.getAccessKeySecret(); String bucketName = properties.getOssBucketName(); // 创建OSS客户端 OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret); List list; // 获取指定文件对象的OSSObject对象 OSSObject ossObject = client.getObject(bucketName, objectKey); // 创建Jackson的ObjectMapper对象用于反序列化操作 ObjectMapper objectMapper = new ObjectMapper(); // 将OSSObject对象中的文件流反序列化成List类型的数据模型 try (InputStream inputStream = ossObject.getObjectContent()) { list = objectMapper.readValue(inputStream, TypeFactory.defaultInstance().constructCollectionType(List.class, clazz)); } finally { // 关闭OSS客户端 client.shutdown(); } return list; } }