Example usage for com.amazonaws.services.s3.transfer TransferManager TransferManager

List of usage examples for com.amazonaws.services.s3.transfer TransferManager TransferManager

Introduction

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

Prototype

protected TransferManager(TransferManagerBuilder builder) 

Source Link

Document

Constructor for use by classes that need to extend the TransferManager.

Usage

From source file:com.shelfmap.simplequery.domain.impl.DefaultBlobReference.java

License:Apache License

@Override
public Upload uploadFrom(InputStream uploadSource, ObjectMetadata metadata) throws BlobOutputException {
    String bucket = resourceInfo.getBucketName();
    String key = resourceInfo.getKey();

    try {// w w w  . j av a2 s  . co  m
        PutObjectRequest request = new PutObjectRequest(bucket, key, uploadSource, metadata);
        AmazonS3 s3 = getContext().getS3();
        TransferManager transfer = new TransferManager(s3);
        this.lastUpload = transfer.upload(request);
        return this.lastUpload;
    } catch (AmazonServiceException ex) {
        throw new BlobOutputException("a problem occured in Amazon S3.", ex);
    } catch (AmazonClientException ex) {
        throw new BlobOutputException("Client had an problem when uploading data.", ex);
    }
}

From source file:com.wowza.wms.plugin.s3upload.ModuleS3Upload.java

License:Open Source License

public void onAppStart(IApplicationInstance appInstance) {
    logger = WMSLoggerFactory.getLoggerObj(appInstance);
    this.appInstance = appInstance;

    try {//from ww w  .  ja v a2  s . c om
        WMSProperties props = appInstance.getProperties();
        accessKey = props.getPropertyStr("s3UploadAccessKey", accessKey);
        secretKey = props.getPropertyStr("s3UploadSecretKey", secretKey);
        bucketName = props.getPropertyStr("s3UploadBucketName", bucketName);
        endpoint = props.getPropertyStr("s3UploadEndpoint", endpoint);
        resumeUploads = props.getPropertyBoolean("s3UploadResumeUploads", resumeUploads);
        deleteOriginalFiles = props.getPropertyBoolean("s3UploadDeletOriginalFiles", deleteOriginalFiles);
        // fix typo in property name
        deleteOriginalFiles = props.getPropertyBoolean("s3UploadDeleteOriginalFiles", deleteOriginalFiles);

        // This value should be the URI representation of the "Group Grantee" found here http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html under "Amazon S3 Predefined Groups"
        aclGroupGranteeUri = props.getPropertyStr("s3UploadACLGroupGranteeUri", aclGroupGranteeUri);
        // This should be a string that represents the level of permissions we want to grant to the "Group Grantee" access to the file to be uploaded
        aclPermissionRule = props.getPropertyStr("s3UploadACLPermissionRule", aclPermissionRule);

        // With the passed property, check if it maps to a specified GroupGrantee
        GroupGrantee grantee = GroupGrantee.parseGroupGrantee(aclGroupGranteeUri);
        // In order for the parsing to work correctly, we will go ahead and force uppercase on the string passed
        Permission permission = Permission.parsePermission(aclPermissionRule.toUpperCase());

        // If we have properties for specifying permisions on the file upload, create the AccessControlList object and set the Grantee and Permissions
        if (grantee != null && permission != null) {
            acl = new AccessControlList();
            acl.grantPermission(grantee, permission);
        }

        if (StringUtils.isEmpty(accessKey) || StringUtils.isEmpty(secretKey)) {
            logger.warn(
                    MODULE_NAME + ".onAppStart: [" + appInstance.getContextStr() + "] missing S3 credentials",
                    WMSLoggerIDs.CAT_application, WMSLoggerIDs.EVT_comment);
            return;
        }

        AmazonS3 s3Client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));

        if (!StringUtils.isEmpty(endpoint))
            s3Client.setEndpoint(endpoint);

        if (!StringUtils.isEmpty(bucketName)) {
            boolean hasBucket = false;
            List<Bucket> buckets = s3Client.listBuckets();
            for (Bucket bucket : buckets) {
                if (bucket.getName().equals(bucketName)) {
                    hasBucket = true;
                    break;
                }
            }
            if (!hasBucket) {
                logger.warn(MODULE_NAME + ".onAppStart: [" + appInstance.getContextStr()
                        + "] missing S3 bucket: " + bucketName, WMSLoggerIDs.CAT_application,
                        WMSLoggerIDs.EVT_comment);
                return;
            }
        }

        logger.info(MODULE_NAME + ".onAppStart [" + appInstance.getContextStr() + "] S3 Bucket Name: "
                + bucketName + ", Resume Uploads: " + resumeUploads + ", Delete Original Files: "
                + deleteOriginalFiles, WMSLoggerIDs.CAT_application, WMSLoggerIDs.EVT_comment);
        transferManager = new TransferManager(s3Client);
        resumeUploads();

        appInstance.addMediaWriterListener(new WriteListener());
    } catch (AmazonS3Exception ase) {
        logger.error(MODULE_NAME + ".onAppStart [" + appInstance.getContextStr() + "] AmazonS3Exception: "
                + ase.getMessage());
    } catch (Exception e) {
        logger.error(
                MODULE_NAME + ".onAppStart [" + appInstance.getContextStr() + "] exception: " + e.getMessage(),
                e);
    } catch (Throwable t) {
        logger.error(MODULE_NAME + ".onAppStart [" + appInstance.getContextStr() + "] throwable exception: "
                + t.getMessage(), t);
    }
}

