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

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

Introduction

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

Prototype

@Override
    public PutObjectResult putObject(PutObjectRequest putObjectRequest)
            throws SdkClientException, AmazonServiceException 

Source Link

Usage

From source file:org.apache.usergrid.apm.util.GenerateSimulatedMobileData.java

License:Apache License

public static boolean uploadSimulatedCrashLogsToS3(App app, String fileName) {
    DeploymentConfig config = DeploymentConfig.geDeploymentConfig();
    AWSCredentials credentials = new BasicAWSCredentials(config.getAccessKey(), config.getSecretKey());
    AmazonS3Client s3Client = new AmazonS3Client(credentials);
    PutObjectRequest putObjectRequest;//w w  w  . j a  v a2  s  .c  om

    String s3FullFileName = config.getEnvironment() + "/" + config.getS3CrashLogFolder() + "/"
            + app.getFullAppName() + "/" + fileName;
    String sampleCrashFileName = null;

    if (fileName.endsWith(".crash")) //it's an iOS crash file
        sampleCrashFileName = "ios-crash-log-example.txt";
    else if (fileName.endsWith(".stacktrace"))
        sampleCrashFileName = "android-crash-log-example.txt";
    try {

        ObjectMetadata metaData = new ObjectMetadata();

        metaData.setHeader(Headers.S3_CANNED_ACL, CannedAccessControlList.AuthenticatedRead);

        InputStream is = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(sampleCrashFileName);

        putObjectRequest = new PutObjectRequest(config.getS3LogBucket(), s3FullFileName, is, null);
        //new ByteArrayInputStream(    //fileName.getBytes("UTF-8")),null);
        PutObjectResult result = s3Client.putObject(putObjectRequest);
        return true;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.broadleafcommerce.vendor.amazon.s3.S3FileServiceProvider.java

License:Apache License

protected List<String> addOrUpdateResourcesInternal(S3Configuration s3config, AmazonS3Client s3,
        FileWorkArea workArea, List<File> files, boolean removeFilesFromWorkArea) {
    final List<String> resourcePaths = new ArrayList<String>();
    for (final File srcFile : files) {
        if (!srcFile.getAbsolutePath().startsWith(workArea.getFilePathLocation())) {
            throw new FileServiceException("Attempt to update file " + srcFile.getAbsolutePath()
                    + " that is not in the passed in WorkArea " + workArea.getFilePathLocation());
        }//w w w .j av  a2 s  .  c o m
        final long ts1 = System.currentTimeMillis();
        final String fileName = srcFile.getAbsolutePath().substring(workArea.getFilePathLocation().length());
        final String resourceName = buildResourceName(s3config, fileName);

        ObjectMetadata meta = null;
        try {
            final GetObjectMetadataRequest get = new GetObjectMetadataRequest(s3config.getDefaultBucketName(),
                    resourceName);
            meta = s3.getObjectMetadata(get);
        } catch (AmazonS3Exception ex) {
            meta = null;
        }
        final long ts2 = System.currentTimeMillis();

        if (meta == null || meta.getContentLength() != srcFile.length()) {
            final PutObjectRequest put = new PutObjectRequest(s3config.getDefaultBucketName(), resourceName,
                    srcFile);

            if ((s3config.getStaticAssetFileExtensionPattern() != null) && s3config
                    .getStaticAssetFileExtensionPattern().matcher(getExtension(fileName)).matches()) {
                put.setCannedAcl(CannedAccessControlList.PublicRead);
            }

            s3.putObject(put);
            final long ts3 = System.currentTimeMillis();

            if (LOG.isTraceEnabled()) {
                final String s3Uri = String.format("s3://%s/%s", s3config.getDefaultBucketName(), resourceName);
                final String msg = String.format(
                        "%s copied/updated to %s; queryTime = %dms; uploadTime = %dms; totalTime = %dms",
                        srcFile.getAbsolutePath(), s3Uri, ts2 - ts1, ts3 - ts2, ts3 - ts1);

                LOG.trace(msg);
            }
        } else {
            if (LOG.isTraceEnabled()) {
                final String s3Uri = String.format("s3://%s/%s", s3config.getDefaultBucketName(), resourceName);
                final String msg = String.format(
                        "%s already at %s with same filesize = %dbytes; queryTime = %dms",
                        srcFile.getAbsolutePath(), s3Uri, srcFile.length(), ts2 - ts1);

                LOG.trace(msg);
            }
        }

        resourcePaths.add(fileName);
    }
    return resourcePaths;
}

From source file:org.broadleafcommerce.vendor.amazon.s3.S3FileServiceProvider.java

License:Apache License

protected void addOrUpdateResourcesInternalStreamVersion(S3Configuration s3config, AmazonS3Client s3,
        InputStream inputStream, String fileName, long fileSizeInBytes) {
    final String bucketName = s3config.getDefaultBucketName();

    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(fileSizeInBytes);
    final String resourceName = buildResourceName(s3config, fileName);
    final PutObjectRequest objToUpload = new PutObjectRequest(bucketName, resourceName, inputStream, metadata);

    if ((s3config.getStaticAssetFileExtensionPattern() != null)
            && s3config.getStaticAssetFileExtensionPattern().matcher(getExtension(fileName)).matches()) {
        objToUpload.setCannedAcl(CannedAccessControlList.PublicRead);
    }//from w  ww  .j  a  va2  s. com

    s3.putObject(objToUpload);

    if (LOG.isTraceEnabled()) {
        final String s3Uri = String.format("s3://%s/%s", s3config.getDefaultBucketName(), resourceName);
        final String msg = String.format("%s copied/updated to %s", fileName, s3Uri);

        LOG.trace(msg);
    }
}

From source file:org.entando.entando.plugins.jps3awsclient.aps.system.services.storage.AmazonS3StorageManager.java

License:Open Source License

public void store(IStorageObject obj, boolean reducedRedundancy, CannedAccessControlList acl)
        throws ApsSystemException {
    try {//w ww  . j  a  v  a2 s  .co  m
        AmazonS3Client client = this.getS3Client();
        String bucketName = obj.getBucketName().toLowerCase();
        this.checkForAndCreateBucket(bucketName, client);
        ObjectMetadata omd = new ObjectMetadata();
        omd.setContentType(obj.getContentType());
        omd.setContentLength(obj.getContentLength());
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, obj.getStoragePath(),
                obj.getInputStream(), omd);
        // Check if reduced redundancy is enabled
        if (reducedRedundancy) {
            putObjectRequest.setStorageClass(StorageClass.ReducedRedundancy);
        }
        if (null != obj.getUserMetadata()) {
            ObjectMetadata objectMetadata = new ObjectMetadata();
            putObjectRequest.setMetadata(objectMetadata);
            Iterator<String> iter = obj.getUserMetadata().keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                objectMetadata.addUserMetadata(key, obj.getUserMetadata().get(key));
            }
        }
        client.putObject(putObjectRequest);
        // If we have an ACL set access permissions for the the data on S3
        if (acl != null) {
            client.setObjectAcl(bucketName, obj.getStoragePath(), acl);
        }
    } catch (Throwable t) {
        _logger.error("Error storing object", t);
        throw new ApsSystemException("Error storing object", t);
    }
}

