【问题标题】:Xamarin text blob Azure storageXamarin 文本 blob Azure 存储
【发布时间】:2018-10-30 11:12:02
【问题描述】:

我正在使用 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

这是屏幕上的输出:

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

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>

【问题讨论】:

  • 如果您的浏览器无法获取图像,那么 xamarin 似乎与您的问题完全不相关。请考虑使用MVCE 编辑它。

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


【解决方案1】:

错误意味着您没有将Public access level 设置为Blob

请在您的教程中查看此要求。

您使用的代码需要此设置,因为它直接使用 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。

【讨论】:

    【解决方案2】:

    1.请先检查您的订阅。

    2.检查您的容器的访问策略。

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

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

    2) 获取配置中的访问密钥。

    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(新 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 2014-01-13
      • 2021-04-01
      • 2017-11-10
      • 2012-03-02
      • 2016-03-04
      • 2012-05-20
      相关资源
      最近更新 更多