Example usage for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client

List of usage examples for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client

Introduction

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

Prototype

@SdkInternalApi
AmazonS3Client(AmazonS3ClientParams s3ClientParams) 

Source Link

Document

Constructs a new client to invoke service methods on S3 using the specified parameters.

Usage

From source file:com.kodemore.aws.s3.KmS3Uploader.java

License:Open Source License

private AmazonS3Client createClient() {
    AWSCredentials creds = new BasicAWSCredentials(_accessKeyId, _secretKey);
    return new AmazonS3Client(creds);
}

From source file:com.kpbird.aws.Main.java

public void createS3() {

    try {/*w  ww  .  j  a v  a2 s  .com*/
        AmazonS3Client s3 = new AmazonS3Client(credentials);
        s3.setEndpoint(endPoint);
        s3.setRegion(region);
        log.Info("Creating Bucket :" + BucketName);

        s3.createBucket(BucketName);
        log.Info("Policy :" + BucketPolicy);
        s3.setBucketPolicy(BucketName, BucketPolicy);

    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}

From source file:com.lithium.flow.filer.S3Filer.java

License:Apache License

public S3Filer(@Nonnull Config config, @Nonnull Access access) {
    checkNotNull(config);//from w  ww  .  java2s  .c o  m
    checkNotNull(access);
    uri = URI.create(config.getString("url"));
    bucket = uri.getHost();
    partSize = config.getInt("s3.partSize", 5 * 1024 * 1024);
    tempDir = new File(config.getString("s3.tempDir", System.getProperty("java.io.tmpdir")));
    service = Executors.newFixedThreadPool(config.getInt("s3.threads", 1));

    String key = config.getString("key");
    String secret = access.getPrompt().prompt(bucket, bucket + " secret: ", true, false);

    s3 = new AmazonS3Client(new BasicAWSCredentials(key, secret));
}

From source file:com.logpig.mweagle.aws.S3FilePutRunnable.java

License:Apache License

@Override
public void run() {
    boolean createBucket = false;
    boolean doExit = false;
    int attempt = 0;
    final AmazonS3Client s3Client = new AmazonS3Client(this.s3Settings.getAWSCredentials());
    while (!doExit && attempt != this.s3Settings.retryCount) {
        try {//from w  ww  .  jav a2 s.  co  m
            if (!s3Settings.mockPut) {
                if (createBucket) {
                    s3Client.createBucket(this.s3Settings.bucketName, this.s3Settings.regionName);
                }
                final File logfile = new File(this.filePath);
                final String keyName = UUID.randomUUID().toString();
                final PutObjectRequest request = new PutObjectRequest(this.s3Settings.bucketName, keyName,
                        logfile);
                s3Client.putObject(request);
            } else {
                logger.warn("Mocking file POST: {}", this.filePath);
            }
            doExit = true;
        } catch (AmazonServiceException ex) {
            createBucket = false;
            if (HttpURLConnection.HTTP_NOT_FOUND == ex.getStatusCode()
                    && ex.getErrorCode().equals("NoSuchBucket")) {
                createBucket = true;
            } else {
                // If the credentials are invalid, don't keep trying...
                doExit = HttpURLConnection.HTTP_FORBIDDEN == ex.getStatusCode();
                if (doExit) {
                    logger.error(String.format("Authentication error posting %s to AWS.  Will not retry.",
                            this.filePath), ex);
                } else {
                    logger.error(String.format("Failed to post %s to AWS", this.filePath), ex);
                }
            }
        } catch (AmazonClientException ex) {
            createBucket = false;
            logger.error(String.format("Failed to post %s to AWS", this.filePath), ex);
        } finally {
            // Create bucket failures don't count
            if (!createBucket) {
                attempt += 1;
            }
        }
    }
}

From source file:com.mateusz.mateuszsqs.SQSprocessor.java

public static void main(String[] args) throws AmazonClientException {
    System.out.println("Start process SQS");
    try {/*from w  ww.  j  a  v  a  2 s .  c o  m*/
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        System.out.println("Error");
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    final Thread mainThread = new Thread(() -> {
        while (!Thread.currentThread().isInterrupted()) {
            AmazonSQS sqs = new AmazonSQSClient(credentials);
            Region usWest2 = Region.getRegion(Regions.US_WEST_2);
            sqs.setRegion(usWest2);

            AmazonS3 s3 = new AmazonS3Client(credentials);
            s3.setRegion(usWest2);

            List<String> filesList = SQSConfig.getMessages(sqs, sqsURL);

            for (String file : filesList) {
                String[] files = file.split(",");
                if (!file.equals("missing parameter: fileNames"))
                    for (int i = 0; i < files.length; i++) {
                        try {
                            SQSConfig.processFile(files[i], s3, bucketName);
                        } catch (IOException e) {
                            System.out.println("Error");
                            e.printStackTrace();
                        }
                    }

            }
            System.out.println("\nWaiting for messages.........\n");
        }
    });
    mainThread.start();
}

From source file:com.maya.portAuthority.util.ImageUploader.java

public static void uploadImage(String imageURL, String imageName, String bucketName)
         throws MalformedURLException, IOException {
     // credentials object identifying user for authentication

     AWSCredentials credentials = new BasicAWSCredentials("AKIAJBFSMHRTIQQ7BKYA",
             "AdHgeP4dyWInWwPn9YlfxFCm3qP1lHjdxOxeJqDa");

     // create a client connection based on credentials
     AmazonS3 s3client = new AmazonS3Client(credentials);

     String folderName = "image"; //folder name
     //    String bucketName = "ppas-image-upload"; //must be unique

     try {/*from  w  ww .  j  a v a 2  s  .  co  m*/
         if (!(s3client.doesBucketExist(bucketName))) {
             s3client.setRegion(Region.getRegion(Regions.US_EAST_1));
             // Note that CreateBucketRequest does not specify region. So bucket is 
             // created in the region specified in the client.
             s3client.createBucket(new CreateBucketRequest(bucketName));
         }

         //Enabe CORS:
         //     <?xml version="1.0" encoding="UTF-8"?>
         //<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
         //    <CORSRule>
         //        <AllowedOrigin>http://ask-ifr-download.s3.amazonaws.com</AllowedOrigin>
         //        <AllowedMethod>GET</AllowedMethod>
         //    </CORSRule>
         //</CORSConfiguration>
         BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();

         CORSRule corsRule = new CORSRule()
                 .withAllowedMethods(
                         Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.GET }))
                 .withAllowedOrigins(Arrays.asList(new String[] { "http://ask-ifr-download.s3.amazonaws.com" }));
         configuration.setRules(Arrays.asList(new CORSRule[] { corsRule }));
         s3client.setBucketCrossOriginConfiguration(bucketName, configuration);

     } catch (AmazonServiceException ase) {
         System.out.println("Caught an AmazonServiceException, which " + "means your request made it "
                 + "to Amazon S3, but was rejected with an error response" + " for some reason.");
         System.out.println("Error Message:    " + ase.getMessage());
         System.out.println("HTTP Status Code: " + ase.getStatusCode());
         System.out.println("AWS Error Code:   " + ase.getErrorCode());
         System.out.println("Error Type:       " + ase.getErrorType());
         System.out.println("Request ID:       " + ase.getRequestId());
     } catch (AmazonClientException ace) {
         System.out.println("Caught an AmazonClientException, which " + "means the client encountered "
                 + "an internal error while trying to " + "communicate with S3, "
                 + "such as not being able to access the network.");
         System.out.println("Error Message: " + ace.getMessage());
     }

     String fileName = folderName + SUFFIX + imageName + ".png";
     URL url = new URL(imageURL);

     ObjectMetadata omd = new ObjectMetadata();
     omd.setContentType("image/png");
     omd.setContentLength(url.openConnection().getContentLength());
     // upload file to folder and set it to public
     s3client.putObject(new PutObjectRequest(bucketName, fileName, url.openStream(), omd)
             .withCannedAcl(CannedAccessControlList.PublicRead));
 }

