AWS S3 - Producer Operations
Common Pattern
Most producer operations follow the same pattern: set the operation parameter on the endpoint URI, optionally set a key via the CamelAwsS3Key header, and send the exchange.
-
Java
-
XML
-
YAML
from("direct:start")
.setHeader("CamelAwsS3Key", constant("camelKey"))
.to("aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client&operation=getObject")
.to("mock:result"); <route>
<from uri="direct:start"/>
<setHeader name="CamelAwsS3Key"><constant>camelKey</constant></setHeader>
<to uri="aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client&operation=getObject"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- setHeader:
name: CamelAwsS3Key
constant: "camelKey"
- to:
uri: aws2-s3://mycamelbucket
parameters:
amazonS3Client: "#amazonS3Client"
operation: getObject
- to:
uri: mock:result Bucket-level operations (listBuckets, deleteBucket, listObjects, createBucket, etc.) don’t require a key header — just set the operation parameter.
The operations take the bucket from the endpoint URI, unless the CamelAwsS3OverrideBucketName header is set, and the key from the keyName option, unless the CamelAwsS3Key header is set.
Operations Reference
| Operation | Description | Notes |
|---|---|---|
| List all buckets for this account in this region. | Paged, see Paging listObjects and listBuckets |
| Create a new bucket. | |
| Delete a bucket. | |
| Check if a bucket exists and you have permission to access it. | Sets the |
| List objects in a bucket. | Paged, see Paging listObjects and listBuckets |
| Get a single object. Returns its content as a stream. | |
| Get a byte range from an object. | See getObjectRange |
| Delete an object. | |
| Delete multiple objects in a single request. | See deleteObjects |
| Copy an object from one bucket to another. | See copyObject |
| Retrieve metadata from an object without returning the object itself. | Requires a key (the |
(no operation) | Upload the body to S3. This is what the producer does when neither the | |
(no operation, with | Upload the body in parts. | See Multipart Upload |
| Generate a presigned download URL. | |
| Generate a presigned upload URL. | Requires |
| Restore an archived object from Glacier storage. | See restoreObject |
| Get the tags of an object. | |
| Set tags on an object. | Requires |
| Delete all tags from an object. | |
| Get the ACL of an object. | |
| Set the ACL on an object. | Use |
| Get the tags of a bucket. | |
| Set tags on a bucket. | Requires |
| Delete all tags from a bucket. | |
| Get the versioning configuration of a bucket. | |
| Set the versioning configuration of a bucket. | Use |
| Get the bucket policy as a JSON string. | |
| Set a bucket policy. | See putBucketPolicy |
| Delete the bucket policy. |
Operation Details
This section covers operations that require special configuration beyond the common pattern.
Paging listObjects and listBuckets
Amazon S3 answers listObjects and listBuckets one page at a time: listObjects returns at most 1,000 objects per page, listBuckets at most 10,000 buckets, and both default to that maximum. Set CamelAwsS3MaxKeys to ask for a smaller page. When more entries follow, the producer sets CamelAwsS3IsTruncated to true and CamelAwsS3ContinuationToken to the token of the next page; send that token back in CamelAwsS3ContinuationToken to read on. On the last page the producer sets CamelAwsS3IsTruncated to false and removes CamelAwsS3ContinuationToken, so a route that loops while the header is present stops by itself.
from("direct:listAll")
.loopDoWhile(PredicateBuilder.or(header("CamelAwsS3ContinuationToken").isNotNull(),
exchangeProperty("firstPass").isNull()))
.setProperty("firstPass", constant(false))
.to("aws2-s3://mybucket?operation=listObjects")
.log("got ${body.size()} objects, more: ${header.CamelAwsS3IsTruncated}")
.end(); The two operations share the headers. A token of one operation is not valid for the other, so remove CamelAwsS3ContinuationToken and CamelAwsS3MaxKeys before another list call, for example between a listBuckets page and the listObjects call made for each of its buckets; otherwise S3 rejects the request.
In POJO mode the token is read from the ListObjectsV2Request body, and the producer still sets the two headers on the answer.
Upload (Single Upload)
Upload a file to S3 based on the body content:
-
Java
-
XML
-
YAML
from("direct:start")
.setHeader("CamelAwsS3Key", constant("camel.txt"))
.setBody(constant("Camel rocks!"))
.to("aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client")
.to("mock:result"); <route>
<from uri="direct:start"/>
<setHeader name="CamelAwsS3Key"><constant>camel.txt</constant></setHeader>
<setBody><constant>Camel rocks!</constant></setBody>
<to uri="aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- setHeader:
name: CamelAwsS3Key
constant: "camel.txt"
- setBody:
constant: "Camel rocks!"
- to:
uri: aws2-s3://mycamelbucket
parameters:
amazonS3Client: "#amazonS3Client"
- to:
uri: mock:result Multipart Upload
For large files, use multipart upload with configurable part size:
from("direct:start")
.setHeader("CamelAwsS3Key", constant("large-file.txt"))
.to("aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client&multiPartUpload=true&autoCreateBucket=true&partSize=1048576")
.to("mock:result"); copyObject
Copy an object between buckets using destination headers:
-
Java
-
XML
-
YAML
from("direct:start")
.setHeader("CamelAwsS3BucketDestinationName", constant("camelDestinationBucket"))
.setHeader("CamelAwsS3Key", constant("camelKey"))
.setHeader("CamelAwsS3DestinationKey", constant("camelDestinationKey"))
.to("aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client&operation=copyObject")
.to("mock:result"); <route>
<from uri="direct:start"/>
<setHeader name="CamelAwsS3BucketDestinationName"><constant>camelDestinationBucket</constant></setHeader>
<setHeader name="CamelAwsS3Key"><constant>camelKey</constant></setHeader>
<setHeader name="CamelAwsS3DestinationKey"><constant>camelDestinationKey</constant></setHeader>
<to uri="aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client&operation=copyObject"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- setHeader:
name: CamelAwsS3BucketDestinationName
constant: "camelDestinationBucket"
- setHeader:
name: CamelAwsS3Key
constant: "camelKey"
- setHeader:
name: CamelAwsS3DestinationKey
constant: "camelDestinationKey"
- to:
uri: aws2-s3://mycamelbucket
parameters:
amazonS3Client: "#amazonS3Client"
operation: copyObject
- to:
uri: mock:result This copies the object camelKey from mycamelbucket to camelDestinationBucket with the key camelDestinationKey.
getObjectRange
Download a specific byte range from an object using the CamelAwsS3RangeStart and CamelAwsS3RangeEnd headers:
from("direct:start")
.setHeader("CamelAwsS3Key", constant("camelKey"))
.setHeader("CamelAwsS3RangeStart", constant("0"))
.setHeader("CamelAwsS3RangeEnd", constant("9"))
.to("aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client&operation=getObjectRange")
.to("mock:result"); This returns the first 10 bytes (0 to 9) of the object.
createDownloadLink
Generate a presigned download URL. Parameters accessKey, secretKey, and region are mandatory for this operation if the S3 client is autowired from the registry:
from("direct:start")
.setHeader("CamelAwsS3Key", constant("camelKey"))
.to("aws2-s3://mycamelbucket?accessKey=xxx&secretKey=yyy®ion=eu-west-1&operation=createDownloadLink")
.to("mock:result"); | If checksum validations are enabled, the URL will no longer be browser compatible because it adds a signed header that must be included in the HTTP request. |
The same pattern applies to createUploadLink.
deleteObjects
Delete multiple objects in a single request. Requires a List<String> of keys via the CamelAwsS3KeysToDelete header:
from("direct:start")
.process(exchange -> {
List<String> keys = List.of("key1", "key2", "key3");
exchange.getIn().setHeader("CamelAwsS3KeysToDelete", keys);
})
.to("aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client&operation=deleteObjects")
.to("mock:result"); restoreObject
Restore an archived object from Glacier storage:
from("direct:start")
.setHeader("CamelAwsS3Key", constant("camelKey"))
.setHeader("CamelAwsS3RestoreDays", constant(1))
.setHeader("CamelAwsS3RestoreTier", constant("Expedited"))
.to("aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client&operation=restoreObject")
.to("mock:result"); putBucketPolicy
Set a bucket policy using a JSON policy string via the CamelAwsS3BucketPolicy header:
from("direct:start")
.process(exchange -> {
String policy = """
{"Version": "2012-10-17", "Statement": [{"Effect": "Allow", \
"Principal": "*", "Action": "s3:GetObject", \
"Resource": "arn:aws:s3:::mycamelbucket/*"}]}""";
exchange.getIn().setHeader("CamelAwsS3BucketPolicy", policy);
})
.to("aws2-s3://mycamelbucket?amazonS3Client=#amazonS3Client&operation=putBucketPolicy")
.to("mock:result"); Minimum IAM Permissions
Producer
For making the producer work, you’ll need at least PutObject and ListBuckets permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::*/*"
},
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::*"
}
]
} If using bucket auto-creation, add the CreateBucket permission:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::*/*"
},
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": "s3:CreateBucket",
"Resource": "arn:aws:s3:::*"
}
]
} Consumer
For making the consumer work, you’ll need at least GetObject, ListBucket, and DeleteObject permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::*/*"
},
{
"Effect": "Allow",
"Action": "s3:DeleteObject",
"Resource": "arn:aws:s3:::*/*"
}
]
} By default, the consumer uses the deleteAfterRead option, meaning the object will be deleted once consumed, which is why the DeleteObject permission is required.