1,引入依赖
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.16.1</version> </dependency>
2,方法
/*** 删除指定前缀或目录下的多个文件** @param prefix 删除目录*/public void deletePathFileList(String prefix) {OSS ossClient = null;try {//封装创建Oss实例CredentialsProvider credentialsProvider = new DefaultCredentialProvider(access_key, secret_key);ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);ossClient = OSSClientBuilder.create().endpoint(endpoint).credentialsProvider(credentialsProvider).clientConfiguration(clientBuilderConfiguration).region(region).build();// 列举所有包含指定前缀的文件并删除。String nextMarker = null;ObjectListing objectListing;do {ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket).withPrefix(prefix).withMarker(nextMarker);objectListing = ossClient.listObjects(listObjectsRequest);if (!objectListing.getObjectSummaries().isEmpty()) {List<String> keys = new ArrayList<>();for (OSSObjectSummary s : objectListing.getObjectSummaries()) {keys.add(s.getKey());}DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket).withKeys(keys).withEncodingType("url");DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();for (String obj : deletedObjects) {String deleteObj = URLDecoder.decode(obj, "UTF-8");System.out.println(deleteObj);}}nextMarker = objectListing.getNextMarker();} while (objectListing.isTruncated());} catch (Exception e) {log.error("阿里云删除指定前缀或目录下的多个文件异常:{}", e.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}