【问题标题】:Azure data lake - External org app access - Authentication methodsAzure 数据湖 - 外部组织应用访问 - 身份验证方法
【发布时间】:2020-12-10 13:40:51
【问题描述】:

假设 organisation A 有一个 Azure 数据湖实例 DL1DL1 实例中有两个容器,分别是 container1container2。现在来自org Bapp A 必须单独访问container2 才能删除文件。

有哪些可用的身份验证方法?任何带有 Azure Python 或 Java SDK 的示例代码参考都会很棒。谢谢。

【问题讨论】:

    标签: azure azure-data-lake-gen2


    【解决方案1】:

    更新 1210:使用兼容的 azure blob 存储 sdk

    您可以使用azure blob storage sdk 来创建container sas token,那么ADLS Gen2 中的用户只能访问this specified container

    注意:我们可以这样做,因为 azure blob storage sdk 与 ADLS Gen2 兼容,请参阅 here 了解更多详细信息..

    以python为例,首先安装the latest azure blob storage package

    然后使用下面的代码生成一个container sas token,可以提供给org B中的apps

    from azure.storage.blob import generate_container_sas, ContainerSasPermissions, ContainerClient
    from datetime import datetime, timedelta
    
    account_name ="your adls gen2 account name"
    account_key="the key"
    container_name="the container name"
    
    sas_token = generate_container_sas(
        account_name=account_name,
        container_name=container_name,
        account_key=account_key,
        permission=permission = ContainerSasPermissions(read=True,write=True,delete=True,list=True),
        expiry= datetime.utcnow() + timedelta(days=3)
    )
    
    #build the following container url of ADLS Gen2, then provide it to others to access the specified container.
    sas_url="https://" + account_name + ".blob.core.windows.net/" + container_name + "?" + sas_token
    
    #when other users have this sas_url, they can use it to access the specified container.
    container = ContainerClient.from_container_url(sas_url)
    
    #for example, they can upload files to the container.
    with open("D:\\test\\ttt.txt","rb") as data:
        container.upload_blob(name="test333.txt",data=data)
    

    更新 01:使用 ADLS Gen2 sdk

    您可以创建container sas token,然后将container sas token 提供给其他人。所以其他人只能使用它来访问指定的容器。

    这里是python的例子,请先安装azure-storage-file-datalake 12.2.0包:

    from azure.storage.filedatalake import generate_file_system_sas,FileSystemSasPermissions,FileSystemClient
    from datetime import datetime, timedelta
    
    account_name ="ADLS Gen2 account name"
    account_key="the key"
    container_name="the container name"
    
    #build account_url
    account_url="https://" + account_name + ".dfs.core.windows.net/"
    
    #generate the container sas token
    sas_token = generate_file_system_sas(
        account_name=account_name,
        file_system_name=container_name,
        credential=account_key,
        permission=FileSystemSasPermissions(read=True,write=True,delete=True,list=True),
        expiry=datetime.utcnow() + timedelta(days=3)
    )
    
    #build the sas account url
    sas_url = account_url + "?" + sas_token
    
    #then we can provide the sas url to others, and they only allowed to access the specified container
    container_client = FileSystemClient(sas_url,container_name)
    file_client = container_client.create_file("c333.txt")
    local_file = open("D:\\test\\ttt.txt",'rb')
    file_contents = local_file.read()
    file_client.append_data(data=file_contents, offset=0, length=len(file_contents))
    file_client.flush_data(len(file_contents))
    


    原答案:

    您可以直接使用ADLS Gen2account nameaccount key进行认证。您可以参考这个official document了解更多详情。

    例如,您可以导航到 azure 门户 -> Azure Data Lake Gen2 -> 访问密钥。从那里,您可以获得帐户名称和帐户密钥。这是截图:

    对于python,您可以关注this article来构建身份验证。

    java用户可以关注this article

    【讨论】:

    • 我只想授予对一个容器/文件系统的访问权限,而不是对整个数据湖或所有容器的访问权限
    • @Rag,我可以想到 2 种方法来做到这一点。请查看更新后的答案:)。
    • 谢谢伊万。以编程方式,您使用 DL/BStorage SDK 的 sn-ps 都有效。使用 Azure 存储资源管理器生成的 SAS URL 不起作用并要求更多权限。示例错误:The SAS token has inadequate permissions. A list permission ('sp=l') is required.
    • 当然。你知道为什么生成的 SAS 字符串不适用于桌面 azure storage explorer 应用吗?
    • @Rag,稍后我会看看并更新这个答案。
    猜你喜欢
    • 2018-02-12
    • 2017-12-03
    • 2018-07-15
    • 2015-10-14
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多