Skip to main content

Actions

The ballerinax/aws.s3 package exposes the following clients:

ClientPurpose
ClientProvides operations to manage S3 buckets and objects via the Amazon S3 REST API.

Client

Provides operations to manage S3 buckets and objects via the Amazon S3 REST API.

Configuration

FieldTypeDefaultDescription
accessKeyIdstringRequiredAccess key ID of the AWS account.
secretAccessKeystringRequiredSecret access key of the AWS account.
regionstring"us-east-1"AWS region for S3 operations (e.g., us-east-1, eu-west-1).
httpVersionhttp:HttpVersionHTTP_1_1HTTP protocol version.
http1SettingsClientHttp1Settings()HTTP/1.x protocol settings (keep-alive, chunking, proxy).
http2Settingshttp:ClientHttp2Settings()HTTP/2 protocol settings.
timeoutdecimal60Connection timeout in seconds.
forwardedstring"disable"forwarded / x-forwarded header behaviour.
poolConfighttp:PoolConfiguration()Connection-pool settings.
cachehttp:CacheConfig()Response caching settings.
compressionhttp:CompressionCOMPRESSION_AUTOaccept-encoding handling.
circuitBreakerhttp:CircuitBreakerConfig()Circuit-breaker settings.
retryConfighttp:RetryConfig()Retry settings for failed requests.
responseLimitshttp:ResponseLimitConfigs()Inbound response size limits.
secureSockethttp:ClientSecureSocket()SSL/TLS configuration.
proxyhttp:ProxyConfig()Proxy server settings.
validationbooleantrueEnables inbound payload validation (constraint package).

http:* types come from ballerina/http; ClientHttp1Settings comes from ballerinax/client.config.

Initializing the client

import ballerinax/aws.s3;

configurable string accessKeyId = ?;
configurable string secretAccessKey = ?;
configurable string region = ?;

s3:Client s3Client = check new ({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: region
});

Operations

Bucket operations

listBuckets

Retrieves a list of all S3 buckets owned by the authenticated user.

Returns: Bucket[]|error

Sample code:

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

Sample response:

[{"name": "my-app-data", "creationDate": "2024-01-15T10:30:00.000Z"}, {"name": "my-logs-bucket", "creationDate": "2024-03-22T08:15:00.000Z"}]
createBucket

Creates a new S3 bucket with the specified name and optional canned ACL.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesA globally unique name for the new bucket.
cannedACLCannedACL?NoThe canned access control list for the bucket (e.g., ACL_PRIVATE, ACL_PUBLIC_READ).

Returns: error?

Sample code:

check s3Client->createBucket("my-new-bucket");
deleteBucket

Deletes the specified S3 bucket. The bucket must be empty before deletion.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket to delete.

Returns: error?

Sample code:

check s3Client->deleteBucket("my-old-bucket");

Object operations

listObjects

Retrieves a list of objects in the specified bucket with optional filtering and pagination.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket.
delimiterstring?NoA character used to group keys (e.g., / for directory-like listing).
encodingTypestring?NoThe encoding method for the response.
maxKeysint?NoMaximum number of keys to return.
prefixstring?NoLimits results to keys beginning with this prefix.
startAfterstring?NoObject key from which to begin listing.
fetchOwnerboolean?NoSet to true to include owner information in the response.
continuationTokenstring?NoToken for paginating through truncated results.

Returns: S3Object[]|error

Sample code:

s3:S3Object[] objects = check s3Client->listObjects("my-bucket", prefix = "documents/");

Sample response:

[{"objectName": "documents/report.pdf", "lastModified": "2024-06-01T12:00:00.000Z", "eTag": "\"d41d8cd98f00b204e9800998ecf8427e\"", "objectSize": "1048576", "storageClass": "STANDARD"}]
createObject

Creates (uploads) a new object in the specified bucket.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket.
objectNamestringYesThe key (name) for the new object.
payloadstring|xml|json|byte[]|stream<io:Block, io:Error?>YesThe content to upload.
cannedACLCannedACL?NoCanned ACL for the object.
objectCreationHeadersObjectCreationHeaders?NoOptional headers such as contentType, cacheControl, contentEncoding.
userMetadataHeadersmap<string>NoUser-defined metadata key-value pairs to attach to the object.

Returns: error?

Sample code:

check s3Client->createObject("my-bucket", "documents/greeting.txt", "Hello, World!");
getObject

