【问题标题】:Azure IListBlobItem Oftype CloudBlob Linq Change List<CloudBlob>Azure IListBlobItem Oftype CloudBlob Linq 更改列表<CloudBlob>
【发布时间】:2017-04-23 16:34:39
【问题描述】:

在我的页面中,我需要按名称、类型和 LastModified 搜索容器和 blob

 var selectedValue = ddlFileType.SelectedValue;
        AzureSettings container = AzureSettingsServices.Select(selectedValue.ParseInt(-1));
        if (ViewState[container.Name] == null)
        {
            IEnumerable<IListBlobItem> blobList = BlobHelper.GetFileListByContainer(container.Name);

            //I add the viewstate for not spending money in azure :)
            ViewState.Add(container.Name, blobList);

        }
         List<CloudBlob> list = null;
        string fileName = txtFileName.Text;
        if (!string.IsNullOrWhiteSpace(fileName))
        {
            //by name and date
            list = ((IEnumerable<IListBlobItem>)ViewState[container.Name]).OfType<CloudBlob>().Where(x => x.Name == fileName && x.Properties.LastModified >= dtDate.ValueRange.StartDate && x.Properties.LastModified <= dtDate.ValueRange.EndDate ).ToList();
        }
        else if (string.IsNullOrWhiteSpace(fileName))
        {
            //by date
            list = ((IEnumerable<IListBlobItem>)ViewState[container.Name]).OfType<CloudBlob>().Where(x => x.Properties.LastModified >= dtDate.ValueRange.StartDate && x.Properties.LastModified <= dtDate.ValueRange.EndDate ).ToList();
        }
        if (list != null)
        {
          // by type
            list=list.OfType<CloudBlob>().Where(x => x.Name.Contains(selectedValue)).ToList();

            SelectedContainer = container.Name;
            grdFiles.DataSource = list;
            grdFiles.DataBind();
        }

问题是我不能将 List 列表作为 gridview 数据源,这是正常的,但是如何将 List 列表设置为 ( IEnumerable bloblist 或在我的视图状态中(注意:在我的视图状态中是我的 Ilistblobitem 列表) 返回我的 gridview 数据源

更新错误 2

grdFiles.DataSource = ((IEnumerable)ViewState[container.Name]);

grdFiles.DataBind();

这里是关于的错误,

'Microsoft.WindowsAzure.Storage,版本=7.2.1.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35''Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility+d__0`1[[Microsoft.WindowsAzure.Storage.Blob. IListBlobItem,Microsoft.WindowsAzure.Storage,版本=7.2.1.0,文化=中性,PublicKeyToken=31bf3856ad364e35]]'

> [SerializationException: 'Microsoft.WindowsAzure.Storage, Version=7.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' Derlemesindeki 'Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility+<LazyEnumerable>d__0`1[[Microsoft.WindowsAzure.Storage.Blob.IListBlobItem, Microsoft.WindowsAzure.Storage, Version=7.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]' ]

System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType 类型) +12207601 System.Runtime.Serialization.FormatterServices.GetSerializableMembers(类型类型,StreamingContext 上下文)+230 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +143

System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(对象 obj,ISurrogateSelector surrogateSelector,StreamingContext 上下文,SerObjectInfoInit serObjectInfoInit,IFormatterConverter 转换器,ObjectWriter objectWriter,SerializationBinder binder)+178 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(对象 obj,ISurrogateSelector surrogateSelector,StreamingContext 上下文,SerObjectInfoInit serObjectInfoInit,IFormatterConverter 转换器,ObjectWriter objectWriter,SerializationBinder binder)+51 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +540 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +131 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(流序列化流,对象图)+17 System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer, Object value) +3046

[ArgumentException: 'Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility+d__01[[Microsoft.WindowsAzure.Storage.Blob.IListBlobItem, Microsoft.WindowsAzure.Storage, Version=7.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]' türündeki 'Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility+&lt;LazyEnumerable&gt;d__01[Microsoft.WindowsAzure.Storage.Blob.IListBlobItem]' değerini seri hale getirme hatası.] System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer, Object value) +3770 System.Web.UI.ObjectStateFormatter.Serialize(流输出流,对象状态图)+144 System.Web.UI.ObjectStateFormatter.Serialize(对象状态图,目的)+71 System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Serialize(对象状态)+39 System.Web.UI.Util.SerializeWithAssert(IStateFormatter 格式化程序,对象状态图)+37 System.Web.UI.Control.EstimateStateSize(对象状态)+45 System.Web.UI.Control.BuildProfileTree(字符串 parentId,布尔 calcViewState)+71 System.Web.UI.Page.BuildPageProfileTree(Boolean enableViewState) +42 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5761

【问题讨论】:

  • 我发现错误,IEnumerable 无法添加到视图状态

标签: linq azure azure-blob-storage oftype


【解决方案1】:

