如何检查特定文件是否存在于我的 S3 中的特定目录中?我使用 Boto3 并尝试了此代码(不起作用):
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
key = 'dootdoot.jpg'
objs = list(bucket.objects.filter(Prefix=key))
if len(objs) > 0 and objs[0].key == key:
print("Exists!")
else:
print("Doesn't exist")
最佳答案
import boto3
import botocore
client = boto3.client('s3')
def checkPath(file_path):
result = client.list_objects(Bucket="Bucket", Prefix=file_path )
exists=False
if 'Contents' in result:
exists=True
return exists
如果提供的 file_path 将存在,那么它将返回 True。例如:'s3://bucket/dir1/dir2/dir3/file.txt'
文件路径:'dir1/dir2' 或 'dir1/'
注意:- 文件路径应该从存储桶名称之后的第一个目录开始。
关于python-3.x - 如何使用python和boto3检查S3存储桶中是否存在特定目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57957585/