Search code examples
boto3

is there any difference between the key and object_key in a S3.Object() in boto3?


The documentation for S3.Object and S3.ObjectVersion mention key(identifier) and object_key (attribute). But I don't see any difference:

import boto3

all_objects = boto3.resource('s3').Bucket('mybucket').object_versions.all()
for obj in all_objects:
  print(obj.key, obj.object_key)
  print(obj.id, obj.version_id)

When should I use one or the other?


Solution

  • The safe option when reading it is to use key because all ObjectSummary, Object and ObjectVersion have key but ObjectSummary does not have object_key.

    So if you want to use consistent syntax without having to worry about the exact type returned by the boto3 method, you can use .key and it will work for both mybucket.object_versions.all() and mybucket.objects.all() because both have .key