listObjects method
Lists object keys in the configured bucket.
This uses the S3 ListObjectsV2 API and extracts object keys from the XML response body.
Parameters:
prefix: Optional key prefix to filter results.maxKeys: Maximum number of keys to request. Defaults to1000.
Example:
final keys = await client.listObjects(prefix: 'uploads/');
print(keys);
Implementation
Future<List<String>> listObjects({String? prefix, int maxKeys = 1000}) async {
try {
final queryParams = <String, String>{'list-type': '2', 'max-keys': maxKeys.toString()};
if (prefix != null) {
queryParams['prefix'] = prefix;
}
final response = await _makeRequest(method: 'GET', path: '/', queryParams: queryParams);
if (response.statusCode == 200) {
final xml = utf8.decode(response.body);
final keys = <String>[];
final regex = RegExp(r'<Key>(.*?)</Key>');
for (final match in regex.allMatches(xml)) {
keys.add(match.group(1)!);
}
return keys;
} else {
return [];
}
} catch (e) {
return [];
}
}