直接从 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?
An administrator can upload any number of files, so to have to
generate a blob sas via web api call for each one of these files seems
to be very inefficient. I would prefer to generate a SAS for the
container and allow the user to upload to that container for a
specified time, say 3 hours.
当然可以。当您在具有 Write
权限(用于上传目的)的 blob 容器上创建 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。
Also, I would like to use chunking to upload each file.
又是可能的。前段时间我写了一些关于这个的博客 posts,你可能会发现它们有用:
http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/(这是在 Azure 存储支持 CORS 之前写的,所以请忽略我在 post 中关于 CORS 的评论)。
http://gauravmantri.com/2013/12/01/windows-azure-storage-and-cors-lets-have-some-fun/
对于我的场景,我试图允许用户使用 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?
An administrator can upload any number of files, so to have to generate a blob sas via web api call for each one of these files seems to be very inefficient. I would prefer to generate a SAS for the container and allow the user to upload to that container for a specified time, say 3 hours.
当然可以。当您在具有 Write
权限(用于上传目的)的 blob 容器上创建 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。
Also, I would like to use chunking to upload each file.
又是可能的。前段时间我写了一些关于这个的博客 posts,你可能会发现它们有用:
http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/(这是在 Azure 存储支持 CORS 之前写的,所以请忽略我在 post 中关于 CORS 的评论)。
http://gauravmantri.com/2013/12/01/windows-azure-storage-and-cors-lets-have-some-fun/