java 阿里云 oss 根据文件名模糊查询 OSSClientBuilder
在阿里云 OSS 中,可以使用 OSSClientBuilder 对象来构建 OSSClient。根据文件名的模糊查询可以通过以下步骤实现:
- 导入所需的阿里云 OSS 相关的依赖包:
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.OSSObjectSummary;
- 创建
OSSClient对象:
String endpoint = "<yourEndpoint>"; // OSS 的 endpoint
String accessKeyId = "<yourAccessKeyId>"; // 阿里云的 Access Key ID
String accessKeySecret = "<yourAccessKeySecret>"; // 阿里云的 Access Key Secret
OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
- 构建
ListObjectsRequest对象,设置prefix为文件名的前缀:
String bucketName = "<yourBucketName>"; // OSS 的 bucket 名称
String prefix = "<yourPrefix>"; // 文件名的前缀
ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
listObjectsRequest.setPrefix(prefix);
- 使用
listObjects方法获取符合条件的文件列表:
ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();
for (OSSObjectSummary objectSummary : objectSummaries) {
System.out.println(objectSummary.getKey());
}
以上代码片段会打印出符合条件的文件名列表。你可以根据需要进行进一步的处理
原文地址: https://www.cveoy.top/t/topic/isBS 著作权归作者所有。请勿转载和采集!