Search in sources :

Example 1 with StorageObjectWrapper

use of org.lucee.extension.resource.s3.info.StorageObjectWrapper 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;
}
Also used : StorageObjectWrapper(org.lucee.extension.resource.s3.info.StorageObjectWrapper) S3Info(org.lucee.extension.resource.s3.info.S3Info)

Example 2 with StorageObjectWrapper

use of org.lucee.extension.resource.s3.info.StorageObjectWrapper 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();
    }
}
Also used : NotExisting(org.lucee.extension.resource.s3.info.NotExisting) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) PageException(lucee.runtime.exp.PageException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) ParentObject(org.lucee.extension.resource.s3.info.ParentObject) ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) AmazonS3Client(org.lucee.extension.resource.s3.pool.AmazonS3Client) StorageObjectWrapper(org.lucee.extension.resource.s3.info.StorageObjectWrapper) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Info(org.lucee.extension.resource.s3.info.S3Info)

Example 3 with StorageObjectWrapper

use of org.lucee.extension.resource.s3.info.StorageObjectWrapper in project extension-s3 by lucee.

the class S3 method _list.

private ValidUntilMap<S3Info> _list(String bucketName, String objectName, boolean onlyChildren, boolean noCache) throws S3Exception {
    try {
        String key = toKey(bucketName, objectName);
        String nameDir = improveObjectName(objectName, true);
        String nameFile = improveObjectName(objectName, false);
        boolean hasObjName = !Util.isEmpty(objectName);
        // not cached
        ValidUntilMap<S3Info> _list = cacheTimeout <= 0 || noCache ? null : objects.get(key);
        if (_list == null || _list.validUntil < System.currentTimeMillis()) {
            long validUntil = System.currentTimeMillis() + cacheTimeout;
            _list = new ValidUntilMap<S3Info>(validUntil);
            objects.put(key, _list);
            // add bucket
            if (!hasObjName && !onlyChildren) {
                outer: while (true) {
                    AmazonS3Client client = getAmazonS3(null, null);
                    try {
                        for (Bucket b : client.listBuckets()) {
                            // TOD is there a more direct way?
                            if (b.getName().equals(bucketName)) {
                                _list.put("", new S3BucketWrapper(this, b, validUntil, log));
                                break outer;
                            }
                        }
                    } finally {
                        client.release();
                    }
                    // should never happen!
                    throw new S3Exception("could not find bucket [" + bucketName + "]");
                }
            }
            AmazonS3Client client = getAmazonS3(bucketName, null);
            ObjectListing list = (hasObjName ? client.listObjects(bucketName, nameFile) : client.listObjects(bucketName));
            try {
                if (list != null && list.getObjectSummaries() != null) {
                    while (true) {
                        List<S3ObjectSummary> kids = list.getObjectSummaries();
                        StorageObjectWrapper tmp;
                        String name;
                        for (S3ObjectSummary kid : kids) {
                            name = kid.getKey();
                            tmp = new StorageObjectWrapper(this, kid, validUntil, log);
                            if (!hasObjName || name.equals(nameFile) || name.startsWith(nameDir))
                                _list.put(kid.getKey(), tmp);
                            exists.put(toKey(kid.getBucketName(), name), tmp);
                            int index;
                            while ((index = name.lastIndexOf('/')) != -1) {
                                name = name.substring(0, index);
                                exists.put(toKey(bucketName, name), new ParentObject(this, bucketName, name, validUntil, log));
                            }
                        }
                        if (list.isTruncated()) {
                            list = client.listNextBatchOfObjects(list);
                        } else {
                            break;
                        }
                    }
                }
            } finally {
                client.release();
            }
        }
        return _list;
    } catch (AmazonS3Exception ase) {
        throw toS3Exception(ase);
    }
}
Also used : S3BucketWrapper(org.lucee.extension.resource.s3.info.S3BucketWrapper) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) ParentObject(org.lucee.extension.resource.s3.info.ParentObject) AmazonS3Client(org.lucee.extension.resource.s3.pool.AmazonS3Client) StorageObjectWrapper(org.lucee.extension.resource.s3.info.StorageObjectWrapper) Bucket(com.amazonaws.services.s3.model.Bucket) S3Info(org.lucee.extension.resource.s3.info.S3Info)

Example 4 with StorageObjectWrapper

use of org.lucee.extension.resource.s3.info.StorageObjectWrapper in project extension-s3 by lucee.

the class S3 method setLastModified.

public void setLastModified(String bucketName, String objectName, long time) throws S3Exception {
    bucketName = improveBucketName(bucketName);
    objectName = improveObjectName(objectName);
    if (!Util.isEmpty(objectName)) {
        if (Util.isEmpty(objectName)) {
            S3BucketWrapper bw = get(bucketName);
            if (bw == null)
                throw new S3Exception("there is no bucket [" + bucketName + "]");
            Bucket b = bw.getBucket();
            // not sure if that is ok that way
            b.setCreationDate(new Date(time));
        } else {
            S3Info info = get(bucketName, objectName);
            if (info == null || info.isVirtual())
                throw new S3Exception("there is no object [" + objectName + "] in bucket [" + bucketName + "]");
            S3ObjectSummary so = ((StorageObjectWrapper) info).getStorageObject();
            so.setLastModified(new Date(time));
        }
    }
// TDOD add for bucket?
}
Also used : S3BucketWrapper(org.lucee.extension.resource.s3.info.S3BucketWrapper) StorageObjectWrapper(org.lucee.extension.resource.s3.info.StorageObjectWrapper) Bucket(com.amazonaws.services.s3.model.Bucket) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) S3Info(org.lucee.extension.resource.s3.info.S3Info) Date(java.util.Date)

Aggregations

S3Info (org.lucee.extension.resource.s3.info.S3Info)4 StorageObjectWrapper (org.lucee.extension.resource.s3.info.StorageObjectWrapper)4 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)3 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)3 Bucket (com.amazonaws.services.s3.model.Bucket)2 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)2 ParentObject (org.lucee.extension.resource.s3.info.ParentObject)2 S3BucketWrapper (org.lucee.extension.resource.s3.info.S3BucketWrapper)2 AmazonS3Client (org.lucee.extension.resource.s3.pool.AmazonS3Client)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)1 IOException (java.io.IOException)1 Date (java.util.Date)1 PageException (lucee.runtime.exp.PageException)1 NotExisting (org.lucee.extension.resource.s3.info.NotExisting)1