删除文件

更新时间: 2020-03-24

目录

删除一个或多个文件

删除文件并设置返回结果

本节介绍如何删除文件。

请您谨慎使用删除操作,文件一旦删除将无法恢复。

删除一个或多个文件

以下代码用于删除一个或多个文件:

// Endpoint以华北三为例,其它Region请按实际情况填写。
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";
String bucketName = "<yourBucketName>";
String key = "<yourObjectName>";

//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);

List<KeyAndVersion> keyAndVersions = new ArrayList<>();
KeyAndVersion keyAndVersion1 = new KeyAndVersion("*** Provide object name or version ***");
keyAndVersions.add(keyAndVersion1);

//删除多个文件
ossClient.deleteObjects(bucketName, keyAndVersions);

删除文件并设置返回结果

以下代码用于删除一个或多个文件并限制返回结果:

// Endpoint以华北三为例,其它Region请按实际情况填写。
String endpoint = "oss.cn-north-3.inspurcloudoss.com";
String accessKey = "<yourAccessKey>";
String secretKey = "<yourSecretKey>";
String bucketName = "<yourBucketName>";
String key = "<yourObjectName>";
//quite表示返回全部的结果
Boolean quite = ture;

//创建OSSClient实例
OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);

List<KeyAndVersion> keyAndVersions = new ArrayList<>();
KeyAndVersion keyAndVersion1 = new KeyAndVersion("*** Provide object name or version ***");
keyAndVersions.add(keyAndVersion1);

//删除多个文件
ossClient.deleteObjects(bucketName, keyAndVersions,quite);