Skip to content

Fix #741 Added list objects v2 api support #744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2025

Conversation

Ankk98
Copy link
Contributor

@Ankk98 Ankk98 commented Jun 6, 2025

Fix #741

AWS S3 ListObjectsV2 is the recommended API for listing objects in S3 buckets. It provides better performance and more efficient pagination compared to the original ListObjects API.

Added support for v2 API without integration with list related functionalities, that will be added in the next PR.

Usage

Basic ListObjectsV2 API Call

# Initialize storage connection
storage = Fog::Storage.new(provider: 'AWS', region: 'us-east-1')

# Basic ListObjectsV2 call
response = storage.list_objects_v2("my-bucket")
objects = response.body['Contents']
key_count = response.body['KeyCount']

ListObjectsV2 with Parameters

# List with prefix
response = storage.list_objects_v2("my-bucket", 'prefix' => 'photos/')

# List with max-keys limit
response = storage.list_objects_v2("my-bucket", 'max-keys' => 1000)

# List starting after a specific key
response = storage.list_objects_v2("my-bucket", 'start-after' => 'photos/2023/')

# List with delimiter for directory-like behavior
response = storage.list_objects_v2("my-bucket", 'delimiter' => '/')

# Include owner information
response = storage.list_objects_v2("my-bucket", 'fetch-owner' => true)

# Combine multiple parameters
response = storage.list_objects_v2("my-bucket", {
  'prefix' => 'uploads/',
  'max-keys' => 100,
  'delimiter' => '/',
  'start-after' => 'uploads/2023/'
})

Pagination with Continuation Token

# Get first page
first_page = storage.list_objects_v2("my-bucket", 'max-keys' => 1000)

if first_page.body['IsTruncated']
  # Get next page using continuation token
  next_page = storage.list_objects_v2("my-bucket", {
    'continuation-token' => first_page.body['NextContinuationToken']
  })
end

Complete Pagination Example

def list_all_objects(storage, bucket_name, options = {})
  all_objects = []
  continuation_token = nil
  
  loop do
    request_options = options.dup
    request_options['continuation-token'] = continuation_token if continuation_token
    
    response = storage.list_objects_v2(bucket_name, request_options)
    all_objects.concat(response.body['Contents'] || [])
    
    break unless response.body['IsTruncated']
    continuation_token = response.body['NextContinuationToken']
  end
  
  all_objects
end

# Usage
all_objects = list_all_objects(storage, "my-bucket", {
  'prefix' => 'photos/',
  'max-keys' => 1000
})

Testing

Run the tests:

rake test

Ankk98 added 3 commits May 31, 2025 23:11
- Introduced comprehensive tests for the ListObjectsV2 API, covering basic functionality, parameter handling (prefix, max-keys, start-after, delimiter, fetch-owner), and pagination with continuation tokens.
- Created separate test files for both model and request layers to ensure thorough validation of the API's behavior and response structure.
…ixes in KeyCount calculation

- Modified the response body to ensure that the 'KeyCount' accurately reflects the total number of objects and unique common prefixes.
- Adjusted the handling of 'CommonPrefixes' to use a unique set, improving the integrity of the response data.
Copy link
Member

@geemus geemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! Apologies it took me so long to get to it.

@geemus geemus merged commit a7a4a00 into fog:master Jun 20, 2025
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for ListObjectsV2
2 participants