Example usage for com.amazonaws.services.s3 AmazonS3ClientBuilder standard

List of usage examples for com.amazonaws.services.s3 AmazonS3ClientBuilder standard

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3ClientBuilder standard.

Prototype

public static AmazonS3ClientBuilder standard() 

Source Link

Usage

From source file:org.lendingclub.mercator.aws.S3BucketScanner.java

License:Apache License

@Override
protected AmazonS3Client createClient() {
    return (AmazonS3Client) builder.configure(AmazonS3ClientBuilder.standard()).build();
}

From source file:org.ow2.proactive.scheduler.examples.S3ConnectorUtils.java

License:Open Source License

/**
 * Get or initialize the S3 client./*from  w  ww  .ja  va2s  .co  m*/
 * Note: this method must be synchronized because we're accessing the
 * field and we're calling this method from a worker thread.
 *
 * @return the S3 client
 */
protected static synchronized AmazonS3 getS3Client(String accessKey, String secretKey, String... args) {

    BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(credentials));

    if (args.length == 1) {
        builder = builder.withRegion(args[0]);

    } else {
        String endpoint = args[0] + "://" + args[1];
        String clientRegion = null;
        if (!ServiceUtils.isS3USStandardEndpoint(endpoint) && (clientRegion = AwsHostNameUtils
                .parseRegion(args[1], AmazonS3Client.S3_SERVICE_NAME)) == null) {
            throw new IllegalArgumentException("Invalid region in " + args[1]);
        }
        builder = builder
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, clientRegion));
    }
    builder = builder.withPathStyleAccessEnabled(true);
    return builder.build();
}

From source file:org.pentaho.amazon.client.impl.S3ClientFactory.java

License:Apache License

@Override
public S3Client createClient(String accessKey, String secretKey, String region) {
    AmazonClientCredentials clientCredentials = new AmazonClientCredentials(accessKey, secretKey, region);

    AmazonS3 awsS3Client = AmazonS3ClientBuilder.standard().withRegion(clientCredentials.getRegion())
            .withCredentials(new AWSStaticCredentialsProvider(clientCredentials.getAWSCredentials())).build();

    S3Client s3Client = new S3ClientImpl(awsS3Client);

    return s3Client;
}

From source file:org.pentaho.di.trans.steps.s3csvinput.S3CsvInputMeta.java

License:Apache License

public AmazonS3 getS3Client(VariableSpace space) throws SdkClientException {
    if (!getUseAwsDefaultCredentials()) {
        // Handle legacy credentials ( embedded in the step ).  We'll force a region since it not specified and
        // then turn on GlobalBucketAccess so if the files accessed are elsewhere it won't matter.
        BasicAWSCredentials credentials = new BasicAWSCredentials(
                Encr.decryptPasswordOptionallyEncrypted(space.environmentSubstitute(awsAccessKey)),
                Encr.decryptPasswordOptionallyEncrypted(space.environmentSubstitute(awsSecretKey)));

        return AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(Regions.US_EAST_1).enableForceGlobalBucketAccess().build();
    } else {//from w ww.ja va  2 s.c  om
        // Get Credentials the new way
        return AmazonS3ClientBuilder.standard().enableForceGlobalBucketAccess().build();
    }
}

From source file:org.restcomm.connect.commons.amazonS3.S3AccessTool.java

License:Open Source License

public AmazonS3 getS3client() {
    BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, securityKey);

    if (testing && (!testingUrl.isEmpty() || !testingUrl.equals(""))) {
        s3client = new AmazonS3Client(awsCreds);
        s3client.setRegion(Region.getRegion(Regions.fromName(bucketRegion)));
        s3client.setEndpoint(testingUrl);
    } else {//www.j  a va  2s.co m
        s3client = AmazonS3ClientBuilder.standard().withRegion(Regions.fromName(bucketRegion))
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();
    }

    return s3client;
}

From source file:org.springfield.lou.servlet.LouServlet.java

License:Open Source License

