Azure IListBlobItem Oftype CloudBlob Linq更改列表<cloudblob>

时间:2017-04-23 16:34:40

标签: linq azure azure-blob-storage oftype

在我的页面中,我需要按名称,类型和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&lt; CloudBlob&gt;列为gridview数据源是正常的,但我如何设置为List&lt; CloudBlob&gt;列入 IEnumerable&lt; IListBlobItem&gt; bloblist或我的视图状态(注意:在我的视图状态中是我的Ilistblobitem列表)返回我的gridview数据源

更新错误2

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

grdFiles.DataBind();

以下是关于

的错误
  

'Microsoft.WindowsAzure.Storage,Version = 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,Version = 7.2.1.0,Culture = neutral,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 type)+12207601      System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type,StreamingContext context)+230      System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()+ 143

     

System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj,ISurrogateSelector surrogateSelector,StreamingContext context,SerObjectInfoInit serObjectInfoInit,IFormatterConverter converter,ObjectWriter objectWriter,SerializationBinder binder)+178      System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj,ISurrogateSelector surrogateSelector,StreamingContext context,SerObjectInfoInit serObjectInfoInit,IFormatterConverter converter,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(Stream serializationStream,Object graph)+17      System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer,Object value)+3046

     

[ArgumentException:'Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility + d__0 1[[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+<LazyEnumerable>d__0 1 [Microsoft.WindowsAzure.Storage.Blob.IListBlobItem]'değerinisersehalegetirmehatası。]      System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer,Object value)+3770      System.Web.UI.ObjectStateFormatter.Serialize(Stream outputStream,Object stateGraph)+144      System.Web.UI.ObjectStateFormatter.Serialize(Object stateGraph,Purpose purpose)+71      System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Serialize(Object state)+39      System.Web.UI.Util.SerializeWithAssert(IStateFormatter formatter,Object stateGraph)+37      System.Web.UI.Control.EstimateStateSize(Object state)+45      System.Web.UI.Control.BuildProfileTree(String parentId,Boolean calcViewState)+71      System.Web.UI.Page.BuildPageProfileTree(Boolean enableViewState)+42      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+5761

1 个答案:

答案 0 :(得分:1)

正如您所说,IEnumerable无法添加到viewstate,也无法成为gridview的数据源。

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

CloudBlockBlob,CloudPageBlob,CloudBlobDirectory

所以我建议您首先检查类型并转换为datatable或其他内容,然后将其添加到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);

结果: enter image description here

  

会更好地查看状态,或者我在非后备和回发中再次查看列表?

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

  

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

据我所知,如果删除blobContainer的权限,它将删除SAS访问策略。

然后,根据此SAS访问策略创建的所有SAS令牌都将无用。

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

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

代码如下:

  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令牌。