Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the list of all versions of the object from S3 present in AWS Resource
In this article, we will see how to get the list of all versions of objects from S3 using boto3 in Python. This is useful when S3 versioning is enabled and you need to track object history.
Prerequisites
Before running this code, ensure you have ?
AWS credentials configured (via AWS CLI, IAM roles, or environment variables)
S3 bucket with versioning enabled
boto3library installed:pip install boto3
Approach
We'll use the list_object_versions() method from the S3 client to retrieve all versions of objects in a specific bucket and prefix ?
Step 1: Import
boto3andbotocorefor exception handlingStep 2: Create an AWS S3 client
Step 3: Use
list_object_versions()with bucket name and prefixStep 4: Handle exceptions and return the results
Example
The following code lists all versions of objects in the testfolder prefix from Bucket_1 ?
import boto3
from botocore.exceptions import ClientError
def list_all_objects_version(bucket_name, prefix_name):
session = boto3.session.Session()
s3_client = session.client('s3')
try:
result = s3_client.list_object_versions(Bucket=bucket_name, Prefix=prefix_name)
return result
except ClientError as e:
raise Exception("boto3 client error in list_all_objects_version function: " + str(e))
except Exception as e:
raise Exception("Unexpected error in list_all_objects_version function: " + str(e))
# List all versions of objects in testfolder
versions = list_all_objects_version("Bucket_1", "testfolder")
print(versions)
Understanding the Output
The response contains several important fields ?
Versions: Array of all object versions with metadata
Key: Object path (e.g., "testfolder/test.zip")
VersionId: Unique version identifier
IsLatest: Boolean indicating if this is the current version
LastModified: Timestamp when the version was created
Size: Object size in bytes
Extracting Specific Version Information
To work with specific versions, you can iterate through the results ?
import boto3
from botocore.exceptions import ClientError
from datetime import datetime
def get_object_versions_info(bucket_name, prefix_name):
session = boto3.session.Session()
s3_client = session.client('s3')
try:
result = s3_client.list_object_versions(Bucket=bucket_name, Prefix=prefix_name)
versions_info = []
for version in result.get('Versions', []):
version_info = {
'Key': version['Key'],
'VersionId': version['VersionId'],
'IsLatest': version['IsLatest'],
'Size': version['Size'],
'LastModified': version['LastModified'].strftime('%Y-%m-%d %H:%M:%S')
}
versions_info.append(version_info)
return versions_info
except ClientError as e:
print(f"Error: {e}")
return []
# Example usage
versions = get_object_versions_info("my-bucket", "documents/")
for version in versions[:3]: # Show first 3 versions
print(f"Key: {version['Key']}")
print(f"Version ID: {version['VersionId']}")
print(f"Is Latest: {version['IsLatest']}")
print(f"Size: {version['Size']} bytes")
print(f"Last Modified: {version['LastModified']}")
print("-" * 40)
Key: documents/report.pdf Version ID: 3/L4kqtJlcpXroDVBH40Nr8X8gdRQBpUMLUo Is Latest: True Size: 245760 Last Modified: 2024-01-15 10:30:25 ---------------------------------------- Key: documents/report.pdf Version ID: 47DEQpj8HBSa.OVBj0HQSQ2E Is Latest: False Size: 198432 Last Modified: 2024-01-10 14:22:18 ---------------------------------------- Key: documents/data.csv Version ID: null Is Latest: True Size: 15840 Last Modified: 2024-01-12 09:15:33 ----------------------------------------
Key Parameters
| Parameter | Description | Required |
|---|---|---|
Bucket |
S3 bucket name | Yes |
Prefix |
Filter objects by prefix (folder path) | No |
MaxKeys |
Maximum number of versions to return (default: 1000) | No |
Conclusion
Use list_object_versions() to retrieve all versions of S3 objects when versioning is enabled. This method returns comprehensive metadata including version IDs, modification dates, and size information for tracking object history.