From source file:org.finra.dm.dao.impl.S3OperationsImpl.java

License:Apache License

/**
 * {@inheritDoc}//  w w  w  .j  a  v a  2  s  . c  om
 */
@Override
public PutObjectResult putObject(PutObjectRequest putObjectRequest, AmazonS3Client s3Client) {
    return s3Client.putObject(putObjectRequest);
}

From source file:org.kuali.maven.wagon.FileHandler.java

License:Educational Community License

public void handleElement(ListIteratorContext<PutFileContext> context, int index, PutFileContext element) {
    RequestFactory factory = element.getFactory();
    AmazonS3Client client = element.getClient();
    PutObjectRequest request = factory.getPutObjectRequest(element);
    client.putObject(request);
}

From source file:temp.zAmazonServiceProvider.java

License:BSD License

@Override
public boolean save(final String path, final File file) {

    if (!isAvailable()) {
        return false;
    }/*from   w  w w . jav  a 2 s .com*/

    try {

        final AmazonS3Client client = amazonConfig.client();
        final String bucket = amazonConfig.bucket();

        final PutObjectRequest request = //
                new PutObjectRequest(bucket, PathHelp.rootLessPath(path), file);

        final PutObjectResult result = client.putObject(request);

        return true;

    } catch (final Exception e) {

        setAvailable(false);

        log.error("bada-boom", e);

        return false;

    }

}