正如你所说,IEnumerable 不能添加到视图状态,也不能成为 gridview 的数据源。

据我所知,IListBlobItem 是一个接口。IEnumerable 包含三种类型的 blob 项。

CloudBlockBlob,CloudPageBlob,CloudBlobDirectory

所以我建议您可以先检查类型并转换为数据表或其他内容,然后将其添加到 ViewState 中。

如下:

  DataTable d1 = new DataTable();
            d1.Columns.Add("Type");
            d1.Columns.Add("Id");
            d1.Columns.Add("Url");
            foreach (var item in blobs)
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob blob = (CloudBlockBlob)item;

                    d1.Rows.Add("CloudBlockBlob", blob.Name, blob.Uri);
                }
                else if (item.GetType() == typeof(CloudPageBlob))
                {
                    CloudPageBlob blob = (CloudPageBlob)item;
                    d1.Rows.Add("CloudPageBlob", blob.Name, blob.Uri);
                }
                else if (item.GetType() == typeof(CloudBlobDirectory))
                {
                    CloudBlobDirectory blob = (CloudBlobDirectory)item;
                    d1.Rows.Add("directory", blob.StorageUri, blob.Uri);
                }

            }
            ViewState.Add(container.Name, d1);

结果:


在非回发和回发中,viewstate 会更好还是 shell 我再次为列表选择天蓝色?

据我所知,您需要为 azure 中的列表 blob 付费(每 10,000 美元 0.10 美元 LRS Cold)。所以我建议你可以使用 viewstate 来存储 listblob 操作的结果,如果你不需要更新 blob 的列表。


如何在我的 asp.net 中处理 sas 过期问题,它将超过 1000 个用户。当我删除 blobContainer.GetPermissions();并制作一个新的并设置它。另一个上传的用户将失败,因为我为另一个用户创建了一个新的 sas。

据我所知,如果你删除blobContainer的权限,就会删除SAS访问策略。

那么根据这个 SAS 访问策略创建的所有 SAS 令牌都将失效。

通常,我们可以创建没有到期日期的访问策略。

然后,您可以在为特定用户创建签名 URL 时根据此访问策略指定到期日期。

代码如下:

  SharedAccessPolicy sharedAccessPolicy = new SharedAccessPolicy();
    sharedAccessPolicy.Permissions = SharedAccessPermissions.Read;
    sharedAccessPolicy.SharedAccessStartTime = DateTime.UtcNow;
    //sharedAccessPolicy.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(1); No need to define expiry time here.

    BlobContainerPermissions blobContainerPermissions = new BlobContainerPermissions();
    blobContainerPermissions.SharedAccessPolicies.Add("default", sharedAccessPolicy);

    container.SetPermissions(blobContainerPermissions);

    Console.WriteLine("Press any key to continue....");
    Console.ReadLine();
    CloudBlob blob = container.GetBlobReference(path);

    string sas = blob.GetSharedAccessSignature(new SharedAccessPolicy()
    {
        SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7),//add expiry date only when you're creating the signed URL
    }
        , "default");

    Console.WriteLine(blob.Uri.AbsoluteUri + sas);

    Process.Start(new ProcessStartInfo(blob.Uri.AbsoluteUri + sas));

    Console.WriteLine("Press any key to continue....");
    Console.ReadLine();

此外,我们可以创建 5 个访问策略,因此我建议您可以创建不同的访问策略并为不同的用户创建不同的 SAS 令牌。

【讨论】:

  • hmm.. 是的,你说得对,新的数据表是个好主意。谢谢你的建议,你是最好的:) viewstate 会更好还是 shell 我在非回发和回发中再次为列表选择天蓝色?
  • 我能问第二个也是最后一个问题吗:) 如何在我的 asp.net 中处理 sas 过期问题,它将超过 1000 个用户。当我删除 blobContainer.GetPermissions();并创建一个新的并设置它。另一个上传的用户将失败,因为我为另一个用户创建了一个新的 sas。
  • 更新答案
  • 那么 viewstate 对我来说很有意义我只需要列出。我认为容器读取权限在没有 blob 引用的情况下不会提供任何安全风险:我用 Sas + uri 呈现 blob。对不起,我让你厌烦了,但我学到了很多,也许其他朋友可以从中受益..
  • 通过使用正确的 SAS,我们可以读取或更新带有 url + SAS 的 blob。但是如果你留下你的 SAS 令牌并且它没有过期,那么每个人都可以在没有授权的情况下访问这个 blob 容器。此外,我很高兴帮助其他人解决问题,如果您觉得答案已经解决了您的问题,请将其标记为答案以帮助其他面临同样问题的人。谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-12-15
  • 2011-12-08
  • 2020-03-16
  • 2013-03-29
  • 2021-04-16
  • 2017-07-04
  • 2022-12-18
  • 2013-01-31
相关资源
最近更新 更多