标签: azure-sdk-python

Python Azure sdk:如何从 keyvault 检索机密?

我需要从 keyvault 中检索秘密。到目前为止,这是我的代码:

from azure.mgmt.keyvault import KeyVaultManagementClient
from azure.common.credentials import ServicePrincipalCredentials


subscription_id = 'x'
# See above for details on creating different types of AAD credentials
credentials = ServicePrincipalCredentials(
    client_id = 'x',
    secret = 'x',
    tenant = 'x'
)

kv_client = KeyVaultManagementClient(credentials, subscription_id)

for vault in kv_client.vaults.list():
    print(vault)
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

msrestazure.azure_exceptions.CloudError:Azure 错误:AuthorizationFailed 消息:对象 ID 为“x”的客户端“x”无权在范围“/subscriptions/x”上执行操作“Microsoft.Resources/subscriptions/resources/read”。

现在我可以使用 C# 代码/ POwershell 使用相同的凭据访问相同的 keyvault,因此授权肯定没有问题。不知道为什么它不能使用 SDK 工作。请帮忙。

python azure azure-keyvault azure-sdk-python azure-sdk

5
推荐指数
2
解决办法
2万
查看次数

是否有 azure python sdk 来检索应用程序洞察数据?

我使用下面的应用程序见解 python sdk 在应用程序见解资源中发送我的自定义指标。

https://github.com/Microsoft/ApplicationInsights-Python

现在,当我想要检索它时,我只能找到基于 Rest API 的方法来检索它。

https://dev.applicationinsights.io/documentation/Using-the-API/Metrics

是否有基于 RestAPI 的curl 或 http 请求的替代方案 - 在应用程序洞察 python sdk 中?

azure azure-application-insights azure-sdk-python

5
推荐指数
2
解决办法
4900
查看次数

使用python将CSV文件上载到Microsoft Azure存储帐户

我正在尝试.csv使用python 将文件上传到Microsoft Azure存储帐户.我发现C-sharp代码将数据写入blob存储.但是,我不懂C#语言.我需要.csv使用python 上传文件.

是否有任何python示例将CSV文件的内容上传到Azure存储?

python azure azure-storage-blobs azure-sdk-python azure-functions

4
推荐指数
1
解决办法
5733
查看次数

使用python sdk在azure中的linux vm中运行命令

我发现 azure python sdk 提供了以下在 linux vm 中运行命令的方法。

from azure.mgmt.compute import compute_management_client
from azure.common.credentials import ServicePrincipalCredentials

credentials = ServicePrincipalCrendentials(client_id, secret, tenant)
client = compute_management_client(credentials, subscription_id)

client.virtual_machines.run_command(resource_group_name,
     vm_name, parameters, customheaders=None, raw=False,
     **operation_config)
Run Code Online (Sandbox Code Playgroud)

但是我如何在这里传递我的命令?我找不到参数和 operation_config 的任何示例。请帮忙

azure azure-virtual-machine azure-sdk-python

4
推荐指数
1
解决办法
2159
查看次数

无法将较大的 blob 上传到 Azure:azure.core.exceptions.ServiceRequestError:操作未完成(写入)(_ssl.c:2317)

我正在尝试使用 Python SDK 将一些更大的 blob (>50MB) 上传到我的 Azure 存储容器:

connect_str = os.environ['AZURE_STORAGE_CONNECTION_STRING']
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

def upload_blob(file_path):
    if os.path.exists(file_path):
        with open(file_path, 'rb') as data:
            blob_client = blob_service_client.get_blob_client(container='foo', blob=file_path)

            print(f"Uploading file {file_path} to blob storage...")
            print(os.path.getsize(file_path))
            return blob_client.upload_blob(data, length=os.path.getsize(file_path))
    else:
        print(f"File {file_path} not found. Please store the file first before uploading")
        return False
Run Code Online (Sandbox Code Playgroud)

但是,当我运行它时,我得到一个azure.core.exceptions.ServiceRequestError

Traceback (most recent call last):
  File "C:/Users/.../storage_controller.py", line 96, in <module>
    upload_blob(config.VECTORIZER_PATH)
  File "C:/Users/.../storage_controller.py", line 34, in upload_blob
    return blob_client.upload_blob(data, length=os.path.getsize(file_path))
  File "C:\Users\...\venv\lib\site-packages\azure\core\tracing\decorator.py", line 83, …
