Skip to main content

Actions

The AWS S3 connector exposes the following clients:

Available clients:

ClientPurpose
ClientBucket management, object CRUD, presigned URLs, and multipart uploads

Client​

The Client provides programmatic access to Amazon S3, supporting bucket management, object uploads and downloads with automatic type binding, presigned URL generation, and multipart uploads for large objects.

Configuration​

ConnectionConfig

FieldTypeDefaultDescription
authauth:AuthConfigRequiredAuthentication configuration: static credentials, AWS profile, STS assume-role, web identity (OIDC), IAM Identity Center (SSO), external credential process, or the default provider chain
regionaws:Region|stringus-east-1AWS region: an aws:Region enum member or a plain region string (e.g., "us-east-1") for regions not yet in the enum
endpointaws:EndpointConfigOptionalOptional endpoint options: FIPS/dualstack variants, or a custom endpoint override (e.g. VPC interface endpoints)

Initializing the client​

import ballerinax/aws.s3;

s3:ConnectionConfig config = {
auth: {
accessKeyId: "AKIAIOSFODNN7EXAMPLE",
secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
},
region: "us-east-1"
};
s3:Client client = check new (config);

Operations​

Bucket Management​

createBucket

Creates an S3 bucket with optional access control and object ownership settings.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
aclCannedACLNoSpecifies accessibility for this bucket (e.g., "private", "public-read"). Defaults to PRIVATE
objectOwnershipObjectOwnershipNoSpecifies ownership of objects uploaded to this bucket (e.g., "BucketOwnerEnforced", "ObjectWriter"). Defaults to BUCKET_OWNER_ENFORCED
objectLockEnabledbooleanNoEnable Object Lock to prevent objects from being deleted or overwritten

Returns: Error?

Sample code:

check client->createBucket("my-app-data");
deleteBucket

Deletes an S3 bucket. The bucket must be empty before it can be deleted.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket

Returns: Error?

Sample code:

check client->deleteBucket("my-app-data");
listBuckets

Lists all S3 buckets in the AWS account associated with the configured credentials.

Parameters:

NameTypeRequiredDescription
———No parameters

Returns: Bucket[]|Error

Sample code:

s3:Bucket[] buckets = check client->listBuckets();

Sample response:

[
{
"name": "my-app-data",
"creationDate": "2024-01-15T10:30:00.000Z"
},
{
"name": "my-backups",
"creationDate": "2024-03-20T08:00:00.000Z"
}
]
getBucketLocation

Gets the AWS region in which an S3 bucket resides.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket

Returns: string|Error

Sample code:

string region = check client->getBucketLocation("my-app-data");

Sample response:

"us-west-2"

Object Storage​

putObjectFromFile

