阿里云OSS批量根据文件url获取字节数组集合

定义接口

byte[] readFile(String url);

List<byte[]> batchReadFile(List<String> urls);

依赖包

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

实现方法

/**
  * 单个获取
  * @param url
  * @return byte[]
  **/
public byte[] readFile(String url) {
    byte[] content = null;

    try {
        OSS ossClient = (new OSSClientBuilder())
        	.build( this.providerProperties.getEndPoint(), 
        			this.providerProperties.getAccessKey(), 
        			this.providerProperties.getSecretKey()
     		);
        content = this.ossRead(ossClient, url);
        ossClient.shutdown();
    } catch (Exception e) {
        log.error("读取文件失败,{}", e);
    }
    return content;
}
/**
  * 批量获取
  * @param urls
  * @return List<byte[]>
  **/
public List<byte[]> batchReadFile(List<String> urls) {
    ArrayList bytes = new ArrayList();

    try {
        OSS ossClient = (new OSSClientBuilder())
        	.build( this.providerProperties.getEndPoint(), 
        			this.providerProperties.getAccessKey(), 
        			this.providerProperties.getSecretKey()
        	);
        Iterator var4 = urls.iterator();

        while(var4.hasNext()) {
            String url = (String)var4.next();
            byte[] content = this.ossRead(ossClient, url);
            bytes.add(content);
        }

        ossClient.shutdown();
    } catch (Exception var7) {
        log.error("批量读取文件失败,{}", var7);
    }

    return bytes;
}

private byte[] ossRead(OSS ossClient, String url) throws Exception {
    String objectName = url.replace(this.providerProperties.getBucketHost() + "/", "");
    OSSObject ossObject = ossClient.getObject(
    							this.providerProperties.getBucketName(), 
    							objectName
    					   );
    InputStream in = ossObject.getObjectContent();
    int buffSize = 1024;
    ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);
    IOUtils.copyLarge(in, out);
    in.close();
    out.close();
    return out.toByteArray();
}

this.providerProperties

就是OSS的配置,不同项目的配置方式不同,大家根据自身项目的配置方式调整,将配置类注入到上面的实现类中即可

@ConfigurationProperties(
    prefix = "file.provider"
)
public class FileProviderProperties {
    private String bucketName;
    private String bucketHost;
    private String endPoint;
    private String accessKey;
    private String secretKey;

    public FileProviderProperties() {
    }

    public String getBucketName() {
        return this.bucketName;
    }

    public String getBucketHost() {
        return this.bucketHost;
    }

    public String getEndPoint() {
        return this.endPoint;
    }

    public String getAccessKey() {
        return this.accessKey;
    }

    public String getSecretKey() {
        return this.secretKey;
    }

    public void setBucketName(final String bucketName) {
        this.bucketName = bucketName;
    }

    public void setBucketHost(final String bucketHost) {
        this.bucketHost = bucketHost;
    }

    public void setEndPoint(final String endPoint) {
        this.endPoint = endPoint;
    }

    public void setAccessKey(final String accessKey) {
        this.accessKey = accessKey;
    }

    public void setSecretKey(final String secretKey) {
        this.secretKey = secretKey;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值