Search in sources :

Example 61 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project druid by druid-io.

the class S3Utils method uploadFileIfPossible.

/**
 * Uploads a file to S3 if possible. First trying to set ACL to give the bucket owner full control of the file before uploading.
 *
 * @param service    S3 client
 * @param disableAcl true if ACL shouldn't be set for the file
 * @param key        The key under which to store the new object.
 * @param file       The path of the file to upload to Amazon S3.
 */
static void uploadFileIfPossible(ServerSideEncryptingAmazonS3 service, boolean disableAcl, String bucket, String key, File file) {
    final PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, file);
    if (!disableAcl) {
        putObjectRequest.setAccessControlList(S3Utils.grantFullControlToBucketOwner(service, bucket));
    }
    log.info("Pushing [%s] to bucket[%s] and key[%s].", file, bucket, key);
    service.putObject(putObjectRequest);
}
Also used : PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 62 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project hazelcast by hazelcast.

the class S3MockTest method generateAndUploadObjects.

private void generateAndUploadObjects(String bucketName, String prefix, int objectCount, int lineCount, String textPrefix) {
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < objectCount; i++) {
        range(0, lineCount).forEach(j -> builder.append(textPrefix).append(j).append(lineSeparator()));
        PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(bucketName).key(prefix + i).build();
        s3Client.putObject(putObjectRequest, RequestBody.fromString(builder.toString()));
        builder.setLength(0);
    }
}
Also used : PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest)

Example 63 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project aws-doc-sdk-examples by awsdocs.

the class S3ObjectOperations method main.

public static void main(String[] args) throws IOException {
    final String USAGE = "\n" + "Usage:\n" + "    <bucketName> <key>\n\n" + "Where:\n" + "    bucketName - the Amazon S3 bucket to create.\n\n" + "    key - the key to use.\n\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String key = args[1];
    // snippet-start:[s3.java2.s3_object_operations.upload]
    Region region = Region.US_WEST_2;
    s3 = S3Client.builder().region(region).build();
    createBucket(s3, bucketName, region);
    PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(key).build();
    s3.putObject(objectRequest, RequestBody.fromByteBuffer(getRandomByteBuffer(10_000)));
    // snippet-end:[s3.java2.s3_object_operations.upload]
    // Multipart upload example
    String multipartKey = "multiPartKey";
    multipartUpload(bucketName, multipartKey);
    // snippet-start:[s3.java2.s3_object_operations.pagination]
    ListObjectsV2Request listObjectsReqManual = ListObjectsV2Request.builder().bucket(bucketName).maxKeys(1).build();
    boolean done = false;
    while (!done) {
        ListObjectsV2Response listObjResponse = s3.listObjectsV2(listObjectsReqManual);
        for (S3Object content : listObjResponse.contents()) {
            System.out.println(content.key());
        }
        if (listObjResponse.nextContinuationToken() == null) {
            done = true;
        }
        listObjectsReqManual = listObjectsReqManual.toBuilder().continuationToken(listObjResponse.nextContinuationToken()).build();
    }
    // snippet-end:[s3.java2.s3_object_operations.pagination]
    // snippet-start:[s3.java2.s3_object_operations.iterative]
    ListObjectsV2Request listReq = ListObjectsV2Request.builder().bucket(bucketName).maxKeys(1).build();
    ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq);
    // Process response pages
    listRes.stream().flatMap(r -> r.contents().stream()).forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
    // snippet-end:[s3.java2.s3_object_operations.iterative]
    // snippet-start:[s3.java2.s3_object_operations.stream]
    // Helper method to work with paginated collection of items directly
    listRes.contents().stream().forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
    // snippet-start:[s3.java2.s3_object_operations.forloop]
    for (S3Object content : listRes.contents()) {
        System.out.println(" Key: " + content.key() + " size = " + content.size());
    }
    // snippet-end:[s3.java2.s3_object_operations.forloop]
    // snippet-start:[s3.java2.s3_object_operations.download]
    GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucketName).key(key).build();
    s3.getObject(getObjectRequest);
    // snippet-end:[s3.java2.s3_object_operations.download]
    // snippet-start:[s3.java2.s3_object_operations.delete]
    DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucketName).key(key).build();
    s3.deleteObject(deleteObjectRequest);
    // snippet-end:[s3.java2.s3_object_operations.delete]
    // Delete an object
    deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucketName).key(multipartKey).build();
    s3.deleteObject(deleteObjectRequest);
    deleteBucket(s3, bucketName);
    System.out.println("Done");
}
Also used : CompletedMultipartUpload(software.amazon.awssdk.services.s3.model.CompletedMultipartUpload) S3Object(software.amazon.awssdk.services.s3.model.S3Object) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) Random(java.util.Random) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response) ByteBuffer(java.nio.ByteBuffer) HeadBucketResponse(software.amazon.awssdk.services.s3.model.HeadBucketResponse) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) CreateBucketRequest(software.amazon.awssdk.services.s3.model.CreateBucketRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) WaiterResponse(software.amazon.awssdk.core.waiters.WaiterResponse) Region(software.amazon.awssdk.regions.Region) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) S3Client(software.amazon.awssdk.services.s3.S3Client) DeleteBucketRequest(software.amazon.awssdk.services.s3.model.DeleteBucketRequest) HeadBucketRequest(software.amazon.awssdk.services.s3.model.HeadBucketRequest) IOException(java.io.IOException) S3Waiter(software.amazon.awssdk.services.s3.waiters.S3Waiter) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) CompletedPart(software.amazon.awssdk.services.s3.model.CompletedPart) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) ListObjectsV2Iterable(software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable) CreateBucketConfiguration(software.amazon.awssdk.services.s3.model.CreateBucketConfiguration) RequestBody(software.amazon.awssdk.core.sync.RequestBody) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) ListObjectsV2Iterable(software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable) Region(software.amazon.awssdk.regions.Region) S3Object(software.amazon.awssdk.services.s3.model.S3Object) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response)

