删除 amazon s3 java 中的文件、目录和桶

问题描述 投票:0回答:11

我想知道如何做到这一点。我查看了 sdk 文档并提供了一些示例,但对语法的一般情况感到困惑。

如果我想删除一个文件,我假设我使用

deleteObject(path, key)
。然而,“关键”是什么?

还有你如何删除一个目录?我似乎找不到这样做的方法。

java amazon-web-services
11个回答
47
投票

这段代码对我有用。 folderPath 类似于“topDir/secondDir/”

void deleteObjectsInFolder(String bucketName, String folderPath) {
   for (S3ObjectSummary file : s3.listObjects(bucketName, folderPath).getObjectSummaries()){
      s3.deleteObject(bucketName, file.getKey());
    }
}

16
投票

S3中的“key”类似于文件路径:

http://bucket.s3.amazonaws.com/some/path/to/use

... 在一个名为

bucket
的桶中,并且有一个
some/path/to/use
的键。

它实际上不是路径,因为没有文件夹。 S3 密钥只是一个大目录(整个存储桶)中文件的文件名。 S3键可以包含

/
,但它没有特殊意义,除非您设置
delimiter
参数并列出一个桶

换句话说,拥有一个名为

some/object
的对象并不能告诉您有关该对象
some
的任何信息(它可能存在也可能不存在——这两个对象不相关)。

但是,您可以请求具有特定前缀的键,所以我可以说“给我所有以

some/path/to/
开头的键,它会返回
some/path/to/use
。它看起来像“列出目录”,但实际上只是在请求文件以特定字符串开头。

我可以很容易地这样命名:

somepathtousea
somepathtouseb

然后说“给我所有以

somepathtouse
开头的东西”(它会说
somepathtousea
somepathtouseb
)。

注意:S3 URL 有多种形式

http://s3.amazonaws.com/bucket/key http://bucket.s3.amazonaws.com/key http://bucket/key (where bucket is a DNS CNAME record pointing to bucket.s3.amazonaws.com)


编辑:

我看了

JavaDocs,这是我看到的函数签名(对于AmazonS3Client

):

public void deleteObject(java.lang.String bucketName, java.lang.String key) throws AmazonClientException, AmazonServiceException

再次编辑:

文件夹现在确实存在,作为内容类型为

application/x-directory

 且键以 
/
 结尾的零长度对象:

$ AWS_PROFILE=prod aws s3api head-object --bucket example-bucket --key example-directory/ { "AcceptRanges": "bytes", "LastModified": "Mon, 29 Apr 2019 14:59:36 GMT", "ContentLength": 0, "ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"", "ContentType": "application/x-directory", "ServerSideEncryption": "AES256", "Metadata": {} }

这仍然只是约定俗成,没有什么可以阻止您拥有以

/

结尾的文件或不存在的“文件夹”中的文件。


8
投票
您可能想看看这个

example,以快速参考如何从 S3 中删除对象。

delete

的语法实际上是
deleteObject( bucketName, key )
其中 bucketName 是您放置文件的存储桶,key 是您要在存储桶中删除的文件的名称。 

将桶视为您的硬盘驱动器,如 C:\、D:\ 等。并将键视为您要删除的文件的绝对路径名。


6
投票
/*这是适合我的解决方案。这里Bucket_Name是我在S3上的bucket名称,key是Bucket_Name下的路径。所以,如果 S3 上的绝对路径是:

s3://my_bucket/Path/to/my/folder

然后,下面的代码应该可以工作。 */


String Bucket_Name = "my_bucket"; String key = "Path/to/my/folder"; ObjectListing objects = s3Client.listObjects(BUCKET_NAME, key); for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) { s3Client.deleteObject(BUCKET_NAME, objectSummary.getKey()); }
    

3
投票
关于在亚马逊 S3 java 中删除文件、目录和存储桶的问题,我想提供用于删除非空 S3 存储桶的代码(

AWS 参考):

