如何使用SAS限制azure blob容器创建

时间:2016-03-02 13:52:08

标签: azure cloud azure-storage-blobs

如何在生成帐户SAS令牌时设置不创建容器的权限?这是我的设置。

    // Create a new access policy for the account.
    SharedAccessAccountPolicy policy = new SharedAccessAccountPolicy()
    {
        Permissions = SharedAccessAccountPermissions.Read | SharedAccessAccountPermissions.Write,
        Services = SharedAccessAccountServices.Blob | SharedAccessAccountServices.Table,
        ResourceTypes = SharedAccessAccountResourceTypes.Service | SharedAccessAccountResourceTypes.Container | SharedAccessAccountResourceTypes.Object,
        SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(2),
        Protocols = SharedAccessProtocol.HttpsOrHttp
    };

1 个答案:

答案 0 :(得分:1)

更新回答:

鉴于您有多个容器,帐户SAS是一个不错的选择。您需要一个用于管理员,一个用于用户。

以下是如何创建admin SAS的示例:

// Create a new access policy for the account.
SharedAccessAccountPolicy policy = new SharedAccessAccountPolicy()
{
    // SAS for Blob service only.
    Services = SharedAccessAccountServices.Blob,

    // Admin has read, write, list, and delete permissions on all containers.
    // In order to write blobs, Object resource type must also be specified.
    ResourceTypes = SharedAccessAccountResourceTypes.Container | SharedAccessAccountResourceTypes.Object,

    Permissions = SharedAccessAccountPermissions.Read | 
        SharedAccessAccountPermissions.Write |
        SharedAccessAccountPermissions.Create | 
        SharedAccessAccountPermissions.List | 
        SharedAccessAccountPermissions.Delete,
    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
    Protocols = SharedAccessProtocol.HttpsOnly
};

以下是如何创建用户SAS的示例:

// Create a new access policy for the account.
SharedAccessAccountPolicy policy = new SharedAccessAccountPolicy()
{
    // SAS for Blob service only.
    Services = SharedAccessAccountServices.Blob,

    // User has create, read, write, and delete permissions on blobs.
    ResourceTypes = SharedAccessAccountResourceTypes.Object,

    Permissions = SharedAccessAccountPermissions.Read |
        SharedAccessAccountPermissions.Write |
        SharedAccessAccountPermissions.Create |
        SharedAccessAccountPermissions.Delete,
    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
    Protocols = SharedAccessProtocol.HttpsOnly
};

原始答案:

您肯定需要为admin SAS使用帐户SAS,但您应该能够在容器上为用户SAS使用服务SAS,除非您需要我不理解的帐户SAS题。在可能的情况下使用服务SAS可能更好,这样您就可以使用最不复杂的权限。此外,您可以将存储的访问策略与服务SAS一起使用,我们建议将其作为最佳做法,以便在受到损害时轻松撤销SAS。

使用SAS服务,您不需要限制容器创建的权限,因为服务SAS不允许您首先创建容器。

这是在容器上创建服务SAS的代码,包括存储的访问策略:

        // Create the storage account with the connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the blob client object.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Get a reference to the container for which shared access signature will be created.
        CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
        container.CreateIfNotExists();

        // Create blob container permissions, consisting of a shared access policy 
        // and a public access setting. 
        BlobContainerPermissions containerPermissions = container.GetPermissions();

        // Clear the container's shared access policies to avoid naming conflicts if you run this method more than once.
        //blobPermissions.SharedAccessPolicies.Clear();

        // The shared access policy provides 
        // read/write access to the container for 24 hours.
        containerPermissions.SharedAccessPolicies.Add("mypolicy", new SharedAccessBlobPolicy()
        {
            // To ensure SAS is valid immediately, don’t set start time.
            // This way, you can avoid failures caused by small clock differences.
            // Note that the Create permission allows the user to create a new blob, as does Write.
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
            Permissions = SharedAccessBlobPermissions.Write |
               SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Create | SharedAccessBlobPermissions.Delete
        });

        // The public access setting explicitly specifies that 
        // the container is private, so that it can't be accessed anonymously.
        containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;

        // Set the permission policy on the container.
        container.SetPermissions(containerPermissions);

        // Get the shared access signature to share with users.
        string sasToken =
           container.GetSharedAccessSignature(null, "mypolicy");

查看此处显示的示例:https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-shared-access-signature-part-1/#examples-create-and-use-shared-access-signatures

另见https://msdn.microsoft.com/en-us/library/azure/dn140255.aspxhttps://msdn.microsoft.com/en-us/library/azure/mt584140.aspx

如果您有任何其他问题,请告诉我们。