method
S3Client.list
Returns some or all (up to 1,000) of the objects in a bucket with each request.
Use the request parameters as selection criteria to return a subset of the objects in a bucket.
Options for listing objects in the bucket
Additional S3 options to override defaults
A promise that resolves to the list response
// List (up to) 1000 objects in the bucket
const allObjects = await bucket.list();
// List (up to) 500 objects under `uploads/` prefix, with owner field for each object
const uploads = await bucket.list({
prefix: 'uploads/',
maxKeys: 500,
fetchOwner: true,
});
// Check if more results are available
if (uploads.isTruncated) {
// List next batch of objects under `uploads/` prefix
const moreUploads = await bucket.list({
prefix: 'uploads/',
maxKeys: 500,
startAfter: uploads.contents!.at(-1).key,
fetchOwner: true,
});
}Referenced types
interface S3ListObjectsOptions
- continuationToken?: string
Indicates that the listing is being continued on this bucket with a token from a previous response. The token is obfuscated and is not a real key. Use it to paginate the list results.
- encodingType?: 'url'
Encoding type used by S3 to encode the object keys in the response. Responses are encoded only in UTF-8. An object key can contain any Unicode character, but the XML 1.0 parser can't parse some characters, such as those with an ASCII value from 0 to 10. For those keys, set this parameter to ask S3 to encode the keys in the response.
- maxKeys?: number
The maximum number of keys returned in the response. By default, the action returns up to 1,000 key names. The response might contain fewer keys but never contains more.
interface S3Options
Configuration options for S3 operations
- accessKeyId?: string
The access key ID for authentication. Defaults to
S3_ACCESS_KEY_IDorAWS_ACCESS_KEY_IDenvironment variables. - acl?: 'private' | 'public-read' | 'public-read-write' | 'aws-exec-read' | 'authenticated-read' | 'bucket-owner-read' | 'bucket-owner-full-control' | 'log-delivery-write'
The Access Control List (ACL) policy for the file. Controls who can access the file and what permissions they have.
// Setting public read access const file = s3.file("public-file.txt", { acl: "public-read", bucket: "my-bucket" }); - bucket?: string
The S3 bucket name. Defaults to
S3_BUCKETorAWS_BUCKETenvironment variables.// Using explicit bucket const file = s3.file("my-file.txt", { bucket: "my-bucket" }); - contentDisposition?: string
The Content-Disposition header value. Controls how the file is presented when downloaded.
// Setting attachment disposition with filename const file = s3.file("report.pdf", { contentDisposition: "attachment; filename=\"quarterly-report.pdf\"" }); - contentEncoding?: string
The Content-Encoding header value. Specifies what content encodings have been applied to the object, for example to indicate that it has been compressed.
// Setting gzip encoding const file = s3.file("data.json.gz", { contentEncoding: "gzip" }); - endpoint?: string
The S3-compatible service endpoint URL. Defaults to
S3_ENDPOINTorAWS_ENDPOINTenvironment variables.// AWS S3 const file = s3.file("my-file.txt", { endpoint: "https://s3.us-east-1.amazonaws.com" }); - partSize?: number
The size of each part in multipart uploads (in bytes).
- Minimum: 5 MiB
- Maximum: 5120 MiB
- Default: 5 MiB
// Configuring multipart uploads const file = s3.file("large-file.dat", { partSize: 10 * 1024 * 1024, // 10 MiB parts queueSize: 4 // Upload 4 parts in parallel }); const writer = file.writer(); // ... write large file in chunks - queueSize?: number
Number of parts to upload in parallel for multipart uploads.
- Default: 5
- Maximum: 255
Increasing this value can improve upload speeds for large files but uses more memory.
- region?: string
The AWS region. Defaults to
S3_REGIONorAWS_REGIONenvironment variables.const file = s3.file("my-file.txt", { bucket: "my-bucket", region: "us-west-2" }); - requestPayer?: boolean
When set to
true, confirms that the requester knows they will be charged for the request and data transfer costs. Required for accessing objects in Requester Pays buckets.// Accessing a file in a Requester Pays bucket const file = s3.file("data.csv", { bucket: "requester-pays-bucket", requestPayer: true }); const content = await file.text(); - retry?: number
Number of retry attempts for failed uploads.
- Default: 3
- Maximum: 255
// Setting retry attempts const file = s3.file("my-file.txt", { retry: 5 // Retry failed uploads up to 5 times }); - secretAccessKey?: string
The secret access key for authentication. Defaults to
S3_SECRET_ACCESS_KEYorAWS_SECRET_ACCESS_KEYenvironment variables. - sessionToken?: string
Optional session token for temporary credentials. Defaults to
S3_SESSION_TOKENorAWS_SESSION_TOKENenvironment variables.// Using temporary credentials const file = s3.file("my-file.txt", { accessKeyId: tempAccessKey, secretAccessKey: tempSecretKey, sessionToken: tempSessionToken }); - storageClass?: 'STANDARD' | 'DEEP_ARCHIVE' | 'EXPRESS_ONEZONE' | 'GLACIER' | 'GLACIER_IR' | 'INTELLIGENT_TIERING' | 'ONEZONE_IA' | 'OUTPOSTS' | 'REDUCED_REDUNDANCY' | 'SNOW' | 'STANDARD_IA'
The storage class for the object. By default, Amazon S3 stores newly created objects in the
STANDARDstorage class.// Setting an explicit storage class const file = s3.file("my-file.json", { storageClass: "STANDARD_IA" }); - type?: string
The Content-Type of the file. Automatically set based on file extension when possible.
// Setting explicit content type const file = s3.file("data.bin", { type: "application/octet-stream" }); - virtualHostedStyle?: boolean
Use a virtual hosted-style endpoint, where the bucket name is part of the hostname. Defaults to
false. Whentrue, ifendpointis provided, thebucketoption is ignored.// Using virtual hosted style const file = s3.file("my-file.txt", { virtualHostedStyle: true, endpoint: "https://my-bucket.s3.us-east-1.amazonaws.com" });
interface S3ListObjectsResponse
- commonPrefixes?: { prefix: string }[]
Keys that share the same prefix, grouped together (up to 1,000). When counting the total number of returns by this API operation, each group counts as one item.
A response can contain CommonPrefixes only if you specify a delimiter.
CommonPrefixes contains all (if there are any) keys between Prefix and the next occurrence of the string specified by a delimiter. These keys act like subdirectories in the directory specified by Prefix.
For example, if the prefix is notes/ and the delimiter is a slash (/) as in notes/summer/july, the common prefix is notes/summer/. All of the keys that roll up into a common prefix count as a single return when calculating the number of returns.
- contents?: { checksumAlgorithm: 'CRC32' | 'CRC32C' | 'SHA1' | 'SHA256' | 'CRC64NVME'; checksumType: 'COMPOSITE' | 'FULL_OBJECT'; eTag: string; key: string; lastModified: string; owner: { displayName: string; id: string }; restoreStatus: { isRestoreInProgress: boolean; restoreExpiryDate: string }; size: number; storageClass: 'STANDARD' | 'DEEP_ARCHIVE' | 'EXPRESS_ONEZONE' | 'GLACIER' | 'GLACIER_IR' | 'INTELLIGENT_TIERING' | 'ONEZONE_IA' | 'OUTPOSTS' | 'REDUCED_REDUNDANCY' | 'SNOW' | 'STANDARD_IA' }[]
Metadata about each object returned.
- continuationToken?: string
If ContinuationToken was sent with the request, it is included in the response. You can use the returned ContinuationToken for pagination of the list response.
- delimiter?: string
Causes keys that contain the same string between the prefix and the first occurrence of the delimiter to be rolled up into a single result element in the CommonPrefixes collection. These rolled-up keys are not returned elsewhere in the response. Each rolled-up result counts as only one return against the MaxKeys value.
- isTruncated?: boolean
Set to false if all of the results were returned. Set to true if more keys are available to return. If the number of results exceeds that specified by MaxKeys, all of the results might not be returned.
- keyCount?: number
The number of keys returned with this request. KeyCount is always less than or equal to the MaxKeys field.
- maxKeys?: number
The maximum number of keys returned in the response. By default, the action returns up to 1,000 key names. The response might contain fewer keys but never contains more.
- nextContinuationToken?: string
NextContinuationToken is sent when isTruncated is true, which means there are more keys in the bucket that can be listed. Continue the next list request to S3 with this NextContinuationToken. NextContinuationToken is obfuscated and is not a real key.