Due to the random volatility, a large amount of renewable energy will bring challenges to the security and stability of distribution network. Comprehensive consideration of system economics, security and flexibility has become the focus of research on distribution network optimization planning under the new situation. For the flexible resource allocation problem of distribution network, this paper analyzes the supply and demand relationship of the flexibility of the distribution network, and establishes a bi-level operationplanning joint optimization model for flexible resources. In the operational layer, we not only introduce the insufficient flexibility rate as the evaluation index of system flexibility, but also introduce the network loss, the abandoned wind and solar energy in the economic penalty, aiming at optimizing the annual operating cost of the system. In the planning layer, we evaluate the system security by introducing the comprehensive security index of the system, aiming at optimizing the annual comprehensive cost of the system. In addition, this paper also considers the third-party companies' investment in energy storage system in the electricity market, and further analyses the impact of energy storage operation strategies under different investors on flexible resource allocation. In this paper, the particle swarm optimization algorithm is used to solve the bi-level allocation model. Finally, the IEEE 33-bus test system is used for verification and analysis of the case. The results verify the validity and rationality of the proposed bi-level allocation model.