OssFileUtil.java 1.24 KB
package com.vion.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.ObjectListing;

/**
 * @author 谢明辉
 * @createDate 2018-11-6
 * @description
 */
public class OssFileUtil {

    /**
     * @param prefix     路径前缀,例如,picture/face/20181010
     * @param bucketName bucketName
     * @param maxKeys    列出的最大文件数量,如果为null,默认100,最大为1000
     * @param delimiter  是否使用分割符
     * @param ossClient  ossClient实例
     * @return com.aliyun.oss.model.ObjectListing
     * @createDate 2018-11-6
     * @description 列举指定目录下的所有文件
     */
    public ObjectListing getFilesInPrefix(String bucketName, String prefix, OSSClient ossClient, Integer maxKeys, boolean delimiter) {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
        if (maxKeys != null) {
            maxKeys = maxKeys > 1000 ? 1000 : (maxKeys < 1 ? 1 : maxKeys);
            listObjectsRequest.setMaxKeys(maxKeys);
        }
        if (delimiter) {
            listObjectsRequest.setDelimiter("/");
        }
        listObjectsRequest.setPrefix(prefix);
        return ossClient.listObjects(listObjectsRequest);
    }
}