Run Code Online (Sandbox Code Playgroud)

python blob azure azure-storage-blobs azure-sdk-python

3
推荐指数
1
解决办法
825
查看次数

使用 Azure python sdk 创建 NSG 不使用安全规则

我在用

\n\n

\xce\xbb pip show azure\nName: azure\nVersion: 2.0.0

\n\n

我想创建一个具有特定安全规则的 NSG。我有以下代码。

\n\n

````

\n\n
from azure.mgmt.compute import ComputeManagementClient\nfrom azure.mgmt.network import NetworkManagementClient\nfrom azure.common.credentials import ServicePrincipalCredentials\nfrom azure.mgmt.network.v2017_03_01.models import NetworkSecurityGroup\nfrom azure.mgmt.network.v2017_03_01.models import SecurityRule\nsubscription_id = \'my-id\'\ncredentials = ...\n\ncompute_client = ComputeManagementClient(\n    credentials,\n    subscription_id\n)\n\nnetwork_client = NetworkManagementClient(\n    credentials,\n    subscription_id\n)\nfrom azure.mgmt.resource.resources import ResourceManagementClient\n\nresource_client = ResourceManagementClient(\n    credentials,\n    subscription_id\n)\nresource_client.providers.register(\'Microsoft.Compute\')\nresource_client.providers.register(\'Microsoft.Network\')\n\nresource_group_name = \'test-rg\'\n\nsecurity_rule = SecurityRule( protocol=\'Tcp\', source_address_prefix=\'Internet\', \n                              source_port_range="*", destination_port_range="3389", priority=100,\n                              destination_address_prefix=\'*\', access=\'Allow\', direction=\'Inbound\')\nnsg_params = NetworkSecurityGroup(id=\'test-nsg\', location=\'UK South\', tags={ \'name\' : \'testnsg\' })\nnetwork_client.network_security_groups.create_or_update(resource_group_name, "test-nsg", parameters=nsg_params, security_rules=[security_rule])\n
Run Code Online (Sandbox Code Playgroud)\n\n

这确实使 NSG 很好,但未能创建适当的规则。

\n\n

我缺少什么? …

azure azure-virtual-network azure-sdk-python

2
推荐指数
1
解决办法
2665
查看次数

Azure CLI与Python SDK

在使用Azure CLI或Azure Python SDK方面是否有推荐的方法?

CLI似乎有更好的文档,但由于它是基于Python构建的,我认为它最终将共享Azure Python SDK的许多功能.

有什么想法吗?是否有一个支持另一个?

作为参考,我们正在构建一个基于Linux的应用程序,包括python和偶尔的shell脚本.所以我们可以使用其中任何一个,尽管我们发现CLI更容易,因为它有更好的文档.

azure azure-cli azure-sdk-python

2
推荐指数
1
解决办法
884
查看次数

使用 Python SDK 创建 Azure 容器时出现“HTTP 标头格式不正确”错误

我正在尝试使用 Python SDK 和以下代码创建 Azure blob 容器。我在响应中收到“ErrorCode:InvalidHeaderValue”。

我正在使用存储帐户的 Azure 门户的“访问密钥”部分中的“ConnectionString”。我不认为连接是问题,因为这条线工作正常blob_service_client = BlobServiceClient.from_connection_string(connection_string)

我为此使用了干净的 venv,下面是库版本 azure-core==1.10.0 azure-storage-blob==12.7.1

import os
import yaml
from azure.storage.blob import ContainerClient, BlobServiceClient

def load_config():
    dir_root = os.path.dirname(os.path.abspath(__file__))
    with open (dir_root + "/config.yaml", "r") as yamlfile:
        return yaml.load(yamlfile, Loader=yaml.FullLoader)

config = load_config()
connection_string = config['azure_storage_connectionstring']

blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_service_client.create_container('testing')
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
  File "/Users/anojshrestha/Documents/codes/gen2lake/project_azure/lib/python3.7/site-packages/azure/storage/blob/_container_client.py", line 292, in create_container
    **kwargs)
  File "/Users/anojshrestha/Documents/codes/gen2lake/project_azure/lib/python3.7/site-packages/azure/storage/blob/_generated/operations/_container_operations.py", line 134, in create
    raise HttpResponseError(response=response, model=error)
azure.core.exceptions.HttpResponseError: Operation returned an invalid status …
Run Code Online (Sandbox Code Playgroud)

