Java Code Examples for com.amazonaws.services.s3.AmazonS3.putObject()

The following are Jave code examples for showing how to use putObject() of the com.amazonaws.services.s3.AmazonS3 class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: full-javaee-app   File: S3Object.java   View Source Code Vote up 6 votes
public static String webHookDump(InputStream stream, String school, String extension) {
    if (stream != null) {
        extension = extension == null || extension.isEmpty() ? ".xml" : extension.contains(".") ? extension : "." + extension;
        String fileName = "webhooks/" + school + "/" + school + "_" + Clock.getCurrentDateDashes() + "_" + Clock.getCurrentTime() + extension;
        AmazonS3 s3 = new AmazonS3Client();
        Region region = Region.getRegion(Regions.US_WEST_2);
        s3.setRegion(region);
        try {
            File file = CustomUtilities.inputStreamToFile(stream);
            s3.putObject(new PutObjectRequest(name, fileName, file));
            return CustomUtilities.fileToString(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return "";
}
 
Example 2
Project: dataset-lib   File: S3Provider.java   View Source Code Vote up 5 votes
public void put(Object[] params) {
    String key = params[0].toString();
    File file = new File(params[0].toString());
    AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
    try {
        s3client.putObject(new PutObjectRequest(bucketName, key, file));
    } 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());
    }
}
 
Example 3
Project: mm-system-2017   File: ClientUploadFile.java   View Source Code Vote up 5 votes
/**
 * Use UploadFile.BucketName
 * @param keyName
 * @param filePathToUpload
 * @param BUCKET_NAME
 */
public static void uploadFile(String keyName,File fileToUpload,String BUCKET_NAME,String contentType) {
	
	

       AmazonS3 s3Client = s3client(); 

       System.out.println(s3Client);
       
       try {
       	System.out.println("Uploading a new object to S3 from a file\n");
           File file = fileToUpload;
           PutObjectRequest objectToPut = new PutObjectRequest(BUCKET_NAME, keyName, file);
           ObjectMetadata meta = new ObjectMetadata();
           meta.setContentType(contentType);
           objectToPut.setMetadata(meta);
           s3Client.putObject(objectToPut);

        } 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());
       }  

	
	
}
 
Example 4
Project: ooso   File: Commons.java   View Source Code Vote up 5 votes
public static void storeObject(String contentType,
                               String content,
                               String destBucket,
                               String destKey) throws UnsupportedEncodingException {
    AmazonS3 s3Client = AmazonS3Provider.getS3Client();

    ObjectMetadata metadata = prepareObjectMetadata(contentType, content);

    s3Client.putObject(
            destBucket,
            destKey,
            new StringInputStream(content),
            metadata);
}
 
Example 5
Project: ooso   File: Commons.java   View Source Code Vote up 5 votes
public static void storeObject(String contentType,
                               File content,
                               String destBucket,
                               String destKey) {
    AmazonS3 s3Client = AmazonS3Provider.getS3Client();

    ObjectMetadata metadata = prepareObjectMetadata(contentType);
    s3Client.putObject(new PutObjectRequest(destBucket, destKey, content).withMetadata(metadata));
}
 
Example 6
Project: radosgw-admin4j   File: BaseTest.java   View Source Code Vote up 5 votes
protected static void createSomeObjects(AmazonS3 s3) {
  String bucketName = "bucket-" + UUID.randomUUID().toString().toLowerCase();
  s3.createBucket(bucketName);
  for (int i = 0; i < 3; i++) {

    s3.putObject(bucketName, "OBJECT-" + UUID.randomUUID(), createString(4096));
  }
  // Usage data are generated in the async way, hope it will be available after wait.
  try {
    Thread.sleep(5000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
}
 
Example 7
Project: rxjava2-aws   File: Sqs.java   View Source Code Vote up 5 votes
public static String sendToQueueUsingS3(AmazonSQS sqs, String queueUrl, AmazonS3 s3, String bucketName,
        Map<String, String> headers, byte[] message, Callable<String> s3IdFactory) {
    Preconditions.checkNotNull(sqs);
    Preconditions.checkNotNull(s3);
    Preconditions.checkNotNull(queueUrl);
    Preconditions.checkNotNull(bucketName);
    Preconditions.checkNotNull(message);
    String s3Id;
    try {
        s3Id = s3IdFactory.call();
    } catch (final Exception e1) {
        throw new RuntimeException(e1);
    }
    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(message.length);
    for (final Entry<String, String> header : headers.entrySet()) {
        metadata.setHeader(header.getKey(), header.getValue());
    }
    s3.putObject(bucketName, s3Id, new ByteArrayInputStream(message), metadata);
    try {
        sqs.sendMessage(queueUrl, s3Id);
    } catch (final RuntimeException e) {
        try {
            s3.deleteObject(bucketName, s3Id);
            throw e;
        } catch (final RuntimeException e2) {
            throw new io.reactivex.exceptions.CompositeException(e, e2);
        }
    }
    return s3Id;
}
 
Example 8
Project: opentest   File: PutS3Object.java   View Source Code Vote up 5 votes
@Override
public void run() {
    super.run();

    String awsCredentialsProfile = this.readStringArgument("awsProfile", "default");
    String bucket = this.readStringArgument("bucket");
    String objectKey = this.readStringArgument("objectKey");
    String sourceFilePath = this.readStringArgument("sourceFile");
    
    File sourceFile = new File(sourceFilePath);
    if (sourceFile.exists()) {
        try {
            AmazonS3 s3Client = new AmazonS3Client(
                    new ProfileCredentialsProvider(awsCredentialsProfile));
            s3Client.putObject(new PutObjectRequest(bucket, objectKey, sourceFile));
         } catch (Exception ex) {
           throw new RuntimeException(String.format(
                    "Failed to upload file \"%s\" to S3 bucket \"%s\"",
                    sourceFilePath,
                    bucket), ex);
        } 
    } else {
        throw new RuntimeException(String.format(
                "Source file \"%s\" doesn't exist",
                sourceFilePath));
    }    
}
 
Example 9
Project: bayou   File: S3LoggerBase.java   View Source Code Vote up 4 votes
/**
 * Performs an S3 Put Object operation storing the UTF-8 bytes of logMsg under the given key
 * using construction provided AWS credentials.
 *
 * @param objectKey the S3 object key. may not be null or whitespace only.
 * @param logMsg the message to store
 * @throws IllegalArgumentException if objectKey is whitespace only.
 */
void putToS3(String objectKey, String logMsg)
{
    if(objectKey == null)
        throw new NullPointerException("objectKey");

    if(objectKey.trim().length() == 0)
        throw new IllegalArgumentException("objectKey may not be only whitespace.");

     /*
     * Make the client used to send the log msg to S3.
     */
    AmazonS3 client;
    {
        Regions regions = Regions.US_EAST_1;
        if(_credentials == null)
        {
            client = AmazonS3ClientBuilder.standard() // get creds from environment
                    .withRegion(regions)
                    .build();
        }
        else
        {
            client =  AmazonS3ClientBuilder.standard()
                    .withCredentials(new AWSStaticCredentialsProvider(_credentials))
                    .withRegion(regions)
                    .build();
        }
    }


    /*
     * Store the log msg in S3.
     */
    byte[] logMsgBytes = logMsg.getBytes(StandardCharsets.UTF_8);

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(logMsgBytes.length);

    client.putObject(_bucketName, objectKey, new ByteArrayInputStream(logMsgBytes), metadata);

    _logger.debug("exiting");
}