From source file:com.zhang.aws.s3.S3TransferProgressSample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    /*/*from  w  w w  .  j  a  v a2  s .  co  m*/
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     *
     * TransferManager manages a pool of threads, so we create a
     * single instance and share it throughout our application.
     */
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        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);
    }

    AmazonS3 s3 = new AmazonS3Client(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    s3.setRegion(usWest2);
    tx = new TransferManager(s3);

    bucketName = "s3-upload-sdk-sample-" + credentials.getAWSAccessKeyId().toLowerCase();

    new S3TransferProgressSample();
}

From source file:doug.iotdemo.mojo.deployer.Deployer.java

License:Open Source License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    AmazonS3 s3 = new AmazonS3Client();

    if (!s3.doesBucketExist(bucketName)) {
        throw new MojoExecutionException("bucket " + bucketName + " does not exist");
    }/*www  .  ja  va2s .c  o  m*/

    getLog().info("Uploading " + source.getName() + " to s3://" + bucketName + "/" + bucketKey);

    TransferManager manager = new TransferManager(s3);
    Transfer transfer;
    if (source.isFile()) {
        transfer = manager.upload(bucketName, bucketKey, source);
    } else if (source.isDirectory()) {
        transfer = manager.uploadDirectory(bucketName, bucketKey, source, true);
    } else {
        throw new MojoExecutionException("Unknown file type " + source.getAbsolutePath());
    }

    try {
        transfer.waitForCompletion();
    } catch (InterruptedException e) {
        throw new MojoExecutionException("Upload to S3 failed", e);
    }
}

From source file:ecplugins.s3.S3Util.java

License:Apache License

/**
 * This procedure deletes the bucket along with its contents
 * @param bucketName/*from   w  w w. ja v  a2s .co  m*/
 * @return
 * @throws Exception
 */
public static boolean DeleteBucket(String bucketName) throws Exception {

    Properties props = TestUtils.getProperties();

    BasicAWSCredentials credentials = new BasicAWSCredentials(props.getProperty(StringConstants.ACCESS_ID),
            props.getProperty(StringConstants.SECRET_ACCESS_ID));

    // Create TransferManager
    TransferManager tx = new TransferManager(credentials);

    // Get S3 Client
    AmazonS3 s3 = tx.getAmazonS3Client();

    if (s3.doesBucketExist(bucketName)) {
        // Multi-object delete by specifying only keys (no version ID).
        DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName).withQuiet(false);

        //get keys
        List<String> keys = new ArrayList<String>();
        ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            keys.add(objectSummary.getKey());
        }

        // Create request that include only object key names.
        List<DeleteObjectsRequest.KeyVersion> justKeys = new ArrayList<DeleteObjectsRequest.KeyVersion>();
        for (String key : keys) {
            justKeys.add(new DeleteObjectsRequest.KeyVersion(key));
        }

        if (justKeys.size() == 0) {
            return false;
        }

        multiObjectDeleteRequest.setKeys(justKeys);
        // Execute DeleteObjects - Amazon S3 add delete marker for each object
        // deletion. The objects no disappear from your bucket (verify).
        DeleteObjectsResult delObjRes = null;

        delObjRes = s3.deleteObjects(multiObjectDeleteRequest);

        s3.deleteBucket(bucketName);
        return true;
    } else {
        System.out.println("Error: Bucket with name " + bucketName + " does not exists.");
        return false;
    }
}

