简体   繁体   English

Azure IListBlobItem类型CloudBlob Linq更改列表<CloudBlob>

[英]Azure IListBlobItem Oftype CloudBlob Linq Change List<CloudBlob>

In my page ,I need to search containers and blobs by name,type and LastModified 在我的页面中,我需要按名称,类型和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();
        }

The problem is I can not give List < CloudBlob > list as gridview datasource that is normal ,but how can I set to the List < CloudBlob > list in to ( IEnumerable < IListBlobItem > bloblist or in my viewstate(note : in my viewstate is my Ilistblobitem list) ) back for my gridview datasource 问题是我不能将List <CloudBlob> list作为正常的gridview数据源提供,但是如何将List <CloudBlob> list设置为 IEnumerable <IListBlobItem> bloblist或在我的viewstate中(注意:在我的viewstate中是我的Ilistblobitem列表) 返回到我的gridview数据源

UPDATE ERROR 2 : 更新错误2

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

grdFiles.DataBind(); grdFiles.DataBind();

Here is the error about , 这是关于的错误,

'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]]' 'Microsoft.WindowsAzure.Storage,版本= 7.2.1.0,区域性=中性,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 type) +12207601 System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +230 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +143 System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType类型)+12207601 System.Runtime.Serialization.FormatterServices.GetSerializableMembers(类型类型,StreamingContext上下文)+230 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()

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.Objec System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(对象obj,ISurrogateSelector代理选择器,StreamingContext上下文,SerObjectInfoInit serObjectInfoInit,IFormatterConverter转换器,ObjectWriter objectWriter,SerializationBinder绑定程序)+178 System.Runtime.Seriainize。 (对象obj,ISurrogateSelector surrogateSelector,StreamingContext上下文,SerObjectInfoInit serObjectInfoInit,IFormatterConverter转换器,ObjectWriter objectWriter,SerializationBinder绑定程序)+51 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph,Header [BererW] fCheck)+540 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(流序列化流,对象图,Header []标头,布尔值fCheck)+131 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(流序列化Stream) ,对象图)+17 System.Web.UI.Objec tStateFormatter.SerializeValue(SerializerBinaryWriter writer, Object value) +3046 tStateFormatter.SerializeValue(SerializerBinaryWriter writer,对象值)+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ğerini seri hale getirme hatası.] 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 cal [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]'的详细信息,请参见System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerIner writer,对象值)+3770 System.Web.UI.ObjectStateFormatter.Serialize(流outputStream,Object stateGraph)+144 System.Web.UI.ObjectStateFormatter.Serialize(Object stateGraph,目的)+71 System.Web.UI.ObjectStateFormatter .System.Web.UI.IStateFormatter.Serialize(对象状态)+39 System.Web.UI.Util.SerializeWithAssert(IStateFormatter格式化程序,Object stateGraph)+37 System.Web.UI.Control.EstimateStateSize(对象状态)+45系统.Web.UI.Control.BuildProfileTree(String parentId,Boolean cal cViewState) +71 System.Web.UI.Page.BuildPageProfileTree(Boolean enableViewState) +42 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5761 cViewState)+71 System.Web.UI.Page.BuildPageProfileTree(布尔enableViewState)+42 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+5761

As you says IEnumerable can not add to viewstate and couldn't be a datasource to gridview. 如您所说,IEnumerable不能添加到viewstate,也不能是gridview的数据源。

As far as I know, IListBlobItem is an interface.The IEnumerable contains three type of blob items. 据我所知,IListBlobItem是一个接口。IEnumerable包含三种类型的Blob项目。

CloudBlockBlob,CloudPageBlob,CloudBlobDirectory

So I suggest you could firstly check the type and convert to datatable or something else and then add it into ViewState. 因此,我建议您首先检查类型并转换为数据表或其他内容,然后将其添加到ViewState中。

Like below: 如下所示:

  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);

Result: 结果: 在此处输入图片说明


will viewstate better or shell I go azure for the list again in nonpostback and postback ? viewstate会更好还是shell在nonpostback和postback中再次青睐列表?

As far as I know, you need pay for the list blob in azure( $0.10 LRS Cold per 10,000). 据我所知,您需要为天蓝色的清单blob(每10,000美元的LRS冷币$ 0.10)支付费用。 So I suggest you could use viewstate to store the result of the listblob operation, if you don't need to update the list of blob. 因此,如果您不需要更新Blob的列表,建议您使用viewstate来存储listblob操作的结果。


How can I handle sas expiration in my asp.net it will be more than 1000 user.. When I delete the blobContainer.GetPermissions(); 我如何在asp.net中处理sas过期,它将超过1000个用户。当我删除blobContainer.GetPermissions();时, and make a new one and set it.The another user who upload will fail because of I create a new sas for another user. 并进行新的设置。由于我为另一个用户创建了新的sas,因此另一个上载的用户将失败。

As far as I know, if you delete the blobContainer's permissions, it will delete the SAS access policy. 据我所知,如果删除blobContainer的权限,它将删除SAS访问策略。

Then all SAS token which created according to this SAS access policy will useless. 然后,根据此SAS访问策略创建的所有SAS令牌将无用。

Normally, we could create a access policy without expiry date. 通常,我们可以创建一个没有到期日期的访问策略。

Then you could specify the expiry date according to this access policy when you're creating the signed URL for specific users. 然后,当您为特定用户创建签名URL时,可以根据此访问策略指定到期日期。

The codes is like below: 代码如下:

  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();

Besides, we could create 5 access policy, so I suggest you could create different access policy and create different SAS token for different user. 此外,我们可以创建5个访问策略,因此建议您创建不同的访问策略并为不同的用户创建不同的SAS令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM