How to restrict azure blob container creation usin

2019-03-04 14:44发布

How to set permission to not create container, while generating Account SAS token? Here is my settings.

    // 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条回答
做个烂人
2楼-- · 2019-03-04 15:20

Updated answer:

Given that you have multiple containers, the account SAS is a good option. You'll need one for the admin and one for the user.

Here's an example of how to create the 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
};

And here's an example of how to create the user 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
};

Original answer:

You definitely need to use an account SAS for the admin SAS, but you should be able to use a service SAS on the container for the user SAS, unless you have a need for an account SAS that I am not understanding from your question. It's probably better to use the service SAS when you can so that you can use the least complicated permissions. Also, you can use a stored access policy with the service SAS, which we recommend as a best practice so that it's easy to revoke the SAS if it were ever compromised.

With the service SAS, you don't need a permission to restrict container creation, because the service SAS doesn't allow you to create a container in the first place.

Here's code to create the service SAS on the container, including the stored access policy:

        // 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");

Take a look at the examples shown here: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-shared-access-signature-part-1/#examples-create-and-use-shared-access-signatures.

Also see https://msdn.microsoft.com/en-us/library/azure/dn140255.aspx and https://msdn.microsoft.com/en-us/library/azure/mt584140.aspx.

Let us know if you have any other questions.

查看更多
登录 后发表回答