AWS S3:获取上次修改的时间戳 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36153110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:00:36  来源:igfitidea点击:

AWS S3 : Get Last Modified Timestamp Java

javaamazon-s3

提问by Codex

I have written a java code to get the list of files in a AWS S3 bucket's folder as a list of strings. Is there any direct function that I could use to get the last modified timestamp of a file that we see in the s3 buckets.

我编写了一个 java 代码来获取 AWS S3 存储桶文件夹中的文件列表作为字符串列表。是否有任何直接函数可以用来获取我们在 s3 存储桶中看到的文件的最后修改时间戳。

回答by H6.

You can get the lastModifiedas a java.util.Datevia the S3ObjectSummaryobject.

您可以通过S3ObjectSummary对象获取lastModifiedas a 。java.util.Date

// ...
ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request()
    .withBucketName("my-bucket")
    .withMaxKeys(1000);

ListObjectsV2Result result = s3client.listObjectsV2(listObjectsV2Request);

for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
    // objectSummary.getLastModified() as java.util.Date
}

回答by gil.fernandes

If you want to know the last modified timestamp of a specific file without listing the whole directory you can use a GetObjectRequestand then retrieve from it the ObjectMetadata. Here is an example:

如果您想知道特定文件的最后修改时间戳而不列出整个目录,您可以使用 aGetObjectRequest然后从中检索 ObjectMetadata。下面是一个例子:

S3Object fullObject = s3Client.getObject(new GetObjectRequest(bucketName, remoteFilePath));
ObjectMetadata objectMetadata = fullObject.getObjectMetadata();
Date lastModified = objectMetadata.getLastModified();

回答by koolhead17

You can alternatively use minio-javaclient library for same. Its Open Source and compatible with AWS S3 API.

您也可以使用minio-java客户端库。其开源并与 AWS S3 API 兼容。

Using ListBucketsAPI, you can easily implement it.

使用ListBucketsAPI,您可以轻松实现它。

try {
  // List buckets that have read access.
  List bucketList = minioClient.listBuckets();
  for (Bucket bucket : bucketList) {
      System.out.println(bucket.creationDate() + ", " + bucket.name());
  }
} catch (MinioException e) {
  System.out.println("Error occured: " + e);
}

Hope it helps.

希望能帮助到你。

Disclaimer: I work for Minio

免责声明:我为Minio工作