繁体   English   中英

使用 AZURE_STORAGE_ACCESS_TOKEN 连接到 Azure Data Lake Storage (Gen 2) 虚拟文件的 Gdal

[英]Gdal connection to Azure Data Lake Storage (Gen 2) virtual file using AZURE_STORAGE_ACCESS_TOKEN

我想从gdal 版本 3.5访问我的 Azure 数据湖存储(第 2 代)文件,使用 AZURE_STORAGE_ACCESS_TOKEN 进行身份验证,如下所述: https://gdal.org/user/virtual_file_systems.html#sia

我们的组织存储帐户中未启用任何其他身份验证选项(AZURE_STORAGE_CONNECTION_STRING、AZURE_NO_SIGN_REQUEST=YES、AZURE_STORAGE_SAS_TOKEN...)

不知道为什么 oauth2 令牌调用 Azure 不起作用 - 请参阅https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow

url: https://login.microsoftonline.com/<TENANT_ID>/oauth2/token

headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}数据请求: {'client_id': '<client_id>', 'resource': 'https://storage.azure.com/.default', 'client_secret': '[REDACTED]', 'grant_type': 'client_credentials'}

响应: {'token_type': 'Bearer', 'expires_in': '3599', 'ext_expires_in': '3599', 'expires_on': '1663774788', 'not_before': '1663770888', 'resource': '<azure_enterprise_app_id>', 'access_token': '<REDACTED>'}

原来您可以使用 MSAL(python SDK)设置 AZURE_STORAGE_ACCESS_TOKEN 参见https://github.com/AzureAD/microsoft-authentication-library-for-python

这有效:

from msal import ConfidentialClientApplication


def get_token():
    app = ConfidentialClientApplication(
        os.getenv("AZURE_SP_CLIENT_ID"),
        authority="https://login.microsoftonline.com/mmcglobal.onmicrosoft.com",
        client_credential=os.getenv("AZURE_SP_CLIENT_SECRET"),
    )

    result = app.acquire_token_for_client(scopes="https://storage.azure.com/.default")

    if "access_token" in result:
        # Call a protected API with the access token.
    #     print(result["token_type"])
        print("Setting access token")
    else:
        print(result.get("error"))
        print(result.get("error_description"))
        print(result.get("correlation_id"))  # You might need this when reporting a bug.
    return result['access_token']


os.environ["AZURE_STORAGE_ACCOUNT"] = <account_name>
os.environ["AZURE_STORAGE_ACCESS_TOKEN"]=get_token()

现在我可以从 /vsiadls/ 加载文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM