use of org.lucee.extension.resource.s3.info.S3Info in project extension-s3 by lucee.
the class S3 method getInfo.
public S3ObjectSummary getInfo(String bucketName, String objectName) throws S3Exception {
bucketName = improveBucketName(bucketName);
objectName = improveObjectName(objectName);
S3Info info = get(bucketName, objectName);
if (info instanceof StorageObjectWrapper)
return ((StorageObjectWrapper) info).getStorageObject();
return null;
}
use of org.lucee.extension.resource.s3.info.S3Info 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();
}
}
use of org.lucee.extension.resource.s3.info.S3Info in project extension-s3 by lucee.
the class S3 method add.
private void add(Map<String, S3Info> map, S3Info info, String prefix, boolean recursive, boolean addPseudoEntries, boolean onlyChildren) throws S3Exception {
String nameFile = improveObjectName(info.getObjectName(), false);
int index, last = 0;
ParentObject tmp;
String objName;
S3Info existing;
if (addPseudoEntries) {
while ((index = nameFile.indexOf('/', last)) != -1) {
tmp = new ParentObject(this, nameFile.substring(0, index + 1), info, log);
if (doAdd(tmp, prefix, recursive, onlyChildren)) {
objName = improveObjectName(tmp.getObjectName(), false);
existing = map.get(objName);
if (existing == null) {
map.put(objName, tmp);
}
}
last = index + 1;
}
}
if (doAdd(info, prefix, recursive, onlyChildren)) {
objName = improveObjectName(info.getObjectName(), false);
existing = map.get(objName);
if (existing == null || existing instanceof ParentObject)
map.put(objName, info);
}
}
use of org.lucee.extension.resource.s3.info.S3Info in project extension-s3 by lucee.
the class S3Resource method listResources.
@Override
public Resource[] listResources() {
S3Resource[] children = null;
// long timeout=System.currentTimeMillis()+provider.getCache();
try {
boolean buckets = false;
List<S3Info> list = null;
if (isRoot()) {
buckets = true;
list = s3.list(false, false);
} else if (isDirectory()) {
list = isBucket() ? s3.list(bucketName, "", false, true, true) : s3.list(bucketName, objectName + "/", false, true, true);
}
if (list != null) {
Iterator<S3Info> it = list.iterator();
children = new S3Resource[list.size()];
S3Info si;
int index = 0;
while (it.hasNext()) {
si = it.next();
children[index] = new S3Resource(engine, s3, props, location, provider, si.getBucketName(), buckets ? "" : S3.improveObjectName(si.getObjectName(), false));
index++;
}
}
} catch (S3Exception e) {
throw CFMLEngineFactory.getInstance().getCastUtil().toPageRuntimeException(e);
}
return children;
}
use of org.lucee.extension.resource.s3.info.S3Info in project extension-s3 by lucee.
the class TestCase method main.
public static void main(String[] args) throws IOException {
S3Properties prop = new S3Properties();
prop.setAccessKeyId("...");
prop.setSecretAccessKey("...");
final Charset UTF8 = Charset.forName("UTF-8");
String bucketName = "testinjavax";
String dir1 = "test/";
String dir2 = "test/sub1/";
String dir3 = "test/sub1/sub2/";
String file1 = "test/sub1/sub2/c.txt";
String file2 = "test/sub1/d.txt";
// 52568
long start = System.currentTimeMillis();
S3 s3 = new S3(prop, 100000, 100000, 100000, Regions.US_EAST_2.toString(), true, null);
try {
if (s3.exists(bucketName)) {
s3.delete(bucketName, true);
}
List<S3Info> buckets = s3.list(false, false);
// dump("Buckets",buckets);
int initalBucketCount = buckets.size();
// create/delete bucket
assertFalse(s3.exists(bucketName));
s3.createDirectory(bucketName, null, null);
assertTrue(s3.exists(bucketName));
s3.delete(bucketName, true);
assertFalse(s3.exists(bucketName));
assertFalse(s3.exists(bucketName, dir1));
assertFalse(s3.isDirectory(bucketName, dir1));
assertFalse(s3.isFile(bucketName, dir1));
assertFalse(s3.exists(bucketName, dir2));
assertFalse(s3.isDirectory(bucketName, dir2));
assertFalse(s3.isFile(bucketName, dir2));
assertFalse(s3.exists(bucketName, dir3));
assertFalse(s3.isDirectory(bucketName, dir3));
assertFalse(s3.isFile(bucketName, dir3));
s3.createDirectory(bucketName, dir3, null, null);
assertTrue(s3.exists(bucketName));
assertTrue(s3.isDirectory(bucketName, dir1));
assertFalse(s3.isFile(bucketName, dir1));
assertTrue(s3.exists(bucketName, dir1));
assertTrue(s3.isDirectory(bucketName, dir2));
assertFalse(s3.isFile(bucketName, dir2));
assertTrue(s3.exists(bucketName, dir2));
assertTrue(s3.exists(bucketName, dir3));
assertTrue(s3.isDirectory(bucketName, dir3));
assertFalse(s3.isFile(bucketName, dir3));
buckets = s3.list(false, false);
contains(buckets, bucketName, null);
assertEquals(initalBucketCount + 1, buckets.size());
// must fail
boolean failed = false;
try {
s3.delete(bucketName, false);
} catch (S3Exception e) {
failed = true;
}
assertTrue(failed);
s3.delete(bucketName, true);
assertFalse(s3.exists(bucketName));
assertFalse(s3.exists(bucketName, dir1));
assertFalse(s3.exists(bucketName, dir2));
assertFalse(s3.exists(bucketName, dir3));
// second round
s3.createDirectory(bucketName, dir3, null, null);
List<S3Info> recPseu = s3.list(bucketName, null, true, true, false);
assertEquals(4, recPseu.size());
contains(recPseu, bucketName, dir1);
contains(recPseu, bucketName, dir2);
contains(recPseu, bucketName, dir3);
List<S3Info> rec = s3.list(bucketName, null, true, false, false);
assertEquals(2, rec.size());
contains(rec, bucketName, dir3);
List<S3Info> pseu = s3.list(bucketName, null, false, true, false);
assertEquals(2, pseu.size());
contains(pseu, bucketName, dir1);
assertTrue(s3.exists(bucketName));
assertTrue(s3.exists(bucketName, dir1));
assertTrue(s3.exists(bucketName, dir2));
assertTrue(s3.exists(bucketName, dir3));
s3.delete(bucketName, dir3, false);
assertTrue(s3.exists(bucketName));
assertTrue(s3.exists(bucketName, dir1));
assertTrue(s3.exists(bucketName, dir2));
assertFalse(s3.exists(bucketName, dir3));
s3.delete(bucketName, dir2, false);
assertTrue(s3.exists(bucketName));
assertTrue(s3.exists(bucketName, dir1));
assertFalse(s3.exists(bucketName, dir2));
assertFalse(s3.exists(bucketName, dir3));
s3.delete(bucketName, dir1, false);
assertTrue(s3.exists(bucketName));
assertFalse(s3.exists(bucketName, dir1));
assertFalse(s3.exists(bucketName, dir2));
assertFalse(s3.exists(bucketName, dir3));
s3.delete(bucketName, false);
assertFalse(s3.exists(bucketName));
assertFalse(s3.exists(bucketName, dir1));
assertFalse(s3.exists(bucketName, dir2));
assertFalse(s3.exists(bucketName, dir3));
// third round
s3.createDirectory(bucketName, dir3, null, null);
assertTrue(s3.exists(bucketName));
assertTrue(s3.exists(bucketName, dir1));
assertTrue(s3.exists(bucketName, dir2));
assertTrue(s3.exists(bucketName, dir3));
s3.delete(bucketName, dir1, true);
assertTrue(s3.exists(bucketName));
assertFalse(s3.exists(bucketName, dir1));
assertFalse(s3.exists(bucketName, dir2));
assertFalse(s3.exists(bucketName, dir3));
s3.delete(bucketName, false);
// create File
s3.createFile(bucketName, file1, null, null);
assertTrue(s3.exists(bucketName, dir1));
assertTrue(s3.exists(bucketName, dir2));
assertTrue(s3.exists(bucketName, dir3));
assertTrue(s3.exists(bucketName, file1));
assertFalse(s3.isDirectory(bucketName, file1));
assertTrue(s3.isFile(bucketName, file1));
s3.delete(bucketName, file1, false);
assertTrue(s3.exists(bucketName, dir1));
assertTrue(s3.exists(bucketName, dir2));
assertTrue(s3.exists(bucketName, dir3));
assertFalse(s3.exists(bucketName, file1));
assertFalse(s3.isDirectory(bucketName, file1));
assertFalse(s3.isFile(bucketName, file1));
s3.write(bucketName, file1, "Susi", "test/plain", UTF8, null, null);
S3Object obj = s3.getData(bucketName, file1);
InputStream is = obj.getObjectContent();
try {
assertEquals("Susi", Util.toString(is));
} finally {
is.close();
}
s3.delete(bucketName, true);
// Copy
s3.write(bucketName, file1, "Susi", "test/plain", UTF8, null, null);
s3.copy(bucketName, file1, bucketName, file2, null, null);
List<S3Info> list = s3.list(bucketName, dir1, true, true, true);
assertEquals(4, list.size());
contains(list, bucketName, dir2);
contains(list, bucketName, dir3);
contains(list, bucketName, file1);
contains(list, bucketName, file2);
s3.delete(bucketName, true);
// Move
s3.write(bucketName, file1, "Susi", "test/plain", UTF8, null, null);
s3.move(bucketName, file1, bucketName, file2, null, null);
list = s3.list(bucketName, dir1, true, true, true);
assertEquals(3, list.size());
contains(list, bucketName, dir2);
contains(list, bucketName, dir3);
contains(list, bucketName, file2);
// S3
bucketName = "testcases3567";
if (s3.isDirectory(bucketName, null))
s3.delete(bucketName, true);
assertFalse(s3.exists(bucketName));
assertFalse(s3.exists(bucketName, null));
assertFalse(s3.isDirectory(bucketName, null));
assertFalse(s3.isFile(bucketName, null));
s3.createDirectory(bucketName, null, null, null);
assertTrue(s3.exists(bucketName));
assertTrue(s3.exists(bucketName, null));
assertFalse(s3.isFile(bucketName, null));
assertTrue(s3.isDirectory(bucketName, null));
String sub = "a";
if (!s3.isFile(bucketName, sub))
;
s3.write(bucketName, sub, "", null, null, null, null);
assertTrue(s3.exists(bucketName, sub));
assertTrue(s3.isFile(bucketName, sub));
assertFalse(s3.isDirectory(bucketName, sub));
String path = "aa/b/c.txt";
String parent = "aa/b";
String gparent = "aa/";
s3.write(bucketName, path, "Susi Sorglos", null, null, null, null);
assertTrue(s3.isFile(bucketName, path));
assertTrue(s3.isDirectory(bucketName, parent));
assertTrue(s3.isDirectory(bucketName, gparent));
list = s3.list(bucketName, parent, true, true, true);
assertEquals(1, list.size());
assertEquals("aa/b/c.txt", list.get(0).getName());
assertEquals(bucketName, list.get(0).getBucketName());
list = s3.list(bucketName, gparent, true, true, true);
assertEquals(2, list.size());
assertEquals("aa/b/", list.get(0).getName());
assertEquals("aa/b/c.txt", list.get(1).getName());
// assertEquals(1, list.size());
s3.delete(bucketName, true);
} finally {
// print.e("exe-time:"+(System.currentTimeMillis()-start));
}
}
Aggregations