Windows Azure BLOB ストレージに画像をアップロードするための SAS を取得するために Web API を使用しています。残念ながら、うまくいきません。面白いのは、Windows ストア アプリであっても、他のアプリケーションで動作することです。これは私のコントローラコードです:
// GET api/users?container="test"&blobname="test.jpg"
public string Get(string container, string blobname)
{
try
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("ConnectionString"));
//CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); ;
CloudBlobContainer blobContainer = blobClient.GetContainerReference(container);
blobContainer.CreateIfNotExist();
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
// Define a 4 hour window that the Windows 8 client can write to Azure Blob Storage.
containerPermissions.SharedAccessPolicies.Add("mypolicy", new SharedAccessPolicy()
{
Permissions = SharedAccessPermissions.Write, // | SharedAccessPermissions.Read ,
//To be available immediately don't set SharedAccessStartTime =
SharedAccessExpiryTime = DateTime.Now.Add(TimeSpan.FromHours(4))
});
// Set the permissions so that Windows 8 client can write to the container
// for the 4 hours specified above.
blobContainer.SetPermissions(containerPermissions);
// Create the shared access signature that will be added to the URL.
string sas = blobContainer.GetSharedAccessSignature(new SharedAccessPolicy(), "mypolicy");
// Creat the URI to be return to the Windows 8 client that will be used to write
// to blob storage.
return string.Format("{0}/{1}{2}", blobContainer.Uri, blobname, sas);
}
catch
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
}
そしてここにクライアントがあります:
string photoSasUrl = "http://127.0.0.1:81/api/users?container={0}&blobname={1}";
using (HttpClient client = new HttpClient())
using (var response = await client.GetAsync(String.Format(photoSasUrl, "profiles-pictures", "test.jpg")))
{
// Retrieve Shared Access Signature from Web Service
var sasUrl = await response.Content.ReadAsStringAsync();
// Trim any miscellaneous quotes
sasUrl = sasUrl.Trim('"');
// Load bytes of image into content object
var content = new StreamContent(e.ChosenPhoto);
// Content-Type will be image/jpeg
content.Headers.Add("Content-Type", "image/jpg");
// Write the bytes of the photo to blob storage
using (var uploadResponse = await client.PutAsync(new Uri(sasUrl), content))
{
if (uploadResponse.IsSuccessStatusCode)
{
// If successful, show on screen
MessageBox.Show("Upload Successful");
}
}
}
WebClient wc = new WebClient();
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri("http://127.0.0.1:81/api/values"));
}
catch { }
私が見つけたのは、Web API プロジェクトの作成時にデフォルトで生成されるサンプル値コントローラーでも機能しないことです。