0

我正在使用 Web API 检索 SAS 以将图片上传到 Windows Azure blob 存储。不幸的是,它不起作用。有趣的是,它可以在任何其他应用程序中运行,甚至在 Windows Store 应用程序中。这是我的控制器代码:

// 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 项目创建时默认生成的示例值控制器。

4

1 回答 1

1

你打电话给地址http://127.0.0.1:81/api/127.0.0.1指向您的 Windows Phone 设备,而不是您的本地服务器。请改用您机器的 IP 地址。

于 2013-03-28T15:59:49.183 回答