Reputation: 409
In my page ,I need to search containers and blobs by name,type and LastModified
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
UPDATE ERROR 2 :
grdFiles.DataSource = ((IEnumerable)ViewState[container.Name]);
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]]'
> [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ğ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
Upvotes: 0
Views: 1206
Reputation: 27997
As you says IEnumerable can not add to viewstate and couldn't be a datasource to gridview.
As far as I know, IListBlobItem is an interface.The IEnumerable contains three type of blob items.
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.
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);
will viewstate better or shell I go azure for the list again in nonpostback and postback ?
As far as I know, you need pay for the list blob in azure( $0.10 LRS Cold per 10,000). 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.
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.
As far as I know, if you delete the blobContainer's permissions, it will delete the SAS access policy.
Then all SAS token which created according to this SAS access policy will useless.
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.
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.
Upvotes: 1