如何使用boto3

时间:2018-12-28 14:29:50

标签: python amazon-web-services amazon-s3 boto3

我只想使用boto3从S3存储桶中检索使用特定前缀的last_modified键。

# Get Today's date
today = datetime.date.today()

# Get Objects date
s3 = boto3.resource('s3',region_name=AWS_REGION)
bucket = s3.Bucket('xxx')
objs = bucket.objects.filter(Prefix='yyyy').limit(1)

def get_object_check_alarm():
  try:
    for obj in objs:
        print(obj)
        lastobjectdate = (obj.last_modified).date()
  except botocore.exceptions.ClientError as e:
    error_code = e.response['Error']['Code']
    if error_code == '404':
        print("There is no file")

  # Compare with defined date
  if today == lastobjectdate:
    print(today)
    print(lastobjectdate)
    print("OK, lastest file comes from today")
  else:
    print(today)
    print(lastobjectdate)
    print("Mail sent")

使用此代码,当前结果不会输出最后修改的密钥。我试图将limit()增加到limit(10),但没有成功。

1 个答案:

答案 0 :(得分:1)

-更新开始-

可能最好在S3中使用日期前缀创建对象。

{bucket} / yyyy / mm / dd / {object}

示例:myS3bucket / 2018/12/29 / myfile.txt

采用这种方法,您的查询变得很容易找出当天是否有任何文件,而且检索到的文件列表也很短。

prefix="/"+str(today.year)+"/"+str(today.month)+"/"+str(today.day)+"/"
objs = bucket.objects.filter(Prefix=prefix).all()

-更新完成-

我不确定您是否提供了完整的代码,但以上代码段中存在一些缩进问题。我刚刚在下面进行了测试,并且工作正常,并且我得到了正确的last_modified日期。

请确保您位于正确的存储区区域。另外,last_modified处于UTC时区,因此您的比较应该考虑到这一点。

import boto3
from datetime import date
import botocore

# Get Today's date
today = date.today()
# Get Objects date
s3 = boto3.resource('s3',region_name='us-east-1')
bucket = s3.Bucket('xxxx')
prefix="/"+str(today.year)+"/"+str(today.month)+"/"+str(today.day)+"/"
objs = bucket.objects.filter(Prefix=prefix).all()

def get_object_check_alarm():
    try:
        for obj in objs:
            print(obj)
            lastobjectdate = (obj.last_modified).date()
    except botocore.exceptions.ClientError as e:
        error_code = e.response['Error']['Code']
        if error_code == '404':
            print("There is no file")

# Compare with defined date
    if today == lastobjectdate:
        print(today)
        print(lastobjectdate)
        print("OK, lastest file comes from today")
    else:
        print(today)
        print(lastobjectdate)
        print("Mail sent")

get_object_check_alarm()

下面是输出。我在EST区域中,所以日期仍然是12/28,但是对象创建日期是12/29,因为创建对象时它在UTC区域中已经是12/29。

  

s3.ObjectSummary(bucket_name ='xxxx',key ='yyyy /')

     

2018-12-28

     

2018-12-29

     

邮件已发送