Топ-5 провайдеров облачного хостинга и примеры кода для взаимодействия с ними

Вот 5 лучших провайдеров облачного хостинга:

  1. Веб-службы Amazon (AWS)
  2. Microsoft Azure
  3. Облачная платформа Google (GCP)
  4. Облако IBM
  5. Oracle Cloud

Теперь давайте рассмотрим некоторые методы взаимодействия с этими провайдерами облачного хостинга на примерах кода.

  1. AWS (Веб-сервисы Amazon):

    import boto3
    # Create an EC2 instance
    ec2 = boto3.resource('ec2')
    instance = ec2.create_instances(
    ImageId='ami-0c94855ba95c71c99',
    MinCount=1,
    MaxCount=1,
    InstanceType='t2.micro'
    )
    print(instance[0].id)
  2. Azure (Microsoft Azure):

    from azure.identity import DefaultAzureCredential
    from azure.mgmt.compute import ComputeManagementClient
    # Authenticate using DefaultAzureCredential
    credential = DefaultAzureCredential()
    compute_client = ComputeManagementClient(credential, subscription_id='your-subscription-id')
    # Create a virtual machine
    vm_params = {
    'location': 'eastus',
    'hardware_profile': {
        'vm_size': 'Standard_DS1_v2'
    },
    'storage_profile': {
        'image_reference': {
            'publisher': 'Canonical',
            'offer': 'UbuntuServer',
            'sku': '16.04-LTS',
            'version': 'latest'
        }
    },
    'os_profile': {
        'computer_name': 'my-vm',
        'admin_username': 'azureuser',
        'admin_password': 'MyPassword123'
    }
    }
    compute_client.virtual_machines.create_or_update('my-resource-group', 'my-vm', vm_params)
  3. GCP (Облачная платформа Google):

    from google.cloud import compute_v1
    # Create an instance
    compute_client = compute_v1.InstancesClient()
    project = 'your-project-id'
    zone = 'us-central1-a'
    instance = {
    'name': 'my-instance',
    'machine_type': f'zones/{zone}/machineTypes/n1-standard-1',
    'disks': [{
        'boot': True,
        'initializeParams': {
            'source_image': f'projects/debian-cloud/global/images/family/debian-10'
        }
    }],
    'network_interfaces': [{
        'network': f'projects/{project}/global/networks/default',
        'access_configs': [{
            'name': 'External NAT',
            'type': 'ONE_TO_ONE_NAT'
        }]
    }]
    }
    operation = compute_client.insert(project=project, zone=zone, instance=instance)
  4. IBM Cloud:

    import ibm_boto3
    # Create a virtual server
    service = ibm_boto3.resource('SoftLayer_Virtual_Guest')
    virtual_server = service.create_instance(
    hourlyBillingFlag=True,
    hostname='my-instance',
    domain='example.com',
    datacenter={'name': 'dal10'},
    flavor={'keyname': 'C1_1X1X25'},
    operatingSystemReferenceCode='UBUNTU_16_64'
    )
    print(virtual_server['id'])
  5. Oracle Cloud:

    import oci
    # Create a compute instance
    config = oci.config.from_file()
    compute_client = oci.core.ComputeClient(config)
    compartment_id = 'your-compartment-id'
    availability_domain = 'your-availability-domain'
    create_instance_details = oci.core.models.LaunchInstanceDetails(
    compartment_id=compartment_id,
    availability_domain=availability_domain,
    display_name='my-instance',
    shape='VM.Standard2.1',
    image_id='your-image-id',
    subnet_id='your-subnet-id',
    metadata={'ssh_authorized_keys': 'your-public-key'}
    )
    instance = compute_client.launch_instance(create_instance_details)
    print(instance.data.id)