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.shareplaylearn.models.UserItemManager.java

License:Open Source License

public HashMap<String, HashMap<ItemSchema.PresentationType, List<String>>> getItemLocations() {
    HashMap<String, HashMap<ItemSchema.PresentationType, List<String>>> itemLocations = new HashMap<>();

    AmazonS3Client s3Client = new AmazonS3Client(
            new BasicAWSCredentials(SecretsService.amazonClientId, SecretsService.amazonClientSecret));

    for (String contentType : ItemSchema.CONTENT_TYPES) {
        for (ItemSchema.PresentationType presentationType : ItemSchema.PRESENTATION_TYPES) {

            ObjectListing listing = s3Client.listObjects(ItemSchema.S3_BUCKET,
                    this.getItemDirectory(contentType, presentationType));

            HashSet<String> locations = getExternalItemListing(listing);
            String curDirectory = makeExternalLocation(getItemDirectory(contentType, presentationType));
            for (String location : locations) {
                //it would be nice if s3 didn't return stuff that doesn't technically match the prefix
                //(due to trailing /), but it looks like it might
                if (curDirectory.endsWith(location)) {
                    log.debug("Skipping location: " + location + " because it looks like a group (folder)"
                            + ", not an object");
                    continue;
                }//from   w  w w . j  av a2s  .  c  o m
                if (!itemLocations.containsKey(contentType)) {
                    itemLocations.put(contentType, new HashMap<>());
                }
                if (!itemLocations.get(contentType).containsKey(presentationType)) {
                    itemLocations.get(contentType).put(presentationType, new ArrayList<>());
                }
                itemLocations.get(contentType).get(presentationType).add(location);
            }
        }
    }
    return itemLocations;
}

From source file:com.shareplaylearn.models.UserItemManager.java

License:Open Source License

private Response checkQuota() {
    AmazonS3Client s3Client = new AmazonS3Client(
            new BasicAWSCredentials(SecretsService.amazonClientId, SecretsService.amazonClientSecret));
    ObjectListing curList = s3Client.listObjects(ItemSchema.S3_BUCKET, this.getUserDir());
    Response listCheck;/*w  ww .  ja va2 s  .c  o  m*/
    if ((listCheck = this.checkObjectListingSize(curList, Limits.MAX_NUM_FILES_PER_USER))
            .getStatus() != Response.Status.OK.getStatusCode()) {
        return listCheck;
    }
    ObjectListing userList = s3Client.listObjects(ItemSchema.S3_BUCKET, "/");
    if ((listCheck = this.checkObjectListingSize(userList, Limits.MAX_TOTAL_FILES))
            .getStatus() != Response.Status.OK.getStatusCode()) {
        return listCheck;
    }
    return Response.status(Response.Status.OK).build();
}

From source file:com.shazam.dataengineering.pipelinebuilder.DeploymentAction.java

License:Apache License

private void deployScriptsToS3() throws DeploymentException {
    String pathPrefix = build.getArtifactsDir().getPath() + "/scripts/";
    AmazonS3 s3Client = new AmazonS3Client(credentials);
    for (S3Environment env : s3Urls.keySet()) {
        if (env.pipelineName.equals(pipelineFile)) {
            String filename = env.scriptName;
            File file = new File(pathPrefix + filename);
            if (file.exists()) {
                String url = s3Urls.get(env);
                clientMessages.add(String.format("[INFO] Uploading %s to %s", filename, url));
                boolean result = AWSProxy.uploadFileToS3Url(s3Client, url, file);
                if (result) {
                    clientMessages.add(String.format("[INFO] Upload successful!"));
                } else {
                    clientMessages.add(String.format("[ERROR] Upload failed!"));
                    throw new DeploymentException();
                }//from   w w  w.  j  a v a2  s. co  m
            } else {
                clientMessages.add(String.format("[ERROR] Unable to find %s in artifacts", filename));
                throw new DeploymentException();
            }
        }
    }
}

From source file:com.shelfmap.simplequery.DefaultContext.java

License:Apache License

protected AmazonS3 createS3(AWSCredentials securityCredential) {
    ClientConfiguration clientConfig = configureS3();
    return clientConfig == null ? new AmazonS3Client(securityCredential)
            : new AmazonS3Client(securityCredential, clientConfig);
}

From source file:com.sjsu.backitup.AwsConsoleApp.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.PropertiesCredentials
 * @see com.amazonaws.ClientConfiguration
 *//*from  w ww .  ja  v a  2 s . c o  m*/
private static void init() throws Exception {
    /*
    * This credentials provider implementation loads your AWS credentials
    * from a properties file at the root of your classpath.
    */
    AWSCredentialsProvider credentialsProvider = new ClasspathPropertiesFileCredentialsProvider();

    s3 = new AmazonS3Client(credentialsProvider);
}

