Java Code Examples for software.amazon.awssdk.services.s3.S3Client#putObject()

The following examples show how to use software.amazon.awssdk.services.s3.S3Client#putObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S3PutObject.java    From piper with Apache License 2.0 6 votes vote down vote up
@Override
public Object handle (TaskExecution aTask) throws Exception {
  
  AmazonS3URI s3Uri = new AmazonS3URI(aTask.getRequiredString("uri"));
  
  String bucketName = s3Uri.getBucket();
  String key = s3Uri.getKey();
  
  S3Client s3 = S3Client.builder().build();
  
  s3.putObject(PutObjectRequest.builder()
                               .bucket(bucketName)
                               .key(key)
                               .acl(aTask.getString("acl")!=null?ObjectCannedACL.fromValue(aTask.getString("acl")):null)
                               .build(), Paths.get(aTask.getRequiredString("filepath")));
  
  return null;
}
 
Example 2
Source File: PutObject.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static String putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) {

        try {
            //Put a file into the bucket
            PutObjectResponse response = s3.putObject(PutObjectRequest.builder()
                            .bucket(bucketName)
                            .key(objectKey)
                            .build(),
                    RequestBody.fromBytes(getObjectFile(objectPath)));

            return response.eTag();
        } catch (S3Exception | FileNotFoundException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }
 
Example 3
Source File: S3Manager.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public String upload(String path) {
    try {
        AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey));
        S3Configuration configuration = S3Configuration.builder()
                .chunkedEncodingEnabled(true)
                .pathStyleAccessEnabled(true)
                .build();
        S3Client client = S3Client.builder()
                .region(clientRegion)
                .credentialsProvider(credentialsProvider)
                .endpointOverride(URI.create(endpoint))
                .serviceConfiguration(configuration)
                .build();
        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .contentType("application/octet-stream")
                .bucket(bucketName)
                .key(path)
                .build();
        RequestBody requestBody = RequestBody.fromFile(Paths.get(path));
        client.putObject(putObjectRequest, requestBody);
        logger.info("S3: upload file success");
        String url = getS3Url(path);
        logger.info("url:{}", url);
        return url;
    } catch (Exception e) {
        logger.error("S3: failed to upload file :{}, error: {}", path, e.getMessage());
    }
    return null;
}
 
Example 4
Source File: S3TestUtils.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
public static void putObject(Class<?> testClass, S3Client s3, String bucketName, String objectKey, String content) {
    s3.putObject(r -> r.bucket(bucketName).key(objectKey), RequestBody.fromString(content));
    Waiter.run(() -> s3.getObjectAcl(r -> r.bucket(bucketName).key(objectKey)))
          .ignoringException(NoSuchBucketException.class, NoSuchObjectException.class)
          .orFail();
    addCleanupTask(testClass, () -> s3.deleteObject(r -> r.bucket(bucketName).key(objectKey)));
}
 
Example 5
Source File: Synch.java    From pegasus with Apache License 2.0 5 votes vote down vote up
/**
 * Transfers the input files to the specified bucket
 *
 * @param bucket
 * @param keyPrefix the prefix mimicking deep LFN functionality
 * @param files
 */
public void transferInputFiles(String bucket, String keyPrefix, List<String> files) {
    S3Client s3Client = S3Client.builder().region(mAWSRegion).build();
    for (String f : files) {
        File file = new File(f);
        if (file.exists()) {
            String key = keyPrefix + file.getName();
            mLogger.debug(
                    "Attempting to upload file "
                            + file
                            + " to bucket "
                            + bucket
                            + " with key "
                            + key);
            s3Client.putObject(
                    PutObjectRequest.builder().bucket(bucket).key(key).build(),
                    RequestBody.of(file));
            mLogger.debug(
                    "Uploaded file " + file + " to bucket " + bucket + " with key " + key);
        } else {
            throw new RuntimeException("Unable file does not exist " + f);
        }
    }
    try {
        s3Client.close();
    } catch (Exception ex) {
        mLogger.error("Unable to close the s3 client", ex);
    }
}