From source file:com.maya.portAuthority.util.ImageUploader.java

public static void uploadImage(String imageURL, String imageName) throws MalformedURLException, IOException {
     // credentials object identifying user for authentication

     AWSCredentials credentials = new BasicAWSCredentials("<Your access key id>", "<Your secret access key>");

     // create a client connection based on credentials
     AmazonS3 s3client = new AmazonS3Client(credentials);

     String folderName = "image";
     String bucketName = "ppas-image-upload";
     String fileName = folderName + SUFFIX + imageName + ".png";
     URL url = new URL(imageURL);

     ObjectMetadata omd = new ObjectMetadata();
     omd.setContentType("image/png");
     omd.setContentLength(url.openConnection().getContentLength());
     // upload file to folder and set it to public
     s3client.putObject(new PutObjectRequest(bucketName, fileName, url.openStream(), omd)
             .withCannedAcl(CannedAccessControlList.PublicRead));
 }

From source file:com.mesosphere.dcos.cassandra.executor.backup.S3StorageDriver.java

License:Apache License

private AmazonS3Client getAmazonS3Client(BackupRestoreContext ctx) throws URISyntaxException {
    final String accessKey = ctx.getAccountId();
    final String secretKey = ctx.getSecretKey();
    String endpoint = getEndpoint(ctx);
    LOGGER.info("endpoint: {}", endpoint);

    final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
    final AmazonS3Client amazonS3Client = new AmazonS3Client(basicAWSCredentials);
    amazonS3Client.setEndpoint(endpoint);

    if (ctx.usesEmc()) {
        final S3ClientOptions options = new S3ClientOptions();
        options.setPathStyleAccess(true);
        amazonS3Client.setS3ClientOptions(options);
    }/*from w ww  .j a v  a  2s  . co m*/

    return amazonS3Client;
}

From source file:com.meteotester.util.S3Util.java

License:Open Source License

public static void saveFileToS3(File file) {
    try {//w w  w. ja  v  a  2  s .c  om
        AWSCredentials myCredentials = new BasicAWSCredentials(Config.AWS_ACCESS_KEY, Config.AWS_SECRET_KEY);
        AmazonS3 s3client = new AmazonS3Client(myCredentials);

        s3client.setRegion(Region.getRegion(Regions.EU_WEST_1));
        String filename = file.getName();
        String path = file.getPath();

        PutObjectRequest req = new PutObjectRequest(Config.S3_BUCKETNAME, path, file);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(file.length());
        String contentType = (filename.contains("json")) ? "application/json" : "text/csv";
        metadata.setContentType(contentType);
        req.setMetadata(metadata);

        s3client.putObject(req);

        log.info(filename + " stored in S3");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.miovision.oss.awsbillingtools.examples.DefaultElasticsearchIndexerExampleApplication.java

License:Open Source License

private static S3BillingRecordLoader<DetailedLineItem> createRecordLoader(String bucketName,
        String awsAccountId) {//from   w  ww  .j  a v  a 2 s.co m
    AmazonS3 amazonS3 = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());
    S3BillingRecordFileScanner fileScanner = new S3BillingRecordFileScanner(amazonS3, bucketName, "",
            awsAccountId);
    DetailedLineItemParser parser = new DetailedLineItemParser();
    return new S3BillingRecordLoader<>(amazonS3, fileScanner, parser);
}