Retrieves an object from the specified bucket as a byte stream.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket.
objectNamestringYesThe key of the object to retrieve.
objectRetrievalHeadersObjectRetrievalHeaders?NoOptional conditional headers (e.g., modifiedSince, ifMatch, range).
byteArraySizeint?NoSize of each byte array chunk in the stream. Default is 8KB.

Returns: stream<byte[], io:Error?>|error

Sample code:

stream<byte[], io:Error?> objectStream = check s3Client->getObject("my-bucket", "documents/greeting.txt");
byte[] content = [];
check from byte[] chunk in objectStream
do {
content.push(...chunk);
};

Resulting content after consuming the stream:

[72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
deleteObject

Deletes an object from the specified bucket.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket.
objectNamestringYesThe key of the object to delete.
versionIdstring?NoThe version ID of the object to delete (for versioned buckets).

Returns: error?

Sample code:

check s3Client->deleteObject("my-bucket", "documents/greeting.txt");

Presigned URLs

createPresignedUrl

Generates a presigned URL for time-limited access to an S3 object without requiring credentials.

Parameters:

NameTypeRequiredDescription
bucketNamestringYesThe name of the bucket.
objectNamestringYesThe key of the object.
actionObjectAction|ObjectCreationHeaders|ObjectRetrievalHeadersYesThe action: RETRIEVE for download, CREATE for upload, or the relevant headers record.
expiresintNoValidity period in seconds. Default is 1800 (30 minutes).
partNoint?NoPart number for multipart uploads.
uploadIdstring?NoUpload ID for multipart uploads.

Returns: string|error

Sample code:

string presignedUrl = check s3Client->createPresignedUrl("my-bucket", "documents/report.pdf", s3:RETRIEVE, 3600);

Sample response:

"https://s3.amazonaws.com/my-bucket/documents/report.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA.../20240601/us-east-1/s3/aws4_request&X-Amz-Date=20240601T120000Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=abc123..."

Note: us-east-1 uses the global endpoint s3.amazonaws.com. For other regions, the host is s3.<region>.amazonaws.com.

Multipart upload

createMultipartUpload

Initiates a multipart upload and returns an upload ID for subsequent part uploads.

Parameters:

NameTypeRequiredDescription
objectNamestringYesThe key of the object.
bucketNamestringYesThe name of the bucket.
cannedACLCannedACL?NoCanned ACL for the object.
multipartUploadHeadersMultipartUploadHeaders?NoOptional headers such as contentType, cacheControl.

Returns: string|error

Sample code:

string uploadId = check s3Client->createMultipartUpload("large-file.zip", "my-bucket");

Sample response:

"VXBsb2FkSWQtZXhhbXBsZQ"
uploadPart

Uploads a single part of a multipart upload. Returns a CompletedPart record with the part number and ETag.

Parameters:

NameTypeRequiredDescription
objectNamestringYesThe key of the object.
bucketNamestringYesThe name of the bucket.
payloadstring|xml|json|byte[]|stream&lt;io:Block, io:Error?&gt;YesThe part content.
uploadIdstringYesThe upload ID from createMultipartUpload.
partNumberintYesThe sequential part number (starting from 1).
uploadPartHeadersUploadPartHeaders?NoOptional headers such as contentLength, contentMD5.

Returns: CompletedPart|error

Sample code:

s3:CompletedPart part1 = check s3Client->uploadPart("large-file.zip", "my-bucket", partData1, uploadId, 1);

Sample response:

{"partNumber": 1, "ETag": "\"a54357aff0632cce46d942af68356b38\""}
completeMultipartUpload

Completes a multipart upload by assembling all previously uploaded parts.

Parameters:

NameTypeRequiredDescription
objectNamestringYesThe key of the object.
bucketNamestringYesThe name of the bucket.
uploadIdstringYesThe upload ID from createMultipartUpload.
completedPartsCompletedPart[]YesArray of CompletedPart records (part number and ETag) for each uploaded part.

Returns: error?

Sample code:

check s3Client->completeMultipartUpload("large-file.zip", "my-bucket", uploadId, [part1, part2, part3]);
abortMultipartUpload

Aborts an in-progress multipart upload, freeing any uploaded parts.

Parameters:

NameTypeRequiredDescription
objectNamestringYesThe key of the object.
bucketNamestringYesThe name of the bucket.
uploadIdstringYesThe upload ID to abort.

Returns: error?

Sample code:

check s3Client->abortMultipartUpload("large-file.zip", "my-bucket", uploadId);