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

com.alogic.s3.S3Client 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.alogic.xscript.plugins.Segment;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
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;

/**
 * s3客户端
 *
 * @since 1.6.16.6
 */
public class S3Client extends Segment {
    protected static final String CONTEXT_ID = "$s3client";

    protected String $endpoint;
    protected String $ak;
    protected String $sk;
    protected String $region = NULL;

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

    @Override
    public void configure(Properties props){
        super.configure(props);

        $endpoint = PropertiesConstants.getRaw(props,"endpoint",NULL);
        $ak = PropertiesConstants.getRaw(props,"ak",NULL);
        $sk = PropertiesConstants.getRaw(props,"sk",NULL);
        $region = PropertiesConstants.getRaw(props,"region",NULL);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String endpoint = PropertiesConstants.transform(ctx,$endpoint,NULL);
        String ak = PropertiesConstants.transform(ctx,$ak,NULL);
        String sk = PropertiesConstants.transform(ctx,$sk,NULL);
        String region = PropertiesConstants.transform(ctx,$region,NULL);

        if (StringUtils.isEmpty(endpoint)){
            log("S3 endpoint is empty","error");
            return ;
        }

        if (StringUtils.isEmpty(ak)){
            log("S3 ak is empty","error");
            return ;
        }

        if (StringUtils.isEmpty(sk)){
            log("S3 sk is empty","error");
            return ;
        }

        try {
            AmazonS3 client = AmazonS3ClientBuilder.standard()
                    .withPathStyleAccessEnabled(true)
                    .withCredentials(new AWSCredentialsProvider(){
                        @Override
                        public AWSCredentials getCredentials() {
                            return new BasicAWSCredentials(ak,sk);
                        }
                        @Override
                        public void refresh() {
                        }
                    }).withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region))
                    .build();
            ctx.setObject(CONTEXT_ID,client);
            super.onExecute(root,current,ctx,watcher);
        }catch (Exception ex){
            log(ExceptionUtils.getStackTrace(ex),"error");
            throw new BaseException("core.e1003",ex.getMessage());
        }finally {
            ctx.removeObject(CONTEXT_ID);
        }
    }

    /**
     * 基于S3Client的操作
     */
    public static abstract class Operation extends Segment{
        protected String id = "id";
        public Operation(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties props){
            super.configure(props);
            id = PropertiesConstants.getString(props,"id","$" + this.getXmlTag(),true);
        }

        @Override
        protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
            AmazonS3 client = ctx.getObject(CONTEXT_ID);
            if (client != null){
                onExecute(client,root,current,ctx,watcher);
            }
        }

        /**
         * 执行父类方法
         */
        protected void onSuperExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
            super.onExecute(root,current,ctx,watcher);
        }

        protected abstract void onExecute(
                AmazonS3 client,
                XsObject root,
                XsObject current,
                LogicletContext ctx,
                ExecuteWatcher watcher
        );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy