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

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

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.anysoft.util.BaseException;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;


/**
 * 将文件内容load到变量,限小文本文件
 *
 * @since 1.6.16.6
 */
public class S3Load extends S3Client.Operation{
    protected String $name;
    protected String $path;
    protected String $dft = NULL;
    protected int contentLength = 10240;

    public S3Load(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);
        $dft = PropertiesConstants.getRaw(props,"dft",NULL);
        contentLength = PropertiesConstants.getInt(props,"contentLength",contentLength);
        contentLength = contentLength <= 0? 1024:contentLength;
    }
    @Override
    protected void onExecute(AmazonS3 client, XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String result = NULL;
        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 {
                boolean exist = client.doesBucketExistV2(name);
                if (exist){
                    result = client.getObjectAsString(name,path);
                }
            } catch (Exception ex) {
                log(ExceptionUtils.getStackTrace(ex), "error");
                throw new BaseException("core.e1003", ex.getMessage());
            }
        }finally {
            PropertiesConstants.setString(ctx,id,
                    StringUtils.isEmpty(result) ? PropertiesConstants.transform(ctx,$dft,NULL):result
            );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy