[英]how to check if particular file exists in s3 bucket
I am new to AWS and I want to check if a particular csv exists in a folder in s3.我是 AWS 的新手,我想检查 s3 的文件夹中是否存在特定的 csv。 And if it does i want to read it and if it doesn't i want to create a df and upload it to s3.
如果是,我想阅读它,如果不是,我想创建一个 df 并将其上传到 s3。
what i did till now到目前为止我做了什么
list_of_files = []
for key in s3_client.list_objects(Bucket= 'abc',Prefix="folder/")['Contents']:
list_of_files.append(key['Key'])
check_files = [list of file to check]
something like就像是
if set(check_files) in set(list_of_files):
read_from_s3(file)
else:
pd.Dataframe()
Use the s3_client get_object.使用 s3_client get_object。 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.get_object
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.get_object
If the object doesn't exists it will throw exception S3.Client.exceptions.NoSuchKey如果 object 不存在,它将抛出异常 S3.Client.exceptions.NoSuchKey
Check below sample检查以下示例
try:
s3_client.get_object(
Bucket=self._bucket,
Key=key,
)
return True
except s3_client.exceptions.NoSuchKey:
return False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.