From source file:ecplugins.s3.S3Util.java

License:Apache License

public static void CreateBucket(String bucketName) throws Exception {

    Properties props = TestUtils.getProperties();

    BasicAWSCredentials credentials = new BasicAWSCredentials(props.getProperty(StringConstants.ACCESS_ID),
            props.getProperty(StringConstants.SECRET_ACCESS_ID));

    // Create TransferManager
    TransferManager tx = new TransferManager(credentials);

    // Get S3 Client
    AmazonS3 s3 = tx.getAmazonS3Client();

    s3.createBucket(bucketName);//from  w ww .  j a  v  a 2  s  .  co m

}

From source file:ecplugins.s3.S3Util.java

License:Apache License

public static boolean CheckIsBucketAvailable(String bucketName) throws Exception {

    Properties props = TestUtils.getProperties();

    BasicAWSCredentials credentials = new BasicAWSCredentials(props.getProperty(StringConstants.ACCESS_ID),
            props.getProperty(StringConstants.SECRET_ACCESS_ID));

    // Create TransferManager
    TransferManager tx = new TransferManager(credentials);

    // Get S3 Client
    AmazonS3 s3 = tx.getAmazonS3Client();

    return s3.doesBucketExist(bucketName);
}

From source file:ecplugins.s3.S3Util.java

License:Apache License

public static int ListBuckets() throws Exception {
    Properties props = TestUtils.getProperties();

    BasicAWSCredentials credentials = new BasicAWSCredentials(props.getProperty(StringConstants.ACCESS_ID),
            props.getProperty(StringConstants.SECRET_ACCESS_ID));

    // Create TransferManager
    TransferManager tx = new TransferManager(credentials);

    // Get S3 Client
    AmazonS3 s3 = tx.getAmazonS3Client();

    List<Bucket> buckets = s3.listBuckets();

    return buckets.size();
}

From source file:ecplugins.s3.S3Util.java

License:Apache License

public static void UploadObject(String bucketName, String key)
        throws AmazonClientException, AmazonServiceException, Exception {
    Properties props = TestUtils.getProperties();
    File file = new File(createFile());
    BasicAWSCredentials credentials = new BasicAWSCredentials(props.getProperty(StringConstants.ACCESS_ID),
            props.getProperty(StringConstants.SECRET_ACCESS_ID));

    // Create TransferManager
    TransferManager tx = new TransferManager(credentials);

    // Get S3 Client
    AmazonS3 s3 = tx.getAmazonS3Client();

    try {/*from www.j a v a 2s .  c o  m*/
        System.out.println("Uploading a new object to S3 from a file\n");

        s3.putObject(new PutObjectRequest(bucketName, key, file));

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException");
        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 such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }

}

From source file:ecplugins.s3.S3Util.java

License:Apache License

public static boolean UploadFolder(String bucketName, String key)
        throws AmazonClientException, AmazonServiceException, Exception {
    Properties props = TestUtils.getProperties();
    File file = new File(createFolder());
    BasicAWSCredentials credentials = new BasicAWSCredentials(props.getProperty(StringConstants.ACCESS_ID),
            props.getProperty(StringConstants.SECRET_ACCESS_ID));

    // Create TransferManager
    TransferManager tx = new TransferManager(credentials);

    // Get S3 Client
    AmazonS3 s3 = tx.getAmazonS3Client();
    MultipleFileUpload objectUpload = tx.uploadDirectory(bucketName, key, file, true);

    while (!objectUpload.isDone()) {
        Thread.sleep(1000);//w w  w  . j  a  va2  s.  c  om
    }

    return true;
}