All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alogic.s3.S3InfoGet Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.alogic.s3;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;


/**
 * 获取对象的metadata
 *
 * @since 1.6.16.6
 */
public class S3InfoGet extends S3Client.Operation{
    protected String $name;
    protected String $path;
    protected String cid = S3Metadata.CONTEXT_ID;

    public S3InfoGet(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties props){
        super.configure(props);
        $name = PropertiesConstants.getRaw(props,"name",NULL);
        $path = PropertiesConstants.getRaw(props,"path",NULL);
        cid = PropertiesConstants.getString(props,"cid",cid);
    }
    @Override
    protected void onExecute(AmazonS3 client, XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        boolean result = false;
        try {
            String name = PropertiesConstants.transform(ctx, $name, NULL);
            if (StringUtils.isEmpty(name)) {
                log("Bucket name is empty", "error");
                return;
            }

            String path = PropertiesConstants.transform(ctx,$path,NULL);
            if (StringUtils.isEmpty(path)){
                log("Object path is empty", "error");
                return;
            }

            try {
                ObjectMetadata metadata = client.getObjectMetadata(name,path);
                if (metadata != null){
                    try {
                        PropertiesConstants.setString(ctx,"$obj.name",path);
                        PropertiesConstants.setString(ctx,"$obj.etag",metadata.getETag());
                        PropertiesConstants.setLong(ctx,"$obj.lastModified",metadata.getLastModified().getTime());
                        PropertiesConstants.setLong(ctx,"$obj.size",metadata.getInstanceLength());
                        ctx.setObject(cid, metadata);
                        onSuperExecute(root,current,ctx,watcher);
                    }finally {
                        ctx.removeObject(cid);
                    }
                    result = true;
                }
            }catch (Exception ex) {
                log(ex.getMessage(), "error");
            }
        }finally {
            PropertiesConstants.setBoolean(ctx,id,result);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy