[PYTHON] To make sure that the specified key is in the specified bucket in Boto 3

In Boto 3, there is no method equivalent to ʻexists ()that confirms the existence of the key until the time of Boto 2. However, a similar function can be achieved by usinglist_objects ()` as shown below.

exists.py


from boto3 import Session
s3client = Session().client('s3')

def exists(bucket: str, key: str) -> bool:
    """
Does the specified key exist in the specified bucket?

    :param bucket: (str) bucket name
    :param key: (str) key
    :return: (bool)
    """
    contents = s3client.list_objects(Prefix=key, Bucket=bucket).get("Contents")
    if contents:
        for content in contents:
            if content.get("Key") == key:
                return True
    return False

Commentary

list_objects () returns a hash of the form:

{
    'IsTruncated': True|False,  #Was the result shredded? True if done
    'Marker': 'string',
    'NextMarker': 'string',
    'Contents': [
        {
            'Key': 'string',
            'LastModified': datetime(2015, 1, 1),
            'ETag': 'string',
            'Size': 123,
            'StorageClass': 'STANDARD'|'REDUCED_REDUNDANCY'|'GLACIER',
            'Owner': {
                'DisplayName': 'string',
                'ID': 'string'
            }
        },
    ],
    'Name': 'string',
    'Prefix': 'string',
<Omitted>
}

'Contents' is the key, and this is the array of corresponding keys. If there is no corresponding key, 'Contents' will not be included in the return value. Therefore, check if 'Contents' is included, and if the value of'Key' in the dictionary in the list matches the key given as an argument, it corresponds to ʻexists ()`. You can reproduce the function.

Full path of key to Prefix? It feels a little strange to specify, but I understand that Prefix is such a thing.

2017/06/29 postscript

In the following cases, it did not work properly, so I fixed it (thx, @masahiro_toriumi)

Prefix only specifies the prefix of the key, so for example, if you call list_object with Prefix "MyBucket / aaa.txt", it will also be caught in "MyBucket / aaa.txt.bak", so "MyBucket / I can't tell exactly if the key "aaa.txt" exists

Recommended Posts

To make sure that the specified key is in the specified bucket in Boto 3
How to judge that the cross key is input in Python3
The story that the private key is set to 600 with chmod
Output the key list included in S3 Bucket to a file
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
[Cloudian # 7] Try deleting the bucket in Python (boto3)
Make any key the primary key in Django's model
How to make a Raspberry Pi that speaks the tweets of the specified user
The background of the characters in the text image is overexposed to make it easier to read.
Linux is something like that in the first place
Find the part that is 575 from Wikipedia in Python
The theory that the key to controlling infection with the new coronavirus is hyperdispersion of susceptibility
[Python] Make sure the received function is a user-defined function
Get the value of a specific key up to the specified index in the dictionary list in Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
How to delete "(base)" that appears in the terminal when Anaconda is installed on Mac
[VLC] How to deal with the problem that it is not in the foreground during playback
Make sure all the elements in the list are the same in Python
How to test that Exception is raised in python unittest
[python] How to check if the Key exists in the dictionary
Put the lists together in pandas to make a DataFrame
Try to extract the keywords that are popular in COTOHA
Is there a bias in the numbers that appear in the Fibonacci numbers?
I want to make the Dictionary type in the List unique
I didn't want to write the AWS key in the program
Hypothesis / Verification (176) How to make a textbook that is easier than "The easiest textbook for quantum computers"
Solution to the problem that the display is corrupted when the .exe command is included in the while loop in wsl2
A story that is a little addicted to the authority of the directory specified by expdp (for beginners)
Play a sound in Python assuming that the keyboard is a piano keyboard
[PyTorch] Make sure the model and dataset are in cuda mode
[Cloudian # 6] Try deleting the object stored in the bucket with Python (boto3)
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
How to give and what the constraints option in scipy.optimize.minimize is
How to use the asterisk (*) in Python. Maybe this is all? ..
Normalize the file that converted Excel to csv as it is.
A script that uses boto to upload a specified folder to Amason S3
Throw something to Kinesis with python and make sure it's in
I want to make the second line the column name in pandas
The story that inheritance behavior is quite different in each language
What to do when the value type is ambiguous in Python?
Programming to fight in the world ~ 5-1
Programming to fight in the world 5-3
Programming to fight in the world-Chapter 4
In the python command python points to python3.8
[python] What is the sorted key?
Cython to try in the shortest
Programming to fight in the world ~ 5-2
The easiest way to make Flask
What to do when the result downloaded via scrapy is in English
[C language] I want to generate random numbers in the specified range
About the case that the speaker did not make sound in Ubuntu LTS 20.04
I want to identify the alert email. --Is that x a wildcard? ---
A shell script to make sure you don't forget the pipenv shell again
What is a C language library? What is the information that is open to the public?
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
What is the fastest way to create a reverse dictionary in python?
[Competition Pro] Algorithm that turns the sandwiched part over to make it ● ○ ●● ○○○ ● → ○○○○○○○○ (JSC2019-C Cell Inversion) [Explanation in the figure]
I created a script to check if English is entered in the specified position of the JSON file in Python.