對於我的場景,我試圖讓用戶將文件拖放到使用JavaScript上傳到容器的網頁上,類似wordpress媒體上傳文件行政方面的工作。我遇到的問題是,我發現的代碼創建一個SAS URL的容器,直接從html5上傳文件到Azure存儲容器
//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;
,但重要的是我發現的例子似乎表明,我必須生成一個SAS網址爲每個斑
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?
我已閱讀並檢查過您的示例,它們非常好。我在單個文件上傳場景中引用了您的分塊示例。所以,在這種情況下,我的web api調用會生成「myfile.png」周圍的所有內容,而且我只需要javascript就可以添加相應的文件名,因爲它會循環上傳文件。例如,我的web api調用可以創建類似https://myaccount.blob.core.windows.net/mycontainer/search-replace?SAS-Token的東西,javascript可以用相應的文件名替換search-replace? – user1790300
這是正確的。如果您查看第一個鏈接中的代碼。它已經在那裏做了。順便說一句,我們有一個免費的實用程序,可以直接在您的存儲帳戶上使用該帖子中提到的代碼上傳blob。如果您不想自己寫一些東西,請隨時查看:http://cloudportam.com/features/storage/blob-receiver。 –
感謝您的幫助。我非常感謝。 – user1790300