Пример #1
0
    def zzztest_storage_account_creation(self):  #!!! need to update
        from azure.common.credentials import UserPassCredentials
        from azure.mgmt.resource import ResourceManagementClient
        from azure.mgmt.storage import StorageManagementClient
        from azure.storage import CloudStorageAccount
        from azure.storage.blob.models import ContentSettings
        import getpass

        username, subscription_id = [
            s.strip()
            for s in open(os.path.expanduser("~") +
                          "/azurebatch/account.txt").xreadlines()
        ]
        print "Azure password"
        password = getpass.getpass()
        credentials = UserPassCredentials(username, password)
        resource_client = ResourceManagementClient(credentials,
                                                   subscription_ids)
        storage_client = StorageManagementClient(credentials, subscription_id)
        resource_client.resource_groups.create_or_update(
            'my_resource_group', {'location': 'westus'})
        async_create = storage_client.storage_accounts.create(
            'my_resource_group', 'my_storage_account', {
                'location': 'westus',
                'account_type': 'Standard_LRS'
            })

        async_create.wait()
        storage_keys = storage_client.storage_accounts.list_keys(
            'my_resource_group', 'my_storage_account')
        storage_keys = {v.key_name: v.value for v in storage_keys.keys}
        storage_client = CloudStorageAccount('my_storage_account',
                                             storage_keys['key1'])
        blob_service = storage_client.create_block_blob_service()
        blob_service.create_container('my_container_name')
        blob_service.create_blob_from_bytes(
            'my_container_name',
            'my_blob_name',
            b'<center><h1>Hello World!</h1></center>',
            content_settings=ContentSettings('text/html'))
        print(blob_service.make_blob_url('my_container_name', 'my_blob_name'))
Пример #2
0
containername = os.environ['AZURE_CONTAINER_NAME']
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
resource_group_params = {'location' : location}
sku = 'standard_ragrs)'
kind = 'BlobStorage'
storage_account_params = {sku:sku,kind:kind,location:location}

# Configure Credentials
credentials = ServicePrincipalCredentials(client_id=os.environ['AZURE_CLIENT_ID'],secret=os.environ['AZURE_CLIENT_SECRET'],tenant=os.environ['AZURE_TENANT_ID'])
resource_client = ResourceManagementClient(credentials, subscription_id)
storage_client = StorageManagementClient(credentials, subscription_id)

# Create Resource Group & Storage Account
resource_client.resource_groups.create_or_update(resourcegroupname, resource_group_params)
create_sa = storage_client.storage_accounts.create(resourcegroupname, storageaccountname, {'location':'eastus','kind':'storage','sku':{'name':'standard_ragrs'}})
create_sa.wait()

# Create Container
sak = storage_client.storage_accounts.list_keys(resourcegroupname, storageaccountname)
storageaccountkey = sak.keys[0].value
storage_client = CloudStorageAccount(storageaccountname, storageaccountkey)
blob_service = storage_client.create_block_blob_service()
blob_service.create_container(containername,public_access=PublicAccess.Blob)

# Copy Files
file_service = FileService(account_name=storageaccountname, account_key=storageaccountkey)
file_service.create_share(containername)
file_service.create_directory(containername, 'directory1')
file_service.create_file_from_path(containername,'directory1','55224azuresetup.ps1','55224azuresetup.ps1',)