Christopher Jakob
Christopher Jakob

Reputation: 493

how to add custom meta data to presigned post s3 upload?

The answer below in incomplete this question is still outstanding.

I have been looking at the docs and from what I gather from aws s3 docs is to add custom meta data add custom fields that start with x-amz-meta-

so if i wanted to add a user meta data object which value is 3 it would be

x-amz-meta-user : 3

in practice in my formData on the front end I have

let fd = new FormData();
    fd.append('acl', req.fields.acl);
    fd.append('key', req.fields.key);
    fd.append('content-type', req.fields['content-type']);
    fd.append('policy', req.fields.policy);
    fd.append('x-amz-meta-user', req.fields['x-amz-meta-user']);
    fd.append('x-amz-meta-contentpost', req.fields['x-amz-meta-contentpost']);
    fd.append('x-amz-algorithm', req.fields['x-amz-algorithm']);
    fd.append('x-amz-credential', req.fields['x-amz-credential']);
    fd.append('x-amz-date', req.fields['x-amz-date']);
    fd.append('x-amz-signature', req.fields['x-amz-signature']);

but I am getting this error:

 error: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AccessDenied</Code>
<Message>Invalid according to Policy: Extra input fields: x-amz-meta-user</Message><RequestId>HJC06ARY0MDRCCTM</RequestId>
<HostId>73sCuIaxYp+Y3o8DOpTai1Abtji4Gaz0GEbLo1tr80t57VohzFTFStJlaUavVilh3FAKkfjEmyM=</HostId></Error>"

class PrivateGeneratePresignedUrlResource(APIView):

def get(self, request, *args, **kwargs):
    userid = kwargs.get('userid')
    contentpostid = kwargs.get('contentpostid')
    if checkIfUserIsContentCreator(request.user):
        if checkIfUserIsActive(request.user):
            user = getUserObject(request.user)
            if user.id == int(userid):
                contentcreatorobject = user.contentcreatoruserid
                contentpost = get_object_or_404(ContentFeedPost, id = int(contentpostid), contentcreator= contentcreatorobject)
                keytime = datetime.now().strftime('%H%M%S%f')
                randomkey = random.randrange(10000000000000, 99999999999999)
                awskey = keytime + str(randomkey)
                fields = {'acl': 'bucket-owner-full-control',
                          'x-amz-meta-user': int(userid),
                          'x-amz-meta-contentpost': int(contentpostid),
                          'content-type': '*'}
                conditions = [
                    {
                        'acl': 'bucket-owner-full-control'
                    },
                    {
                        'content-type': '*'
                    }
                ]
                s3 = boto3.client('s3',
                                  aws_access_key_id=AWS_ACCESS_KEY_ID,
                                  aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
                                  region_name=AWS_REGION_NAME)
                post = s3.generate_presigned_post(
                    Bucket=AWS_S3_MOD_BUCKET_NAME,
                    Key=awskey,
                    Fields=fields,
                    Conditions=conditions
                )
                return Response({
                    'url': post['url'],
                    'fields': post['fields'],
                    'uriroot': AWS_S3_MOD_BUCKET_ROOT_URI
                })
            context = {'param userid is not request user id'}
            return Response(context, status=HTTP_401_UNAUTHORIZED )
        context = {'content creator is not active'}
        return Response(context, status=HTTP_401_UNAUTHORIZED)
    context = {'user is not content creator'}
    return Response(context, status=HTTP_401_UNAUTHORIZED)

which makes me think I am missing something or I read the docs wrong. There is a missing piece is it obvious to someone?

Upvotes: 2

Views: 2658

Answers (1)

MisterSmith
MisterSmith

Reputation: 3624

You need to include x-amz-meta-user in the Fields parameter of generate_presigned_post to approve its use - see http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html and https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html.

Upvotes: 1

Related Questions