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

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

Introduction

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

Prototype

public PutObjectResult putObject(String bucketName, String key, String content)
        throws AmazonServiceException, SdkClientException;

Source Link

Document

Encodes a String into the contents of an S3 object.

Usage

From source file:org.deeplearning4j.aws.s3.uploader.S3Uploader.java

License:Apache License

/**
 * Upload the file to the bucket./* w  w  w.j a v  a 2 s.c  o  m*/
 * Will create the bucket if it hasn't already been created
 * @param file the file to upload
 * @param bucketName the name of the bucket
 */
public void upload(File file, String bucketName) {
    AmazonS3 client = new AmazonS3Client(creds);
    bucketName = ensureValidBucketName(bucketName);

    List<Bucket> buckets = client.listBuckets();
    for (Bucket b : buckets)
        if (b.getName().equals(bucketName)) {
            client.putObject(bucketName, file.getName(), file);
            return;
        }

    //bucket didn't exist: create it
    client.createBucket(bucketName);
    client.putObject(bucketName, file.getName(), file);

}

From source file:org.geppetto.persistence.s3.S3Manager.java

License:Open Source License

public void saveFileToS3(File file, String path) {
    AmazonS3 s3 = getS3Connection();
    s3.putObject(PersistenceHelper.BUCKET_NAME, path, file);
}

From source file:org.huahinframework.emanager.util.S3Utils.java

License:Apache License

/**
 * @param s3//from   w  w w .jav  a  2 s. c  o m
 * @param path
 * @param file
 * @throws URISyntaxException
 */
public static void upload(AmazonS3 s3, String path, File file) throws URISyntaxException {
    URI uri = new URI(path);
    String key = uri.getPath().substring(1);
    s3.putObject(uri.getHost(), key, file);
}

From source file:org.jumpmind.symmetric.io.RedshiftBulkDatabaseWriter.java

License:Open Source License

protected void flush() {
    if (loadedRows > 0) {
        stagedInputFile.close();//from   w w  w.j  a v  a  2s . c om
        statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        AmazonS3 s3client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
        if (isNotBlank(s3Endpoint)) {
            s3client.setEndpoint(s3Endpoint);
        }
        String objectKey = stagedInputFile.getFile().getName();
        try {
            s3client.putObject(bucket, objectKey, stagedInputFile.getFile());
        } catch (AmazonServiceException ase) {
            log.error("Exception from AWS service: " + ase.getMessage());
        } catch (AmazonClientException ace) {
            log.error("Exception from AWS client: " + ace.getMessage());
        }

        try {
            JdbcSqlTransaction jdbcTransaction = (JdbcSqlTransaction) transaction;
            Connection c = jdbcTransaction.getConnection();
            String sql = "COPY " + getTargetTable().getFullyQualifiedTableName() + " ("
                    + Table.getCommaDeliminatedColumns(table.getColumns()) + ") FROM 's3://" + bucket + "/"
                    + objectKey + "' CREDENTIALS 'aws_access_key_id=" + accessKey + ";aws_secret_access_key="
                    + secretKey + "' CSV DATEFORMAT 'YYYY-MM-DD HH:MI:SS' "
                    + (needsExplicitIds ? "EXPLICIT_IDS" : "")
                    + (isNotBlank(appendToCopyCommand) ? (" " + appendToCopyCommand) : "");
            Statement stmt = c.createStatement();

            log.debug(sql);
            stmt.execute(sql);
            stmt.close();
            transaction.commit();
        } catch (SQLException ex) {
            throw platform.getSqlTemplate().translate(ex);
        } finally {
            statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        }

        stagedInputFile.delete();
        try {
            s3client.deleteObject(bucket, objectKey);
        } catch (AmazonServiceException ase) {
            log.error("Exception from AWS service: " + ase.getMessage());
        } catch (AmazonClientException ace) {
            log.error("Exception from AWS client: " + ace.getMessage());
        }

        createStagingFile();
        loadedRows = 0;
        loadedBytes = 0;
    }
}

From source file:org.systemsbiology.athero.SimpleStoreActivitiesS3Impl.java

License:Open Source License

/**
 * /*from w w w  .  ja  va2s  . c  o m*/
 * @param bucket
 *          Name of S3 bucket
 * @param localName
 *          Name of the file to upload
 * @param remoteName
 *          Key for the S3 object
 * @param fromBox
 *          The value for this parameter is used to control the scheduling of this Activity at runtime.
 *          In this case it is putting requirement to run this activity on the machine name passed in.
 *          We want to run this activity on the same box that ran the download. 
 * @return
 *          A Value object
 */

private String uploadFileToS3(String bucket, String localName, String remoteName) {
    System.out.println(
            "uploadToS3 begin bucket=" + bucket + " remoteName=" + remoteName + ", localName=" + localName);
    AmazonS3 storage = getS3Client();
    File f = new File(localName);
    try {
        storage.putObject(bucket, remoteName, f);
        Date expiration = new Date(new Date().getTime() + 1000 * 60 * 30);
        storage.generatePresignedUrl(bucket, remoteName, expiration);
        System.out.println("uploadToS3 done");
        return storage.generatePresignedUrl(bucket, remoteName, expiration).toString();
    } catch (AmazonServiceException e) {
        String message = "Failure uploading to S3";
        System.out.println(message);
        throw e;
    } catch (AmazonClientException e) {
        String message = "Failure uploading to S3";
        System.out.println(message);
        throw e;
    }
}