Search in sources :

Example 1 with S3BucketWrapper

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

the class S3 method setMetaData.

public void setMetaData(String bucketName, String objectName, Struct metadata) throws PageException, S3Exception {
    Decision dec = CFMLEngineFactory.getInstance().getDecisionUtil();
    Cast cas = CFMLEngineFactory.getInstance().getCastUtil();
    Iterator<Entry<Key, Object>> it = metadata.entryIterator();
    Entry<Key, Object> e;
    ObjectMetadata metadataCopy = new ObjectMetadata();
    Map<String, String> data = new ConcurrentHashMap<String, String>();
    while (it.hasNext()) {
        e = it.next();
        metadataCopy.addUserMetadata(toMetaDataKey(e.getKey()), cas.toString(e.getValue()));
    }
    bucketName = improveBucketName(bucketName);
    objectName = improveObjectName(objectName);
    if (!Util.isEmpty(objectName)) {
        AmazonS3Client client = getAmazonS3(bucketName, null);
        try {
            S3BucketWrapper bw = get(bucketName);
            if (bw == null)
                throw new S3Exception("there is no bucket [" + bucketName + "]");
            Bucket b = bw.getBucket();
            CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName).withNewObjectMetadata(metadataCopy);
            client.copyObject(request);
            flushExists(bucketName, objectName);
        } finally {
            client.release();
        }
    } else
        // TOOD possible?
        throw new S3Exception("cannot set metadata for a bucket");
}
Also used : Cast(lucee.runtime.util.Cast) S3BucketWrapper(org.lucee.extension.resource.s3.info.S3BucketWrapper) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) Decision(lucee.runtime.util.Decision) Entry(java.util.Map.Entry) AmazonS3Client(org.lucee.extension.resource.s3.pool.AmazonS3Client) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) Bucket(com.amazonaws.services.s3.model.Bucket) ParentObject(org.lucee.extension.resource.s3.info.ParentObject) S3Object(com.amazonaws.services.s3.model.S3Object) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Key(lucee.runtime.type.Collection.Key)

Example 2 with S3BucketWrapper

use of org.lucee.extension.resource.s3.info.S3BucketWrapper 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 3 with S3BucketWrapper

use of org.lucee.extension.resource.s3.info.S3BucketWrapper 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)

Example 4 with S3BucketWrapper

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

the class S3 method list.

// https://s3.eu-central-1.wasabisys.com/lucee-ldev0359-43d24e88acb9b5048097f8961fc5b23c/a
public List<S3Info> list(boolean recursive, boolean listPseudoFolder) throws S3Exception {
    AmazonS3Client client = null;
    try {
        // no cache for buckets
        if (cacheTimeout <= 0 || buckets == null || buckets.validUntil < System.currentTimeMillis()) {
            client = getAmazonS3(null, null);
            List<Bucket> s3buckets = client.listBuckets();
            long now = System.currentTimeMillis();
            buckets = new ValidUntilMap<S3BucketWrapper>(now + cacheTimeout);
            for (Bucket s3b : s3buckets) {
                buckets.put(s3b.getName(), new S3BucketWrapper(this, s3b, now + cacheTimeout, log));
            }
        }
        List<S3Info> list = new ArrayList<S3Info>();
        Iterator<S3BucketWrapper> it = buckets.values().iterator();
        S3Info info;
        while (it.hasNext()) {
            info = it.next();
            list.add(info);
            if (recursive) {
                Iterator<S3Info> iit = list(info.getBucketName(), "", recursive, listPseudoFolder, true).iterator();
                while (iit.hasNext()) {
                    list.add(iit.next());
                }
            }
        }
        return list;
    } catch (AmazonServiceException se) {
        throw toS3Exception(se);
    } finally {
        if (client != null)
            client.release();
    }
}
Also used : S3BucketWrapper(org.lucee.extension.resource.s3.info.S3BucketWrapper) AmazonS3Client(org.lucee.extension.resource.s3.pool.AmazonS3Client) Bucket(com.amazonaws.services.s3.model.Bucket) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Info(org.lucee.extension.resource.s3.info.S3Info)

Aggregations

Bucket (com.amazonaws.services.s3.model.Bucket)4 S3BucketWrapper (org.lucee.extension.resource.s3.info.S3BucketWrapper)4 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)3 S3Info (org.lucee.extension.resource.s3.info.S3Info)3 AmazonS3Client (org.lucee.extension.resource.s3.pool.AmazonS3Client)3 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)2 ParentObject (org.lucee.extension.resource.s3.info.ParentObject)2 StorageObjectWrapper (org.lucee.extension.resource.s3.info.StorageObjectWrapper)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)1 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)1 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 S3Object (com.amazonaws.services.s3.model.S3Object)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Key (lucee.runtime.type.Collection.Key)1 Cast (lucee.runtime.util.Cast)1 Decision (lucee.runtime.util.Decision)1