python azure azure-storage azure-sdk-python

2
推荐指数
1
解决办法
7384
查看次数

Azure SDK Python:标记特定资源

我想使用 python 在 Azure 中的每个资源上创建标签。

我在文档中看到这个模块: http //azure-sdk-for-python.readthedocs.io/en/latest/ref/azure.mgmt.resource.resources.operations.html#azure.mgmt.resource.resources。操作.标签操作

create_or_update: 创建订阅资源标签列表:获取订阅资源标签列表

似乎我只能对资源组进行标记操作,而不能对资源进行标记操作?

例子:

向资源组添加标签:Set-AzureRmResourceGroup 向资源添加标签:Set-AzureRmResource

编辑:

感谢您的 api 查找代码,非常整洁。但我相信我手动放置的旧 api 也应该有效。我几乎没有修改过你的代码(我们可能有不同的 Azure SDK,我使用的是 2.0.0rc5)。添加api函数(非常有用)后,不幸的是我仍然遇到相同的错误。

from azure.common.credentials import UserPassCredentials
from azure.mgmt.resource.resources import ResourceManagementClient

def resolve_resource_api(client, resource):
    """ This method retrieves the latest non-preview api version for
    the given resource (unless the preview version is the only available
    api version) """
    provider = client.providers.get(resource.id.split('/')[6])
    rt = next((t for t in provider.resource_types
               if t.resource_type == '/'.join(resource.type.split('/')[1:])), None)
    #print(rt)
    if rt and 'api_versions' in rt.__dict__: …
Run Code Online (Sandbox Code Playgroud)

python azure azure-sdk-python

1
推荐指数
1
解决办法
3361
查看次数

如何通过Python Azure SDK知道Azure Blob对象的大小

遵循针对Python开发人员Microsoft Azure文档。该azure.storage.blob.models.Blob班确实有所谓的私有方法__sizeof__()。但是,无论blob为空(0字节)还是1 GB,它都会返回一个恒定值16。是否有可动态检查对象大小的Blob对象的任何方法/属性?

更清楚地说,这就是我的源代码的样子。

for i in blobService.list_blobs(container_name=container, prefix=path):
    if i.name.endswith('.json') and r'CIJSONTM.json/part' in i.name:
        #do some stuffs
Run Code Online (Sandbox Code Playgroud)

但是,数据池包含许多具有合法名称的空blob,在我之前#do some stuffs,我想对大小进行额外检查,以判断我是否在处理空blob。

另外,__sizeof__()如果不是blob对象的大小,该方法究竟能提供什么的好处呢?

python sizeof azure azure-storage-blobs azure-sdk-python

1
推荐指数
1
解决办法
1228
查看次数

在 Azure 函数代码中使用 Python 包

我创建了一个 Azure 函数,并希望能够在该函数的 Python 代码中使用各种包;以 Numpy 为例。显然,代码一旦发布到 Azure 就不会在我的本地机器上运行。这意味着我无法将 Numpy 安装到它运行的任何基础架构上,因此我无法在我的代码中导入 Numpy。如何在代码中使用像 Numpy 这样的包?

azure azure-sdk-python azure-functions azure-sdk

1
推荐指数
1
解决办法
1429
查看次数

使用 python API 列出 Azure BlobStorage 上的文件

此代码尝试列出 Blob 存储中的文件:

#!/usr/bin/env python3

import os

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
from datetime import datetime, timedelta
import azure.cli.core as az

print(f"Azure Blob storage v{__version__} - Python quickstart sample")

account_name = "my_account"
container_name = "my_container"
path_on_datastore = "test/path"

def _create_sas(expire=timedelta(seconds=10)) -> str:
    cli = az.get_default_cli()

    expire_date = datetime.utcnow() + expire
    expiry_string = datetime.strftime(expire_date, "%Y-%m-%dT%H:%M:%SZ")
    cmd = ["storage", "container", "generate-sas", "--name", container_name, "--account-name",
           account_name, "--permissions", "lr", "--expiry", expiry_string, "--auth-mode", "login", "--as-user"]
    if cli.invoke(cmd) != 0:
        raise RuntimeError("Could not receive …
Run Code Online (Sandbox Code Playgroud)

python azure python-3.x azure-blob-storage azure-sdk-python

0
推荐指数
1
解决办法
7242
查看次数