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

Azure IListBlobItem Oftype CloudBlob Linq Change List<CloudBlob>

在我的页面中,我需要按名称、类型和 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 < CloudBlob > 列表作为正常的 gridview 数据源,但是如何将 List < CloudBlob > 列表设置为 ( IEnumerable < IListBlobItem > 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__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+<LazyEnumerable>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(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

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

据我所知,IListBlobItem 是一个 interface.The 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);

结果:


will viewstate better or shell I go azure for the list again in nonpostback and postback ?

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


How can I handle sas expiration in my asp.net it will be more than 1000 user.. When I delete the 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.

据我所知,如果删除 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 令牌。