Example 64 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project aws-doc-sdk-examples by awsdocs.

the class S3AsyncOps method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    S3AsyncOps <bucketName> <key> <path>\n\n" + "Where:\n" + "    bucketName - the name of the Amazon S3 bucket (for example, bucket1). \n\n" + "    key - the name of the object (for example, book.pdf). \n" + "    path - the local path to the file (for example, C:/AWS/book.pdf). \n";
    if (args.length != 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String key = args[1];
    String path = args[2];
    Region region = Region.US_WEST_2;
    S3AsyncClient client = S3AsyncClient.builder().region(region).build();
    PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(key).build();
    // Put the object into the bucket
    CompletableFuture<PutObjectResponse> future = client.putObject(objectRequest, AsyncRequestBody.fromFile(Paths.get(path)));
    future.whenComplete((resp, err) -> {
        try {
            if (resp != null) {
                System.out.println("Object uploaded. Details: " + resp);
            } else {
                // Handle error
                err.printStackTrace();
            }
        } finally {
            // Only close the client when you are completely done with it
            client.close();
        }
    });
    future.join();
}
Also used : PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) Region(software.amazon.awssdk.regions.Region) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient)

Example 65 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project aws-doc-sdk-examples by awsdocs.

the class ManagingObjectTags method putS3ObjectTags.

// snippet-start:[s3.java2.s3_object_manage_tags.main]
public static void putS3ObjectTags(S3Client s3, String bucketName, String objectKey, String objectPath) {
    try {
        // Define the tags.
        Tag tag1 = Tag.builder().key("Tag 1").value("This is tag 1").build();
        Tag tag2 = Tag.builder().key("Tag 2").value("This is tag 2").build();
        List<Tag> tags = new ArrayList<Tag>();
        tags.add(tag1);
        tags.add(tag2);
        Tagging allTags = Tagging.builder().tagSet(tags).build();
        PutObjectRequest putOb = PutObjectRequest.builder().bucket(bucketName).key(objectKey).tagging(allTags).build();
        s3.putObject(putOb, RequestBody.fromBytes(getObjectFile(objectPath)));
    } catch (S3Exception e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) Tagging(software.amazon.awssdk.services.s3.model.Tagging) Tag(software.amazon.awssdk.services.s3.model.Tag) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest)

Aggregations

PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)140 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)80 ByteArrayInputStream (java.io.ByteArrayInputStream)63 Test (org.junit.Test)61 File (java.io.File)35 IOException (java.io.IOException)32 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)29 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)23 Upload (com.amazonaws.services.s3.transfer.Upload)23 AmazonClientException (com.amazonaws.AmazonClientException)20 InputStream (java.io.InputStream)18 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)14 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)14 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)14 BusinessObjectDataEntity (org.finra.herd.model.jpa.BusinessObjectDataEntity)13 HashMap (java.util.HashMap)12 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)10 AmazonServiceException (com.amazonaws.AmazonServiceException)9 AmazonS3 (com.amazonaws.services.s3.AmazonS3)9 Exchange (org.apache.camel.Exchange)8