Duplicate key errors when uploading attachments

Either through user error (double-submit) or retry caused by timeouts,
we often get errors raised due to duplicate keys because the same
attachment is uploaded twice in quick succession. These changes handle
this specific case.