use of org.lucee.extension.resource.s3.info.NotExisting in project extension-s3 by lucee.
the class S3 method get.
public S3Info get(String bucketName, final String objectName) throws S3Exception {
if (Util.isEmpty(objectName)) {
return get(bucketName);
}
bucketName = improveBucketName(bucketName);
String nameFile = improveObjectName(objectName, false);
String nameDir = improveObjectName(objectName, true);
// cache
S3Info info = cacheTimeout <= 0 ? null : exists.get(toKey(bucketName, nameFile));
if (info != null && info.validUntil() >= System.currentTimeMillis()) {
if (info instanceof NotExisting)
return null;
return info;
}
info = null;
AmazonS3Client client = getAmazonS3(bucketName, null);
try {
long validUntil = System.currentTimeMillis() + cacheTimeout;
ObjectListing objects = null;
try {
ListObjectsRequest lor = new ListObjectsRequest();
lor.setBucketName(bucketName);
lor.setPrefix(nameFile);
lor.setMaxKeys(100);
objects = client.listObjects(lor);
} catch (Exception e) {
if (log != null)
log.error("s3", e);
else
e.printStackTrace();
}
/* Recursively delete all the objects inside given bucket */
if (objects == null || objects.getObjectSummaries() == null || objects.getObjectSummaries().size() == 0) {
// we do not return this, we just store it to cache that it
exists.put(toKey(bucketName, objectName), new NotExisting(bucketName, objectName, validUntil, log));
// does
return null;
}
String targetName;
S3ObjectSummary stoObj = null;
int count = 0;
// while (true) {
for (S3ObjectSummary summary : objects.getObjectSummaries()) {
count++;
// direct match
targetName = summary.getKey();
if (nameFile.equals(targetName) || nameDir.equals(targetName)) {
exists.put(toKey(bucketName, nameFile), info = new StorageObjectWrapper(this, stoObj = summary, validUntil, log));
}
// pseudo directory?
// if (info == null) {
targetName = summary.getKey();
if (nameDir.length() < targetName.length() && targetName.startsWith(nameDir)) {
exists.put(toKey(bucketName, nameFile), info = new ParentObject(this, bucketName, nameDir, validUntil, log));
}
// set the value to exist when not a match
if (!(stoObj != null && stoObj.equals(summary))) {
exists.put(toKey(summary.getBucketName(), summary.getKey()), new StorageObjectWrapper(this, summary, validUntil, log));
}
// set all the parents when not exist
// TODO handle that also a file with that name can exist at the same time
String parent = nameFile;
int index;
while ((index = parent.lastIndexOf('/')) != -1) {
parent = parent.substring(0, index);
exists.put(toKey(bucketName, parent), new ParentObject(this, bucketName, parent, validUntil, log));
}
}
// }
if (info == null) {
// we do not return this, we just store it to cache that it does
exists.put(// we do not return this, we just store it to cache that it does
toKey(bucketName, objectName), // we do not return this, we just store it to cache that it does
new NotExisting(bucketName, objectName, validUntil, log));
}
return info;
} catch (AmazonServiceException ase) {
throw toS3Exception(ase);
} finally {
client.release();
}
}
Aggregations