CORS error appears when I Upload multiple files at once to S3 Bucket with boto3

I have a question about S3 upload, I use boto3 library in python to connect to S3.

def get_client():
return boto3.client('s3', aws_access_key_id = 'AWS_ACCESS_KEY',
        aws_secret_access_key = 'AWS_SECRET_KEY')

My Multiple upload file in below

for item in img_files:
            file_type = ''
            uuid_key = uuid.uuid4()
            fs = FileSystemStorage()
            filename = fs.save(f'{S3_TEMP_FOLDER}{uuid_key}-{item.name}', item)
            file_o_name, file_o_ext = os.path.splitext(filename)
            if file_o_ext.lower() in ['.jpg', '.png', '.jpeg']:
                file_type = 'image'
            else:
                file_type = 'video'

            uploaded_file_path = fs.path(filename)
            
            try:
                s3_client.upload_file(uploaded_file_path, bucket_name, f'{folder_key}/{uuid_key}-{item.name}', ExtraArgs={'ACL': 'public-read'})
                uploaded_data = {
                    "file_name": item.name,
                    "file_key": f'{folder_key}/{uuid_key}-{item.name}',
                    "bucket_name": bucket_name,
                    "folder_id": get_folder_id.id,
                    "created_by": request.user.username,
                    "updated_by": request.user.username,
                    "bucket_id": bucket.id,
                    "file_type": file_type
                }
                S3FileManagement.objects.create(**uploaded_data)
            except Exception as e:
                logger.error({'message': str(e)})
                logger.error({'message': S3_FOLDER_MESSAGE['S3_FILE_UPLOAD_FAILED']})
                return Response({'message': S3_FOLDER_MESSAGE['S3_FILE_UPLOAD_FAILED']}, status=status.HTTP_400_BAD_REQUEST)

When I upload about 5-10 files at a time, the upload process is no problem, but when I upload > 30 files, there will be some files with 502 - CORS errors.

In settings python, I was allows CORS

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOWED_ORIGINS = [
    '..{url}..vercel.app',
]
CSRF_TRUSTED_ORIGINS = [
    '..{url}..vercel.app',
]

This is the picture I got error.

Additionaly, the python backend I'm using aapanel. Any more additional settings needed?

Thanks.

Back to Top