python-3.x - 如何使用python和boto3检查S3存储桶中是否存在特定目录

标签 python-3.x amazon-s3 boto3

如何检查特定文件是否存在于我的 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/

相关文章:

python - np.random.choice 在直方图中有一个缺口

python - python3 中的 Requests.get 不起作用。需要帮助才能从搜索查询的所有页面获取数据

python - Scrapy - 无法在 XMLFeedSpider 中发出额外请求

python - 如何从 S3 加载 pickle 文件以在 AWS Lambda 中使用?

python - 在 matplotlib 中更改字体

java - 使用AWS S3作为监控平台的中间存储层

shell - 将文件复制到 AWS S3 存储桶

javascript - 上传到亚马逊s3非常慢需要时间

ubuntu - 如何使用 Boto3 修复 DynamoDB 的 "NoCredentialsError"

python - 模拟boto3 S3客户端方法Python