Method: Aws::S3::Client#list_objects_v2

Defined in:
lib/aws-sdk-s3/client.rb

#list_objects_v2(params = {}) ⇒ Types::ListObjectsV2Output

Returns some or all (up to 1,000) of the objects in a bucket with each request. You can use the request parameters as selection criteria to return a subset of the objects in a bucket. A ‘200 OK` response can contain valid or invalid XML. Make sure to design your application to parse the contents of the response and handle it appropriately. For more information about listing objects, see [Listing object keys programmatically] in the *Amazon S3 User Guide*. To get a list of your buckets, see [ListBuckets].

<note markdown=“1”> **Directory buckets** - For directory buckets, you must make requests for this API operation to the Zonal endpoint. These endpoints support virtual-hosted-style requests in the format ‘bucket_name.s3express-az_id.region.amazonaws.com/key-name `. Path-style requests are not supported. For more information, see

Regional and Zonal endpoints][3

in the *Amazon S3 User Guide*.

</note>

Permissions : * **General purpose bucket permissions** - To use this operation,

  you must have READ access to the bucket. You must have permission
  to perform the `s3:ListBucket` action. The bucket owner has this
  permission by default and can grant this permission to others. For
  more information about permissions, see [Permissions Related to
  Bucket Subresource Operations][4] and [Managing Access Permissions
  to Your Amazon S3 Resources][5] in the *Amazon S3 User Guide*.

* **Directory bucket permissions** - To grant access to this API
  operation on a directory bucket, we recommend that you use the [
  `CreateSession` ][6] API operation for session-based
  authorization. Specifically, you grant the
  `s3express:CreateSession` permission to the directory bucket in a
  bucket policy or an IAM identity-based policy. Then, you make the
  `CreateSession` API call on the bucket to obtain a session token.
  With the session token in your request header, you can make API
  requests to this operation. After the session token expires, you
  make another `CreateSession` API call to generate a new session
  token for use. Amazon Web Services CLI or SDKs create session and
  refresh the session token automatically to avoid service
  interruptions when a session expires. For more information about
  authorization, see [ `CreateSession` ][6].

Sorting order of returned objects : * **General purpose bucket** - For general purpose buckets,

  `ListObjectsV2` returns objects in lexicographical order based on
  their key names.

* **Directory bucket** - For directory buckets, `ListObjectsV2` does
  not return objects in lexicographical order.

HTTP Host header syntax

: Directory buckets - The HTTP Host header syntax is ‘

Bucket_name.s3express-az_id.region.amazonaws.com`.

This section describes the latest revision of this action. We recommend that you use this revised API operation for application development. For backward compatibility, Amazon S3 continues to support the prior version of this API operation, [ListObjects].

The following operations are related to ‘ListObjectsV2`:

  • GetObject][8
  • PutObject][9
  • CreateBucket][10

[1]: docs.aws.amazon.com/AmazonS3/latest/userguide/ListingKeysUsingAPIs.html [2]: docs.aws.amazon.com/AmazonS3/latest/API/API_ListBuckets.html [3]: docs.aws.amazon.com/AmazonS3/latest/userguide/s3-express-Regions-and-Zones.html [4]: docs.aws.amazon.com/AmazonS3/latest/userguide/using-with-s3-actions.html#using-with-s3-actions-related-to-bucket-subresources [5]: docs.aws.amazon.com/AmazonS3/latest/userguide/s3-access-control.html [6]: docs.aws.amazon.com/AmazonS3/latest/API/API_CreateSession.html [7]: docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjects.html [8]: docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html [9]: docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html [10]: docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html

The returned response is a pageable response and is Enumerable. For details on usage see PageableResponse.

Examples:

Example: To get object list


# The following example retrieves object list. The request specifies max keys to limit response to include only 2 object
# keys. 

resp = client.list_objects_v2({
  bucket: "DOC-EXAMPLE-BUCKET", 
  max_keys: 2, 
})

resp.to_h outputs the following:
{
  contents: [
    {
      etag: "\"70ee1738b6b21e2c8a43f3a5ab0eee71\"", 
      key: "happyface.jpg", 
      last_modified: Time.parse("2014-11-21T19:40:05.000Z"), 
      size: 11, 
      storage_class: "STANDARD", 
    }, 
    {
      etag: "\"becf17f89c30367a9a44495d62ed521a-1\"", 
      key: "test.jpg", 
      last_modified: Time.parse("2014-05-02T04:51:50.000Z"), 
      size: 4192256, 
      storage_class: "STANDARD", 
    }, 
  ], 
  is_truncated: true, 
  key_count: 2, 
  max_keys: 2, 
  name: "DOC-EXAMPLE-BUCKET", 
  next_continuation_token: "1w41l63U0xa8q7smH50vCxyTQqdxo69O3EmK28Bi5PcROI4wI/EyIJg==", 
  prefix: "", 
}

Request syntax with placeholder values


resp = client.list_objects_v2({
  bucket: "BucketName", # required
  delimiter: "Delimiter",
  encoding_type: "url", # accepts url
  max_keys: 1,
  prefix: "Prefix",
  continuation_token: "Token",
  fetch_owner: false,
  start_after: "StartAfter",
  request_payer: "requester", # accepts requester
  expected_bucket_owner: "AccountId",
  optional_object_attributes: ["RestoreStatus"], # accepts RestoreStatus
})

Response structure


resp.is_truncated #=> Boolean
resp.contents #=> Array
resp.contents[0].key #=> String
resp.contents[0].last_modified #=> Time
resp.contents[0].etag #=> String
resp.contents[0].checksum_algorithm #=> Array
resp.contents[0].checksum_algorithm[0] #=> String, one of "CRC32", "CRC32C", "SHA1", "SHA256"
resp.contents[0].size #=> Integer
resp.contents[0].storage_class #=> String, one of "STANDARD", "REDUCED_REDUNDANCY", "GLACIER", "STANDARD_IA", "ONEZONE_IA", "INTELLIGENT_TIERING", "DEEP_ARCHIVE", "OUTPOSTS", "GLACIER_IR", "SNOW", "EXPRESS_ONEZONE"
resp.contents[0].owner.display_name #=> String
resp.contents[0].owner.id #=> String
resp.contents[0].restore_status.is_restore_in_progress #=> Boolean
resp.contents[0].restore_status.restore_expiry_date #=> Time
resp.name #=> String
resp.prefix #=> String
resp.delimiter #=> String
resp.max_keys #=> Integer
resp.common_prefixes #=> Array
resp.common_prefixes[0].prefix #=> String
resp.encoding_type #=> String, one of "url"
resp.key_count #=> Integer
resp.continuation_token #=> String
resp.next_continuation_token #=> String
resp.start_after #=> String
resp.request_charged #=> String, one of "requester"

Parameters:

  • params (Hash) (defaults to: {})

    ({})

Options Hash (params):

  • :bucket (required, String)

    **Directory buckets** - When you use this operation with a directory bucket, you must use virtual-hosted-style requests in the format ‘ Bucket_name.s3express-az_id.region.amazonaws.com`. Path-style requests are not supported. Directory bucket names must be unique in the chosen Availability Zone. Bucket names must follow the format ` bucket_base_name–az-id–x-s3` (for example, ` DOC-EXAMPLE-BUCKET–usw2-az1–x-s3`). For information about bucket naming restrictions, see [Directory bucket naming rules] in the *Amazon S3 User Guide*.

    **Access points** - When you use this action with an access point, you must provide the alias of the access point in place of the bucket name or specify the access point ARN. When using the access point ARN, you must direct requests to the access point hostname. The access point hostname takes the form AccessPointName-AccountId.s3-accesspoint.Region.amazonaws.com. When using this action with an access point through the Amazon Web Services SDKs, you provide the access point ARN in place of the bucket name. For more information about access point ARNs, see [Using access points] in the *Amazon S3 User Guide*.

    <note markdown=“1”> Access points and Object Lambda access points are not supported by directory buckets.

    </note>
    

    **S3 on Outposts** - When you use this action with Amazon S3 on Outposts, you must direct requests to the S3 on Outposts hostname. The S3 on Outposts hostname takes the form ‘ AccessPointName-AccountId.outpostID.s3-outposts.Region.amazonaws.com`. When you use this action with S3 on Outposts through the Amazon Web Services SDKs, you provide the Outposts access point ARN in place of the bucket name. For more information about S3 on Outposts ARNs, see

    What is S3 on Outposts?][3

    in the *Amazon S3 User Guide*.

    [1]: docs.aws.amazon.com/AmazonS3/latest/userguide/directory-bucket-naming-rules.html [2]: docs.aws.amazon.com/AmazonS3/latest/userguide/using-access-points.html [3]: docs.aws.amazon.com/AmazonS3/latest/userguide/S3onOutposts.html

  • :delimiter (String)

    A delimiter is a character that you use to group keys.

    <note markdown=“1”> * **Directory buckets** - For directory buckets, ‘/` is the only

    supported delimiter.
    
    • Directory buckets - When you query ‘ListObjectsV2` with a delimiter during in-progress multipart uploads, the `CommonPrefixes` response parameter contains the prefixes that are associated with the in-progress multipart uploads. For more information about multipart uploads, see [Multipart Upload Overview] in the *Amazon S3 User Guide*.

    </note>
    

    [1]: docs.aws.amazon.com/AmazonS3/latest/dev/mpuoverview.html

  • :encoding_type (String)

    Encoding type used by Amazon S3 to encode object keys in the response. If using ‘url`, non-ASCII characters used in an object’s key name will be URL encoded. For example, the object test_file(3).png will appear as test_file%283%29.png.

  • :max_keys (Integer)

    Sets 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 will never contain more.

  • :prefix (String)

    Limits the response to keys that begin with the specified prefix.

    <note markdown=“1”> **Directory buckets** - For directory buckets, only prefixes that end in a delimiter (‘/`) are supported.

    </note>
    
  • :continuation_token (String)

    ‘ContinuationToken` indicates to Amazon S3 that the list is being continued on this bucket with a token. `ContinuationToken` is obfuscated and is not a real key. You can use this `ContinuationToken` for pagination of the list results.

  • :fetch_owner (Boolean)

    The owner field is not present in ‘ListObjectsV2` by default. If you want to return the owner field with each key in the result, then set the `FetchOwner` field to `true`.

    <note markdown=“1”> **Directory buckets** - For directory buckets, the bucket owner is returned as the object owner for all objects.

    </note>
    
  • :start_after (String)

    StartAfter is where you want Amazon S3 to start listing from. Amazon S3 starts listing after this specified key. StartAfter can be any key in the bucket.

    <note markdown=“1”> This functionality is not supported for directory buckets.

    </note>
    
  • :request_payer (String)

    Confirms that the requester knows that she or he will be charged for the list objects request in V2 style. Bucket owners need not specify this parameter in their requests.

    <note markdown=“1”> This functionality is not supported for directory buckets.

    </note>
    
  • :expected_bucket_owner (String)

    The account ID of the expected bucket owner. If the account ID that you provide does not match the actual owner of the bucket, the request fails with the HTTP status code ‘403 Forbidden` (access denied).

  • :optional_object_attributes (Array<String>)

    Specifies the optional fields that you want returned in the response. Fields that you do not specify are not returned.

    <note markdown=“1”> This functionality is not supported for directory buckets.

    </note>
    

Returns:

See Also:



11354
11355
11356
11357
# File 'lib/aws-sdk-s3/client.rb', line 11354

def list_objects_v2(params = {}, options = {})
  req = build_request(:list_objects_v2, params)
  req.send_request(options)
end