private String handleFileUpload(HttpServletRequest request) {
    System.out.println("HANDLE FILE UPLOAD");
    try {// w  w  w  .  j a  v a2  s . co m
        String targetid = request.getParameter("targetid");
        System.out.println("TARGETID UPLOAD=" + targetid);
        String screenid = request.getParameter("screenid");
        String cfilename = request.getParameter("cfilename");
        System.out.println("CFILENAME=" + cfilename);
        String cfilesize = request.getParameter("cfilesize");
        System.out.println("CFILESIZE=" + cfilesize);

        Html5ApplicationInterface app = null;
        String url = request.getRequestURI();
        int pos = url.indexOf("/domain/");
        if (pos != -1) {
            String tappname = url.substring(pos);
            app = ApplicationManager.instance().getApplication(tappname);
        }
        Screen eventscreen = app.getScreen(screenid);

        if (eventscreen == null)
            return null;

        String method = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/method");
        System.out.println("METHOD=" + method);

        String destpath = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/destpath");
        System.out.println("DESTPATH=" + destpath + " T=" + targetid);
        if (destpath == null || destpath.equals("")) {
            setUploadError(eventscreen, targetid, "destpath not set");
            return null;
        }

        String destname_prefix = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/destname_prefix");
        if (destname_prefix == null || destname_prefix.equals("")) {
            setUploadError(eventscreen, targetid, "destname_prefix not set");
            return null;
        }

        String filetype = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/filetype");
        if (filetype == null || filetype.equals("")) {
            setUploadError(eventscreen, targetid, "filetype not set");
            return null;
        }

        String fileext = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/fileext");
        if (fileext == null || fileext.equals("")) {
            setUploadError(eventscreen, targetid, "fileext not set");
            return null;
        }

        String checkupload = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/checkupload");
        if (checkupload == null || checkupload.equals("")) {
            setUploadError(eventscreen, targetid, "checkupload not set");
            return null;
        }

        String storagehost = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/storagehost");
        if (storagehost == null || storagehost.equals("")) {
            setUploadError(eventscreen, targetid, "storagehost not set");
            return null;
        }

        String destname_type = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/destname_type");
        if (destname_type == null || destname_type.equals("")) {
            setUploadError(eventscreen, targetid, "destname_type not set");
            return null;
        }

        String publicpath = eventscreen.getModel()
                .getProperty("/screen['upload']/target['" + targetid + "']/publicpath");
        if (publicpath == null || publicpath.equals("")) {
            setUploadError(eventscreen, targetid, "publicpath not set");
            return null;
        }

        // here we can check if its a valid upload based on filename and other specs and kill if needed, also map real extension 

        fileext = getValidExtension(fileext, cfilename);
        if (fileext == null)
            return null; // kill the request its not a valid format

        if (method.equals("s3amazon")) {
            String bucketname = eventscreen.getModel()
                    .getProperty("/screen['upload']/target['" + targetid + "']/bucketname");
            if (bucketname == null || bucketname.equals("")) {
                setUploadError(eventscreen, targetid, "bucketname not set");
                return null;
            }

            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new EnvironmentVariableCredentialsProvider()).build();
            String filename = "unknown";
            int storageport = 22;

            if (destname_type.equals("epoch")) {
                filename = destpath + destname_prefix + "" + new Date().getTime();
            }

            String publicurl = publicpath + bucketname + "/" + filename + "." + fileext;

            FsPropertySet ps = new FsPropertySet(); // we will use this to send status reports back
            ps.setProperty("action", "start");
            ps.setProperty("progress", "0");
            ps.setProperty("cfilename", cfilename);
            ps.setProperty("url", publicurl);
            eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps);

            try {
                InputStream inst = request.getInputStream();
                int read = 0;
                int readtotal = 0;
                int b;
                while ((b = inst.read()) != 44) {
                    // skip the base64 tagline, not sure how todo this better
                }

                Base64InputStream b64i = new Base64InputStream(inst);

                //System.out.println("Uploading a new object to S3 from a stream "+bucketname+"/"+filename+"."+fileext);

                ObjectMetadata metadata = new ObjectMetadata();
                metadata.setContentType(filetype + "/" + fileext);

                PutObjectRequest or = new PutObjectRequest(bucketname, filename + "." + fileext, b64i,
                        metadata);

                or.setGeneralProgressListener(new UploadProgressListener(eventscreen.getModel(), publicurl,
                        cfilename, cfilesize, targetid));
                s3Client.putObject(or);

            } catch (AmazonServiceException ase) {
                ase.printStackTrace();
            }
            ps.setProperty("action", "done");
            ps.setProperty("progress", "100");
            ps.setProperty("cfilename", cfilename);
            ps.setProperty("url", publicurl);

            eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps);
            return bucketname + "/" + filename + "." + fileext;

        } else if (method.equals("scp")) {
            String pemfile = eventscreen.getModel()
                    .getProperty("/screen['upload']/target['" + targetid + "']/pemfile");
            if (destpath == null || destpath.equals("")) {
                setUploadError(eventscreen, targetid, "destpath not set");
                return null;
            }

            String storagename = eventscreen.getModel()
                    .getProperty("/screen['upload']/target['" + targetid + "']/storagename");
            if (storagename == null || storagehost.equals("")) {
                setUploadError(eventscreen, targetid, "storagename not set");
                return null;
            }

            String filename = "unknown";
            int storageport = 22;

            if (destname_type.equals("epoch")) {
                filename = destname_prefix + "" + new Date().getTime();
            }

            String publicurl = publicpath + filename + "." + fileext;

            FsPropertySet ps = new FsPropertySet(); // we will use this to send status reports back
            ps.setProperty("action", "start");
            ps.setProperty("progress", "0");
            ps.setProperty("url", publicurl);
            eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps);

            JSch jsch = new JSch();
            jsch.addIdentity(pemfile);
            jsch.setConfig("StrictHostKeyChecking", "no");
            Session session = jsch.getSession(storagename, storagehost, storageport);
            session.connect();
            Channel channel = session.openChannel("sftp");

            channel.connect();
            ChannelSftp channelSftp = (ChannelSftp) channel;
            channelSftp.cd(destpath);

            InputStream inst = request.getInputStream();
            int read = 0;
            int readtotal = 0;
            int b;
            while ((b = inst.read()) != 44) {
                // skip the base64 tagline, not sure how todo this better
            }
            Base64InputStream b64i = new Base64InputStream(inst);

            channelSftp.put(b64i, filename + "." + fileext);

            ps.setProperty("action", "done");
            ps.setProperty("progress", "100");
            ps.setProperty("url", publicurl);
            eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps);
            return filename + "." + fileext;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.springframework.cloud.aws.core.io.s3.AmazonS3ClientFactory.java

