直接从html5将文件上载到Azure存储容器

Uploading files to Azure storage container from html5 directly

本文关键字:Azure 存储 上载 文件 html5      更新时间:2023-09-26

在我的场景中,我试图允许用户使用javascript将文件拖放到网页上,然后上传到容器中,上传文件的方式类似于wordpress媒体上传的管理方式。我遇到的问题是,我找到了为容器创建SAS url的代码,

        //Set the expiry time and permissions for the container.
        //In this case no start time is specified, so the shared access signature becomes valid immediately.
        SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
        sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24);
        sasConstraints.Permissions = SharedAccessBlobPermissions.Write;
        //Generate the shared access signature on the container, setting the constraints directly on the signature.
        string sasContainerToken = container.GetSharedAccessSignature(sasConstraints);
        //Return the URI string for the container, including the SAS token.
        return container.Uri + sasContainerToken;

但我发现的所有例子似乎都表明,我必须为每个blob 生成一个sas url

        Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        //Get a reference to a container to use for the sample code, and create it if it does not exist.
        Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(containerName);
        container.CreateIfNotExists();
        //Create a new stored access policy and define its constraints.
        Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy sharedPolicy = new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy()
        {
            SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),
            Permissions = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Write
        };
        //Get the container's existing permissions.
        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions permissions = container.GetPermissions();//new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions();
        Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
        return blob.Uri.AbsoluteUri + blob.GetSharedAccessSignature(sharedPolicy);

相反,就好像我正在上传一个文件。

管理员可以上传任何数量的文件,因此必须通过web api调用为这些文件中的每一个生成一个blob-sas似乎效率很低。我更愿意为容器生成一个SAS,并允许用户在指定的时间内上传到该容器,比如3小时。此外,我想使用分块上传每个文件。这可能吗?还是我必须为每个文件生成一个blob-sas url?

管理员可以上传任意数量的文件,因此必须通过webapi调用为这些文件中的每一个生成blob-sas效率非常低。我更愿意为容器,并允许用户将指定时间,例如3小时。

这当然是可能的。当您在具有Write权限的blob容器上创建SAS时(用于上载目的),该SAS可用于上载该容器中的多个blob。您只需要根据上传的文件构建blob URI并附加SAS令牌。例如,您在myaccount存储帐户中为mycontainer容器创建了一个SAS令牌,并上传了一个文件myfile.png,您的SAS URL将是https://myaccount.blob.core.windows.net/mycontainer/myfile.png?SAS-Token

我在您的代码中注意到,您正在返回带有SAS令牌的容器URI。在这种情况下,您只需要在容器名之后插入文件名即可获得blob上传URI。

此外,我想使用分块上传每个文件。

这又是可能的。不久前,我写了一些关于这方面的博客文章,你可能会觉得很有用:

http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/(这是在Azure Storage中支持CORS之前写的,所以请忽略我在帖子中对CORS的评论)。

http://gauravmantri.com/2013/12/01/windows-azure-storage-and-cors-lets-have-some-fun/