直接从html5将文件上载到Azure存储容器
Uploading files to Azure storage container from html5 directly
在我的场景中,我试图允许用户使用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/
- 通过Azure存储以HTML形式获取JSON文件
- Fin上传器直接上传到Azure Blob存储-在Javascript中获取文件uri
- Azure documentdb存储过程返回部分结果
- Javascript下载一个URL-Azure Blob存储
- Azure节点:从SQL Azure Easy表插入功能访问表存储
- Azure Blob 存储 请求的资源错误中不存在访问控制允许源标头
- 将返回的对象存储到 Azure Microsoft数组 JavaScript 中
- 在 Azure 表存储的插入函数中查询其他表
- 使用拖放区将文件直接上传到 Azure Blob 存储(使用 SAS).js
- Node.js azure存储TableService没有方法
- 将两个github存储库部署到单个azure web应用程序
- 直接从html5将文件上载到Azure存储容器
- 从js上传一个图像文件到azure blob存储
- 将文件从客户端上载到Azure Blob存储|MVC
- Azure存储身份验证(JavaScript)
- 使用Node.js访问azure表存储
- 强健的Azure存储库(CloudBlob, PageBlob)为Silverlight或Javascript
- Azure存储blob REST API: MAC签名不同于Azure计算签名(缺少内容类型)
- 在 JavaScript 中创建 SAS for Azure 存储 (PhoneGap)
- Azure存储块Blob从客户端Javascript上传