In cloud computing, cloud providers can provide cloud consumers with two provisioning plans for computing resources, namely reservation and demand plans. In general, the cost of using the IT resources provided by the reserve plan is cheaper than that provided by the on-demand plan, since the cloud consumer has to pay the supplier in advance. With the reserve plan, the consumer can reduce the total cost of supplying resources. However, the best anticipated reserve of resources is difficult to achieve due to the uncertainty of future consumer demand and the prices of suppliers' resources. To address this problem, an optimal cloud resource provisioning algorithm (OCRP) is proposed through the formulation of a stochastic programming model. The OCRP algorithm can provide calculation resources to be used in multiple stages of provisioning, as well as a long-term plan, for example, four stages in a fourth and twelve-step plan in an annual plan. The uncertainty of demand and prices is considered in the OCRP. In this work, different approaches are considered to obtain the solution of the OCRP algorithm including deterministic equivalent formulation, approximation of the sample mean and decomposition of Benders. Numerical studies are conducted in which the results clearly show that with the OCRP algorithm, the cloud consumer can successfully minimize the total cost of resource provisioning in cloud computing environments.