public void deleteBucket(final String bucketName) { final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { ObjectListing objectListing = s3.listObjects(bucketName); while (true) { for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator.hasNext(); ) { S3ObjectSummary summary = (S3ObjectSummary) iterator.next(); s3.deleteObject(bucketName, summary.getKey()); } if (objectListing.isTruncated()) { objectListing = s3.listNextBatchOfObjects(objectListing); } else { break; } } VersionListing versionListing = s3.listVersions(new ListVersionsRequest().withBucketName(bucketName)); while (true) { for (Iterator<?> iterator = versionListing.getVersionSummaries().iterator(); iterator.hasNext(); ) { S3VersionSummary vs = (S3VersionSummary) iterator.next(); s3.deleteVersion(bucketName, vs.getKey(), vs.getVersionId()); } if (versionListing.isTruncated()) { versionListing = s3.listNextBatchOfVersions(versionListing); } else { break; } } s3.deleteBucket(bucketName); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } }
    

2
投票
对我有用,注意截断!

long start = System.currentTimeMillis(); long totalSize = 0; int totalItems = 0; String key ="path/to/folder/" String bucket = "my-bucket" final ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(key); ObjectListing objects = s3.listObjects(listObjectsRequest); do { for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) { totalSize += objectSummary.getSize(); totalItems++; s3.deleteObject(bucketName, objectSummary.getKey()); } objects = s3.listNextBatchOfObjects(objects); } while (objects.isTruncated()); long stop = System.currentTimeMillis(); LOG.trace("User {} had {} items with {} Kb, took {} ms to delete", user.getName(), totalItems, totalSize / 1024, stop - start);
    

2
投票
ListObjectsV2Result 对我有用。尝试一次。

private void deleteObjectsInFolder() { try { ListObjectsV2Result result; do { String folderPath = " "; result = s3.listObjectsV2(Constants.BUCKET_NAME, folderPath); Log.e("count:", result.getKeyCount() + ""); if (result.getKeyCount() != 0) { for (S3ObjectSummary objectSummary : result.getObjectSummaries()) { s3.deleteObject(Constants.BUCKET_NAME, objectSummary.getKey()); } } System.out.println("Next Continuation Token : " + result.getNextContinuationToken()); } while (result.isTruncated() == true); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, " + "which means your request made it " + "to Amazon S3, but was rejected with an error response " + "for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, " + "which means the client encountered " + "an internal error while trying to communicate" + " with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
    

2
投票
批量删除 S3 存储桶中的对象列表:

public void deleteObjects(String bucketName, List<String> keys) { List<KeyVersion> bulk = new ArrayList<>(); for (int i = 0; i < keys.size(); i++) { bulk.add(new KeyVersion(keys.get(i))); if (i % 100 == 0) { try { s3Client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(bulk)); } catch (Exception e) { System.err.println(e.getErrorMessage()); } bulk.clear(); } } if (bulk.size() > 0) { try { s3Client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(bulk)); } catch (Exception e) { System.err.println(e.getErrorMessage()); } } }

来源:

http://codeflex.co/delete-objects-from-amazon-s3-bucket-using-aws-sdk-for-java/


2
投票
这行代码适用于我的情况,其中

keyName

 是文件名:

s3Client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
    

0
投票
科特林

class S3( var bucketName: String? = null, var key: String? = null, val accessKey: String? = null, val secretKey: String? = null, val region: String? = null ) fun delete( s3: S3, keyword: String = "", ) { with(s3) { val client = client(accessKey, secretKey, region) var objects = client.listObjects(bucketName, key) while (true) { for (i in objects.objectSummaries) { if (!i.key.contains(keyword)) { continue } client.deleteObject(bucketName, i.key) } if (objects.isTruncated) { objects = client.listNextBatchOfObjects(objects) } else { break } } var versions = client.listVersions(bucketName, key) while (true) { for (i in versions.versionSummaries) { if (!i.key.contains(keyword)) { continue } client.deleteVersion(bucketName, i.key, i.versionId) } if (versions.isTruncated) { versions = client.listNextBatchOfVersions(versions) } else { break } } } }
    

0
投票
这是对我有用的答案,我使用了 Danger 的答案

这需要时间,但最终会删除所有文件和文件夹。

public static void deleteObjects(AmazonS3 s3Client, String bucketName, String folderPath) { while(getFilesListInBucket(s3Client, bucketName, folderPath).size()>0) { for (S3ObjectSummary file : s3Client.listObjects(bucketName, folderPath).getObjectSummaries()) { s3Client.deleteObject(bucketName, file.getKey()); } } }
    
© www.soinside.com 2019 - 2024. All rights reserved.