I'm trying to get last added file in S3 specific folder. and I refer this(How to download the latest file of an S3 bucket using Boto3?) post and try.
@api_view(("GET",))
def get_protocol(request, pk):
union = Union.objects.get(pk=pk)
s3 = get_s3_client()
filepath = "media/private/" + str(pk) + "/certificate/docx"
get_last_modified = lambda obj: int(obj["LastModified"].strftime("%s"))
objs = s3.list_objects_v2(
Bucket="unifolio",
Prefix=filepath + "/" + "Union" + str(pk) + "_" + "certificate" + "3",
)
last_added = [obj["Key"] for obj in sorted(objs, key=get_last_modified)][0]
url = s3.generate_presigned_url(
ClientMethod="get_object",
Params={"Bucket": "unifolio", "Key": last_added},
# url 생성 후 60초가 지나면 접근 불가
ExpiresIn=60,
)
return Response()
but the error occur like below:
File "/Users/kimdoehoon/project/unifolio/unions/api_views.py", line 199, in get_protocol
objs_sorted = [obj["Key"] for obj in sorted(objs, key=get_last_modified)]
File "/Users/kimdoehoon/project/unifolio/unions/api_views.py", line 194, in <lambda>
get_last_modified = lambda obj: int(obj["LastModified"].strftime("%s"))
TypeError: string indices must be integers
I don't recognize why indices must be integer error. Could anybody kindly help me?