From source file:com.sjsu.faceit.example.S3Sample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*//w w  w .  j  av a2  s.  com
     * Important: Be sure to fill in your AWS access credentials in the
     *            AwsCredentials.properties file before you try to run this
     *            sample.
     * http://aws.amazon.com/security-credentials
     */
    System.out.println(new File(".").getAbsolutePath());
    AmazonS3 s3 = new AmazonS3Client(
            new PropertiesCredentials(S3Sample.class.getResourceAsStream("AwsCredentials.properties")));

    String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
    String key = "MyObjectKey";

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon S3");
    System.out.println("===========================================\n");

    try {
        /*
         * Create a new S3 bucket - Amazon S3 bucket names are globally unique,
         * so once a bucket name has been taken by any user, you can't create
         * another bucket with that same name.
         *
         * You can optionally specify a location for your bucket if you want to
         * keep your data closer to your applications or users.
         */
        System.out.println("Creating bucket " + bucketName + "\n");
        s3.createBucket(bucketName);

        /*
         * List the buckets in your account
         */
        System.out.println("Listing buckets");
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();

        /*
         * Upload an object to your bucket - You can easily upload a file to
         * S3, or upload directly an InputStream if you know the length of
         * the data in the stream. You can also specify your own metadata
         * when uploading to S3, which allows you set a variety of options
         * like content-type and content-encoding, plus additional metadata
         * specific to your applications.
         */
        System.out.println("Uploading a new object to S3 from a file\n");
        s3.putObject(new PutObjectRequest(bucketName, "abc/" + key, new File("/Users/prayag/Desktop/2.jpg")));

        /*
         * Download an object - When you download an object, you get all of
         * the object's metadata and a stream from which to read the contents.
         * It's important to read the contents of the stream as quickly as
         * possibly since the data is streamed directly from Amazon S3 and your
         * network connection will remain open until you read all the data or
         * close the input stream.
         *
         * GetObjectRequest also supports several other options, including
         * conditional downloading of objects based on modification times,
         * ETags, and selectively downloading a range of an object.
         */
        System.out.println("Downloading an object");
        S3Object object = s3.getObject(new GetObjectRequest(bucketName, "abc/" + key));
        System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
        displayTextInputStream(object.getObjectContent());

        /*
         * List objects in your bucket by prefix - There are many options for
         * listing the objects in your bucket.  Keep in mind that buckets with
         * many objects might truncate their results when listing their objects,
         * so be sure to check if the returned object listing is truncated, and
         * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve
         * additional results.
         */
        System.out.println("Listing objects");
        ObjectListing objectListing = s3
                .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My"));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(
                    " - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        }
        System.out.println();

        /*
         * Delete an object - Unless versioning has been turned on for your bucket,
         * there is no way to undelete an object, so use caution when deleting objects.
         */
        //            System.out.println("Deleting an object\n");
        //            s3.deleteObject(bucketName, key);

        /*
         * Delete a bucket - A bucket must be completely empty before it can be
         * deleted, so remember to delete any objects from your buckets before
         * you try to delete them.
         */
        //            System.out.println("Deleting bucket " + bucketName + "\n");
        //            s3.deleteBucket(bucketName);
    } 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 "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:com.sludev.commons.vfs2.provider.s3.SS3FileProvider.java

License:Apache License

/**
 * Create FileSystem event hook/* ww  w  .jav  a  2  s  .c o  m*/
 * 
 * @param rootName
 * @param fileSystemOptions
 * @return
 * @throws FileSystemException 
 */
@Override
protected FileSystem doCreateFileSystem(FileName rootName, FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    SS3FileSystem fileSystem = null;
    GenericFileName genRootName = (GenericFileName) rootName;

    AWSCredentials storageCreds;
    AmazonS3Client client;

    FileSystemOptions currFSO;
    UserAuthenticator ua;

    if (fileSystemOptions == null) {
        currFSO = getDefaultFileSystemOptions();
        ua = SS3FileSystemConfigBuilder.getInstance().getUserAuthenticator(currFSO);
    } else {
        currFSO = fileSystemOptions;
        ua = DefaultFileSystemConfigBuilder.getInstance().getUserAuthenticator(currFSO);
    }

    UserAuthenticationData authData = null;
    try {
        authData = ua.requestAuthentication(AUTHENTICATOR_TYPES);

        String currAcct = UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(genRootName.getUserName())));

        String currKey = UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(genRootName.getPassword())));

        storageCreds = new BasicAWSCredentials(currAcct, currKey);

        client = new AmazonS3Client(storageCreds);

        if (StringUtils.isNoneBlank(endpoint)) {
            client.setEndpoint(endpoint);
        }

        if (region != null) {
            client.setRegion(region);
        }

        fileSystem = new SS3FileSystem(genRootName, client, fileSystemOptions);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return fileSystem;
}

From source file:com.snapdeal.scm.core.AmazonS3Configuration.java

License:Open Source License

@Bean
public AmazonS3 getS3Client() {
    if (s3Client == null) {
        AWSCredentials s3Credentials = new BasicAWSCredentials(accessId, secretKey);
        s3Client = new AmazonS3Client(s3Credentials);
        s3Client.setRegion(Region.getRegion(Regions.fromName(region)));
    }/*from ww w.j  a  va2s.c  o  m*/
    return s3Client;
}

From source file:com.spotify.docker.BuildMojo.java

License:Apache License

private AciRepository getAciRepository() throws MojoExecutionException {
      if (Strings.isNullOrEmpty(this.repository)) {
          throw new MojoExecutionException("Must set repository to push to");
      }/*from  www .  jav  a  2s.  com*/
      URI uri = URI.create(this.repository);
      String scheme = uri.getScheme();
      if (scheme.equals("s3")) {
          AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();
          AmazonS3Client amazonS3Client = new AmazonS3Client(provider);
          String bucketName = uri.getHost();
          String prefix = uri.getPath();
          return new S3AciRepository(amazonS3Client, bucketName, prefix);
      } else {
          throw new MojoExecutionException("Unknown repository scheme: " + scheme);
      }
  }

From source file:com.stehno.sanctuary.core.remote.S3RemoteStore.java

License:Apache License

public S3RemoteStore(String s3AccessKey, String s3SecretKey) {
    this.amazonS3 = new AmazonS3Client(new BasicAWSCredentials(s3AccessKey, s3SecretKey));
}