答案 0 :(得分:1)
这可能会有所帮助..
S3Service s3Service = new S3Service(providerCredentials);
Date tempDate = null;
S3Object recentObj = null;
for(S3Object object : s3Service.listObjects("bucketName")) {
modifiedDate =(Date)object.getMetadata(BaseStorageItem.METADATA_HEADER_LAST_MODIFIED_DATE);
if(tempDate == null) {
tempDate = modifiedDate;
} else {
if(modifiedDate.compareTo(tempDate) < 0) {
tempDate = modifiedDate;
recentObj = object;
}
}
}
我还没有测试过这段代码。希望这会有所帮助..
答案 1 :(得分:0)
使用Spring Boot:
@Service
public class AWSS3Service {
private static final Logger LOGGER = LogManager.getLogger(AWSS3Service.class);
@Autowired
private AmazonS3 amazonS3;
@Value("${aws.s3.bucket}")
private String bucketName;
@Value("${aws.s3.folder.download}")
private String foldernameDownload;
public byte[] downloadFile(final String typeName) {
byte[] content = null;
LOGGER.info("Downloading most recent file from {}", typeName);
ObjectListing objects = amazonS3.listObjects(bucketName,foldernameDownload+"/"+ typeName+"/");
Date tempDate = null;
S3ObjectSummary recentObj = null;
List<S3ObjectSummary> objectSummaries = objects.getObjectSummaries();
for (S3ObjectSummary objectSummary : objectSummaries) {
if (objectSummary.getSize()<=0)
continue;
Date modifiedDate = objectSummary.getLastModified();
if(tempDate == null || modifiedDate.compareTo(tempDate) > 0) {
tempDate = modifiedDate;
recentObj = objectSummary;
}
}
String obKey ="";
if(recentObj != null)
obKey = recentObj.getKey();
else
return content;//no files found
S3Object s3Object = amazonS3.getObject(bucketName,obKey);
S3ObjectInputStream stream = s3Object.getObjectContent();
try {
content = IOUtils.toByteArray(stream);
LOGGER.info("File downloaded successfully.");
s3Object.close();
} catch(final IOException ex) {
LOGGER.error("IO Error Message= {}", ex.getMessage());
}
return content;
}
}