Xamarin文本blob Azure存储

时间:2018-05-20 18:09:31

标签: azure xamarin.forms azure-sql-database azure-storage azure-storage-blobs

我使用Azure存储blob来存储图像,我试图在我的Xamrin.form应用程序中显示它。我在github上找到了一个简单的教程和代码。

我已按照以下步骤成功实施,并在azure storage blob上创建帐户。

问题是:我可以看到文件的名称,但不能看到“图像”

这是错误:

read started: <Thread Pool> #9
[0:] HTTP Request: Could not retrieve https://xxxxxx.blob.core.windows.net/yyyy/kakashi.jpg, status code NotFound
[0:] ImageLoaderSourceHandler: Could not retrieve image or image data was invalid: Uri: https://lxxxxxx.blob.core.windows.net/yyyy/kakashi.jpg
Thread finished: <Thread Pool> #4

这是教程: click to see

这是Github: click to see

以下是屏幕上的输出: enter image description here

我将图片放入Urlof时出现此错误(https://lxxxxxx.blob.core.windows.net/yyyy/kakashi.jpg )在我的bronwser上:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>ResourceNotFound</Code>
<Message>
The specified resource does not exist. RequestId:97933c69-a01e-014f-6669-f0502e000000 Time:2018-05-20T18:33:28.4774584Z
</Message>
</Error>

2 个答案:

答案 0 :(得分:2)

错误表示您未将Public access level设置为Blob

在教程中查看此要求。 enter image description here

您使用的代码需要此设置,因为它使用blob Uri直接访问blob。

请参阅PhotosBlobStorageService.cs

return blobList.Select(x => new PhotoModel { Title = x.Name, Uri = x.Uri }).ToList();

如果您确实希望保持Private级别,则必须对上述语句进行一些更改。这是参考。

return blobList.Select(x => new PhotoModel { Title = x.Name,
            Uri = new Uri(x.Uri+x.GetSharedAccessSignature(
                new SharedAccessBlobPolicy {
                    Permissions = SharedAccessBlobPermissions.Read|SharedAccessBlobPermissions.Write,
                    // you can modify the expiration to meet your requirement
                    SharedAccessExpiryTime = DateTime.UtcNow.AddYears(1)
                } ))
        }).ToList();

此更改允许您使用SAS访问私人blob。

答案 1 :(得分:1)

1.请先检查您的订阅。

2.检查容器的访问策略。 enter image description here

3. 以下是保存并通过代码获取blob的步骤。

1)使用NuGet,我们可以安装所需的Assembly包。 转到“管理解决方案菜单包”并搜索WindowsAzure.Storage和WindowsAzure.ConfigurationManager并单击“安装”。

2)获取配置中的访问密钥。 enter image description here

3)通过代码创建blob的示例代码:

 public async Task<string> SaveImagesToAzureBlob(HttpPostedFileBase imageToUpload)
    {
        try
        {
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("sampleimage");

            if (await cloudBlobContainer.CreateIfNotExistsAsync())
            {
                await cloudBlobContainer.SetPermissionsAsync(
                    new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    }
                    );
            }

            string imageFullPath = null;
            string imageName = Guid.NewGuid().ToString() + "-" + Path.GetExtension(imageToUpload.FileName);

            CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(imageName);
            cloudBlockBlob.Properties.ContentType = imageToUpload.ContentType;
            await cloudBlockBlob.UploadFromStreamAsync(imageToUpload.InputStream);

            imageFullPath = cloudBlockBlob.Uri.ToString();
            return imageFullPath;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

现在,检查您的存储帐户,您可以看到生成的容器样本。

默认情况下,容器将是私有的,没有人可以从外部访问。要设置权限,我们应该使用如下的SetPermission方法。

CloudBlobContainer .SetPermissions(new BlobContainerPermissions {PublicAccess = BlobContainerPublicAccessType.Blob});
请在列表中尝试不同的权限。

请注意权限级别设置。在您的情况下,它可能会导致问题。

了解更多详情: 参考

https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-deployment-model https://docs.microsoft.com/en-us/azure/storage/blobs/storage-dotnet-how-to-use-blobs