Skip to content

Data Source: awsS3BucketObjects

\~> NOTE: The awsS3BucketObjects data source is DEPRECATED and will be removed in a future version! Use awsS3Objects instead, where new features and fixes will be added.

\~> NOTE on maxKeys: Retrieving very large numbers of keys can adversely affect Terraform's performance.

The objects data source returns keys (i.e., file names) and other metadata about objects in an S3 bucket.

Example Usage

The following example retrieves a list of all object keys in an S3 bucket and creates corresponding Terraform object data sources:

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
const dataAwsS3BucketObjectsMyObjects =
  new aws.dataAwsS3BucketObjects.DataAwsS3BucketObjects(this, "my_objects", {
    bucket: "ourcorp",
  });
const dataAwsS3ObjectObjectInfo = new aws.dataAwsS3Object.DataAwsS3Object(
  this,
  "object_info",
  {
    bucket: dataAwsS3BucketObjectsMyObjects.id,
    key: `\${element(${dataAwsS3BucketObjectsMyObjects.keys}, count.index)}`,
  }
);
/*In most cases loops should be handled in the programming language context and 
not inside of the Terraform context. If you are looping over something external, e.g. a variable or a file input
you should consider using a for loop. If you are looping over something only known to Terraform, e.g. a result of a data source
you need to keep this like it is.*/
dataAwsS3ObjectObjectInfo.addOverride(
  "count",
  `\${length(${dataAwsS3BucketObjectsMyObjects.keys})}`
);

Argument Reference

The following arguments are supported:

  • bucket - (Required) Lists object keys in this S3 bucket. Alternatively, an S3 access point ARN can be specified
  • prefix - (Optional) Limits results to object keys with this prefix (Default: none)
  • delimiter - (Optional) Character used to group keys (Default: none)
  • encodingType - (Optional) Encodes keys using this method (Default: none; besides none, only "url" can be used)
  • maxKeys - (Optional) Maximum object keys to return (Default: 1000)
  • startAfter - (Optional) Returns key names lexicographically after a specific object key in your bucket (Default: none; S3 lists object keys in UTF-8 character encoding in lexicographical order)
  • fetchOwner - (Optional) Boolean specifying whether to populate the owner list (Default: false)

Attributes Reference

In addition to all arguments above, the following attributes are exported:

  • keys - List of strings representing object keys
  • commonPrefixes - List of any keys between prefix and the next occurrence of delimiter (i.e., similar to subdirectories of the prefix "directory"); the list is only returned when you specify delimiter
  • id - S3 Bucket.
  • owners - List of strings representing object owner IDs (see fetchOwner above)