Uploads an S3 object from a local file path. Use this when the file already exists on disk.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
filePathstringYesThe local file path to upload
contentTypestringNoThe MIME type of the content
aclCannedACLNoSpecifies accessibility for this object. Defaults to PRIVATE
storageClassStorageClassNoThe storage class of the object. Defaults to STANDARD
metadatamap<string>NoCustom data to attach to the object (e.g., &#123;"author": "John"&#125;)
cacheControlstringNoSpecifies caching behavior along the request/reply chain
contentDispositionstringNoSpecifies presentational information for the object
contentEncodingstringNoSpecifies what content encodings have been applied to the object
contentLanguagestringNoThe language the content is in (e.g., "en-US")
expiresstringNoThe date and time at which the object is no longer cacheable
taggingstringNoTags for the object (e.g., "env=prod&team=finance")
serverSideEncryptionstringNoEncryption type ("AES256" or "aws:kms")
fileFormatFileFormatNoThe file format to use for serializing record content. Overrides the format inferred from the object key extension

Returns: Error?

Sample code:

check client->putObjectFromFile("my-app-data", "reports/report.pdf", "/tmp/report.pdf");
putObject

Uploads an S3 object from in-memory content with automatic type serialization. The content parameter supports:

  • Built-in types: byte[], string, json, xml
  • Custom record types (e.g., User): serialized as JSON for .json object keys and as XML for .xml object keys
  • Record arrays (e.g., User[]) and stream<record {}, error?>: serialized as CSV with field names as headers for .csv object keys
  • stream<byte[], error?>: collected into bytes before uploading

The fileFormat parameter overrides the format inferred from the object key extension.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
contentUploadContentYesThe object content. Accepted types: byte[], string, json, xml, record &#123;&#125;, record &#123;&#125;[], stream&#60;byte[], error?&#62;, stream&#60;record &#123;&#125;, error?&#62;
contentTypestringNoThe MIME type of the content
aclCannedACLNoSpecifies accessibility for this object. Defaults to PRIVATE
storageClassStorageClassNoThe storage class of the object. Defaults to STANDARD
metadatamap<string>NoCustom data to attach to the object (e.g., &#123;"author": "John"&#125;)
cacheControlstringNoSpecifies caching behavior along the request/reply chain
contentDispositionstringNoSpecifies presentational information for the object
contentEncodingstringNoSpecifies what content encodings have been applied to the object
contentLanguagestringNoThe language the content is in (e.g., "en-US")
expiresstringNoThe date and time at which the object is no longer cacheable
taggingstringNoTags for the object (e.g., "env=prod&team=finance")
serverSideEncryptionstringNoEncryption type ("AES256" or "aws:kms")
fileFormatFileFormatNoThe file format to use for serializing record content. Overrides the format inferred from the object key extension

Returns: Error?

Sample code:

check client->putObject("my-app-data", "data.json", {"orderId": "ORD-123", "total": 129.99});
putObjectAsStream

Uploads an S3 object from a byte stream. Use this for large files to avoid loading the entire content into memory. The contentLength parameter is required.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
contentStreamstream<byte[], error?>YesThe content stream to upload
contentLengthintYesThe size of the content in bytes
contentTypestringNoThe MIME type of the content
aclCannedACLNoSpecifies accessibility for this object. Defaults to PRIVATE
storageClassStorageClassNoThe storage class of the object. Defaults to STANDARD
metadatamap<string>NoCustom data to attach to the object
cacheControlstringNoSpecifies caching behavior along the request/reply chain
contentDispositionstringNoSpecifies presentational information for the object
contentEncodingstringNoSpecifies what content encodings have been applied to the object
contentLanguagestringNoThe language the content is in
expiresstringNoThe date and time at which the object is no longer cacheable
taggingstringNoTags for the object
serverSideEncryptionstringNoEncryption type ("AES256" or "aws:kms")
fileFormatFileFormatNoThe file format to use for serializing record content

Returns: Error?

Sample code:

stream<byte[], error?> fileStream = check io:fileReadBlocksAsStream("/tmp/large-file.bin");
check client->putObjectAsStream("my-app-data", "large-file.bin", fileStream, contentLength = 52428800);
getObject

Downloads an S3 object from a bucket with automatic type deserialization. The return type is inferred from the variable on the left-hand side. Supported target types:

  • Built-in types: byte[], string, json, xml
  • Custom record types (e.g., User): deserialized from JSON for .json object keys and from XML for .xml object keys
  • Record arrays (e.g., User[]) and stream<record {}, error?>: deserialized from CSV with the first row as headers for .csv object keys
  • stream<byte[], error?>: retrieves large objects without loading the entire content into memory

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
targetTypetypedesc<RetrievableType>NoExpected return type for automatic data binding. Inferred from the assignment target
versionIdstringNoGet a specific version of the object (when versioning is enabled)
rangestringNoDownloads the specified byte range of an object (e.g., "bytes=0-1023")
ifMatchstringNoReturn the object only if its ETag matches the one specified
ifNoneMatchstringNoReturn the object only if its ETag differs from the one specified
ifModifiedSincestringNoReturn the object only if it has been modified since the specified time (e.g., "2024-01-15T00:00:00Z")
ifUnmodifiedSincestringNoReturn the object only if it has not been modified since the specified time
partNumberintNoThe part number of the file part to retrieve
responseContentTypestringNoOverride the MIME type of the content in the response
responseContentDispositionstringNoOverride the presentational information for the object in the response

Returns: RetrievableType|Error

Sample code:

json data = check client->getObject("my-app-data", "data.json");

Sample response:

{
"orderId": "ORD-123",
"total": 129.99,
"status": "shipped"
}
deleteObject

Deletes an S3 object from a bucket. Supports versioned buckets via versionId and MFA-protected deletion.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
versionIdstringNoDelete a specific version of the object (when versioning is enabled)
mfastringNoMulti-factor authentication token (needed if MFA Delete is turned on for the bucket)
bypassGovernanceRetentionbooleanNoSkip the lock protection and delete the object even if it is protected (use with caution)

Returns: Error?

Sample code:

check client->deleteObject("my-app-data", "reports/old-report.csv");
listObjects

Lists S3 objects in a bucket. Supports prefix filtering, delimiter-based grouping (folder simulation), and pagination via continuation tokens.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
prefixstringNoFilter objects that start with this value (e.g., "reports/" for all objects in the reports folder)
delimiterstringNoCharacter to group object keys (e.g., "/" to list like folders)
maxKeysintNoMaximum number of objects to return (1–1000)
continuationTokenstringNoToken to get the next page of results (from a previous isTruncated response)
startAfterstringNoList objects after this key name
fetchOwnerbooleanNoInclude owner information in the results
encodingTypestringNoEncoding type for object keys (e.g., "url")

Returns: ListObjectsResponse|Error

Sample code:

s3:ListObjectsResponse response = check client->listObjects("my-app-data", prefix = "reports/");

Sample response:

{
"objects": [
{
"key": "reports/q1-2024.csv",
"size": 4096,
"lastModified": "2024-03-31T12:00:00.000Z",
"eTag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
"storageClass": "STANDARD"
},
{
"key": "reports/q2-2024.csv",
"size": 5120,
"lastModified": "2024-06-30T12:00:00.000Z",
"eTag": "\"a87ff679a2f3e71d9181a67b7542122c\"",
"storageClass": "STANDARD"
}
],
"count": 2,
"isTruncated": false
}
copyObject

Copies an S3 object from one location to another within or across buckets. Supports metadata replacement, conditional copies, and storage class changes on the destination.

Parameters:

NameTypeRequiredDescription
sourceBucketstringYesThe source bucket name
sourceKeystringYesThe source object path
destinationBucketstringYesThe destination bucket name
destinationKeystringYesThe destination object path
aclCannedACLNoSpecifies accessibility for the copied object. Defaults to PRIVATE
storageClassStorageClassNoStorage class for the copied object. Defaults to STANDARD
metadataDirectivestringNo"COPY" to keep the original metadata or "REPLACE" to use new metadata
metadatamap<string>NoNew metadata for the copied object (only used when metadataDirective is "REPLACE")
contentTypestringNoThe MIME type of the copied object
cacheControlstringNoSpecifies caching behavior along the request/reply chain
contentDispositionstringNoSpecifies presentational information for the object
contentEncodingstringNoSpecifies what content encodings have been applied to the object
taggingstringNoTags for the copied object (e.g., "env=prod&team=finance")
copySourceIfMatchstringNoCopy only if the source ETag matches the one specified
copySourceIfNoneMatchstringNoCopy only if the source ETag differs from the one specified
copySourceIfModifiedSincestringNoCopy only if the source has been modified since the specified time
copySourceIfUnmodifiedSincestringNoCopy only if the source has not been modified since the specified time

Returns: Error?

Sample code:

check client->copyObject("my-app-data", "reports/q1-2024.csv", "my-backups", "archive/2024/q1.csv");
doesObjectExist

Checks whether an S3 object exists in a bucket without downloading it. Returns true if it exists, false if not found, or an Error on request or transport failures.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket

Returns: boolean|Error

Sample code:

boolean exists = check client->doesObjectExist("my-app-data", "reports/q1-2024.csv");

Sample response:

true
getObjectMetadata

Retrieves metadata for an S3 object without downloading its content. Equivalent to an HTTP HEAD request.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
versionIdstringNoGet metadata for a specific version of the object (when versioning is enabled)
partNumberintNoThe part number of the file part to get metadata for
ifMatchstringNoReturn the metadata only if its ETag matches the one specified
ifNoneMatchstringNoReturn the metadata only if its ETag differs from the one specified
ifModifiedSincestringNoReturn the metadata only if the object has been modified since the specified time
ifUnmodifiedSincestringNoReturn the metadata only if the object has not been modified since the specified time

Returns: ObjectMetadata|Error

Sample code:

s3:ObjectMetadata metadata = check client->getObjectMetadata("my-app-data", "reports/q1-2024.csv");

Sample response:

{
"key": "reports/q1-2024.csv",
"contentLength": 4096,
"contentType": "text/csv",
"eTag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
"lastModified": "2024-03-31T12:00:00.000Z",
"storageClass": "STANDARD",
"versionId": "3HL4kqtJlcpXrof3vjVBH40Nk3",
"userMetadata": {
"author": "John",
"project": "quarterly-reports"
}
}

Presigned URLs​

createPresignedUrl

Creates a presigned URL for time-limited, credential-free access to an S3 object. Use httpMethod = "GET" to generate a download URL and httpMethod = "PUT" to generate an upload URL. The default validity is 15 minutes.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
expirationMinutesintNoHow long the URL is valid in minutes. Defaults to 15. Maximum is 10080 (7 days)
httpMethodHttpMethodNoThe HTTP method the URL allows: "GET" to download or "PUT" to upload. Defaults to GET
contentTypestringNoThe MIME type of the content (for PUT requests)
contentDispositionstringNoSpecifies presentational information for the object (for GET requests)
responseContentTypestringNoOverride the file type when downloading (for GET requests)
versionIdstringNoGenerate a URL for a specific version of the object (when versioning is enabled)

Returns: string|Error

Sample code:

string url = check client->createPresignedUrl("my-app-data", "reports/q1-2024.csv", expirationMinutes = 60);

Sample response:

"https://my-app-data.s3.us-east-1.amazonaws.com/reports/q1-2024.csv?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIOSFODNN7EXAMPLE%2F20240331%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240331T120000Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=abc123..."

Multipart Uploads​

createMultipartUpload

Initiates a multipart upload session for a large object. Returns an upload ID that must be passed to subsequent uploadPart, completeMultipartUpload, or abortMultipartUpload calls. Each part must be at least 5 MB except the last.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
contentTypestringNoThe MIME type of the content
aclCannedACLNoSpecifies accessibility for this object. Defaults to PRIVATE
storageClassStorageClassNoThe storage class of the object. Defaults to STANDARD
metadatamap<string>NoCustom data to attach to the object
cacheControlstringNoSpecifies caching behavior along the request/reply chain
contentDispositionstringNoSpecifies presentational information for the object
contentEncodingstringNoSpecifies what content encodings have been applied to the object
taggingstringNoTags for the object (e.g., "env=prod&team=finance")
serverSideEncryptionstringNoEncryption type ("AES256" or "aws:kms")

Returns: string|Error

Sample code:

string uploadId = check client->createMultipartUpload("my-app-data", "large-dataset.csv");

Sample response:

"VXBsb2FkIElEIGZvciBteS1vYmplY3Q"
uploadPart

Uploads a single part in an ongoing multipart upload. Returns the ETag of the uploaded part, which must be collected and passed to completeMultipartUpload. Part numbers must be between 1 and 10000.

Supported content types: byte[], string, json, xml, record {}, record {}[], stream<byte[], error?>, and stream<record {}, error?>.

  • record {} requires a .json or .xml object key (or explicit fileFormat)
  • record {}[] and stream<record {}, error?> require a .csv object key
  • stream<byte[], error?> is collected into bytes before uploading

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
uploadIdstringYesThe upload ID returned by createMultipartUpload
partNumberintYesThe part number (1–10000). Parts are assembled in ascending order by part number
contentUploadContentYesThe part content. Accepted types: byte[], string, json, xml, record &#123;&#125;, record &#123;&#125;[], stream&#60;byte[], error?&#62;, stream&#60;record &#123;&#125;, error?&#62;
contentLengthintNoSize of the part in bytes
contentMD5stringNoMD5 hash of the part content for data integrity verification
fileFormatFileFormatNoThe file format to use for serializing record content. Overrides the format inferred from the object key extension

Returns: string|Error

Sample code:

string etag1 = check client->uploadPart("my-app-data", "large-dataset.csv", uploadId, 1, part1Bytes);

Sample response:

"\"a54357aff0632cce46d942af68356b38\""
uploadPartAsStream

Uploads a single part as a byte stream in an ongoing multipart upload. Use this to stream large parts without loading them fully into memory. The contentLength parameter is required.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
uploadIdstringYesThe upload ID returned by createMultipartUpload
partNumberintYesThe part number (1–10000)
contentStreamstream<byte[], error?>YesThe content stream for this part
contentLengthintYesSize of the part in bytes
contentMD5stringNoMD5 hash of the part content for data integrity verification
fileFormatFileFormatNoThe file format to use for serializing record content

Returns: string|Error

Sample code:

stream<byte[], error?> partStream = check io:fileReadBlocksAsStream("/tmp/part1.bin");
string etag1 = check client->uploadPartAsStream("my-app-data", "large-dataset.csv", uploadId, 1, partStream, contentLength = 5242880);

Sample response:

"\"b6d81b360a5672d80c27430f39153e2c\""
completeMultipartUpload

Completes a multipart upload by assembling all previously uploaded parts into the final object. The partNumbers and etags arrays must correspond to each other by index and must include every part uploaded.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
uploadIdstringYesThe upload ID returned by createMultipartUpload
partNumbersint[]YesArray of part numbers in the order they were uploaded
etagsstring[]YesArray of ETags corresponding to each part number

Returns: Error?

Sample code:

check client->completeMultipartUpload("my-app-data", "large-dataset.csv", uploadId, [1, 2, 3], [etag1, etag2, etag3]);
abortMultipartUpload

Aborts an in-progress multipart upload and releases all storage associated with the uploaded parts. Call this if the upload fails or is cancelled to avoid incurring storage charges for partial uploads.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket
objectKeystringYesThe path of the object within the bucket
uploadIdstringYesThe upload ID returned by createMultipartUpload

Returns: Error?

Sample code:

check client->abortMultipartUpload("my-app-data", "large-dataset.csv", uploadId);