Search code examples
pythonboto3attributeerror

AttributeError: 'dict' object has no attribute 'append' boto3 generate presigned post


I am attempting to use boto3 to create a presigned post signature and form.

 key = str(venuepk) +'/' + str(samplemenuid) + '/${filename}'
            fields = {'acl': 'public-read',
                      'content-type': 'application/PDF'}
            conditions = {'acl': 'public-read',
                      'content-type': 'application/PDF'}
            s3=boto3.client('s3')
            post = s3.generate_presigned_post(
                Bucket = AWS_UPLOAD_BUCKET,
                Key = key,
                Fields = fields,
                Conditions=conditions
            )
            return Response({
                'url': post['url'],
                'fields': post['fields'],
                'samplemenupk': samplemenuid
            })

followed the docs to a 'T'

https://boto3.readthedocs.io/en/latest/guide/s3.html#generating-presigned-posts

but I keep getting this error:

File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/rest_framework/views.py", line 491, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/suitsandtables/venues/views.py", line 142, in post
    Conditions=conditions
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/botocore/signers.py", line 698, in generate_presigned_post
    conditions.append({'bucket': bucket})
AttributeError: 'dict' object has no attribute 'append'

Solution

  • The error is because there is no append method for python dicts. The conditions value should be a list as shown below

    conditions = [
        {"acl": "public-read"},
        ["content-length-range", 10, 100]
    ]
    

    It's actually shown as a list in the documentation