License:Apache License

private AmazonS3ClientBuilder buildAmazonS3ForRegion(AmazonS3 prototype, String region) {
    AmazonS3ClientBuilder clientBuilder = AmazonS3ClientBuilder.standard();
    if (prototype instanceof AmazonS3Client) {
        AWSCredentialsProvider awsCredentialsProvider = (AWSCredentialsProvider) ReflectionUtils
                .getField(this.credentialsProviderField, prototype);
        clientBuilder.withCredentials(awsCredentialsProvider);
    }/*from  w  w w  .  ja v  a 2  s  .  co m*/

    return clientBuilder.withRegion(region);
}

From source file:org.symphonyoss.vb.util.AwsS3Client.java

License:Apache License

public AwsS3Client() {

    AWSCredentials credentials = new BasicAWSCredentials(System.getProperty(BotConfig.S3_KEY_ID),
            System.getProperty(BotConfig.S3_ACCESS_KEY));
    s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1)
            .withCredentials(new AWSStaticCredentialsProvider(credentials)).build();

}

From source file:org.wildfly.camel.test.common.aws.S3Utils.java

License:Apache License

public static AmazonS3Client createS3Client() {
    BasicCredentialsProvider credentials = BasicCredentialsProvider.standard();
    AmazonS3Client client = !credentials.isValid() ? null
            : (AmazonS3Client) AmazonS3ClientBuilder.standard().withCredentials(credentials)
                    .withRegion("eu-west-1").build();
    return client;
}

From source file:sys2202.aws.s3.Sample.java

License:Open Source License

public static void main(String[] args) throws Exception {

    // create the client we'll use to connect to S3
    AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1).build();

    // list buckets in our S3 account
    System.out.println("Listing buckets in our S3 account...\n");
    for (Bucket bucket : s3.listBuckets()) {
        System.out.println("\t" + bucket.getName());
    }//from   ww  w. j  av  a 2s .c om

    System.out.println();

    // create a new bucket to experiment with
    String bucketName = "msg8u-sys2202-bucket"; // set the bucket name -- this must be unique, so you'll want to use your ID instead of msg8u
    System.out.println("Creating bucket " + bucketName + "...\n");
    s3.createBucket(bucketName);

    // list buckets in our S3 account
    System.out.println("Listing buckets in our S3 account...\n");
    for (Bucket bucket : s3.listBuckets()) {
        System.out.println("\t" + bucket.getName());
    }

    System.out.println();

    // create and upload a sample file
    System.out.println("Uploading a new object to S3 from a local file...\n");
    File sampleFile = createSampleFile();
    String objectKey = "my-test-file";
    PutObjectRequest putRequest = new PutObjectRequest(bucketName, objectKey, sampleFile);
    s3.putObject(putRequest);

    // list objects in our new bucket -- notice the new object is now present
    System.out.println("Listing objects in our new bucket...\n");
    ListObjectsRequest listRequest = new ListObjectsRequest().withBucketName(bucketName);
    ObjectListing objectListing = s3.listObjects(listRequest);
    for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
        System.out.println("\t" + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
    }

    System.out.println();

    // download and display the sample file that we just uploaded
    System.out.println("Downloading the sample file...\n");
    GetObjectRequest getRequest = new GetObjectRequest(bucketName, objectKey);
    S3Object object = s3.getObject(getRequest);
    displayTextInputStream(object.getObjectContent());

    // delete the sample file from S3
    System.out.println("Deleting the sample file...\n");
    s3.deleteObject(bucketName, objectKey);

    // delete the bucket
    System.out.println("Deleting the bucket...\n");
    s3.deleteBucket(bucketName);

    System.out.println("All done!");
}