What exactly is a key in S3?

Hi, this is Charu from Games Solution Department, Classmethod.

I was trying to put object in S3 bucket, that's when I met with the 'Key' attribute in S3. But as I was new to this, I was not sure what value should I provide to 'Key'. I thought there might be others equally confused as I was, so here I am presenting this short blog to explain what exactly is a Key?

What is a Key?

Amazon S3 is meant for storing data in the form of objects inside a bucket. Any object stored in S3 has a unique identity in the form of URI, as shown below-

s3://bucket/folder/file.txt

Here, a file (file.txt) is stored inside the organised container(folder) in the bucket(bucket).

You can view the key in your S3 console inside the object details page as shown below-

Now, while defining the URI, it can be broken down into two sections:

  • The Bucket Name
  • The Key
  • The bucket name refers to the object’s name in which the object is stored, while the key represents the FULL path of the object INSIDE the bucket.

    But the real question is, how to define the key while working through code. Don't worry, I'll explain this to you through my code snippet.

    Example- My Code Snippet

    Here is my short code snippet where I am trying to put the 'value' in the bucket. A new object with a new key will be created every time the lambda function is invoked.

    import os
    from zoneinfo import ZoneInfo
    from datetime import datetime, timezone, timedelta
    import time
    
    LOG_BUCKET_NAME = os.environ['LOG_BUCKET_NAME']
    
    jst = ZoneInfo('Asia/Tokyo')
    current_jst_time = datetime.now(jst)
    current_unix_time = int(current_jst_time.timestamp())
    
    value = log_data.get('rows', [])
    s3 = boto3.client('s3')
    
    log_data_key = f'data-/{current_jst_time.year}/{current_jst_time.month:02d}/{current_jst_time.day:02d}/{current_jst_time.hour:02d}/{current_unix_time}'
    
    s3.put_object(
        Bucket=LOG_BUCKET_NAME,
        Key=log_data_key,
        Body=json.dumps(value))

    As you can see here, 'log_data_key' is my key variable. I have assigned year, month, day, hour and current unix time at the end. The path is long but it will keep the objects storage classified, unique and easy to understand.

    Coming back to Key explanation-

    In a code, a key is the object reference to the unique identifier within a bucket.

    Think of buckets as a table in a database and think of keys as the rows in the table you are referring(better known as an object) in the bucket.

    A key is the unique identifier for an object within a bucket. Every object in a bucket has exactly one key. It is just a string - the "path and filename" of the object in the bucket, without a leading /.

    Conclusion

    In this short blog, we discussed about the keys- what are the keys and how to assign it. Finally, I will explain it with a simple example; as in real life, we all have different unique addresses, in the same way keys are also the uniques addresses of every object.

    I hope you got a clear idea about the Keys in S3.

    Thank you for reading!

    Happy Learning :)