How to add LastModifiedTime to Blob Container.
- Hi,As we know we have the LastModifiedTime Property with Blob Container. But Whenever Itrying to retrieve the container.LastModifiedTime property it always returned {1/1/0001 12:00:00 AM}.So I want to know thathow to add the LastModifiedTime Property of the blobContainer so that we can retrieve the Last Modified Timeof the blobContainer whenever required.RegardsMunish Bhargav
Answers
- Hello, you must call GetContainerProperties in order to get the actual LastModifiedTime.
ContainerProperties cp = container.GetContainerProperties();
Now you can access cp.LastModifiedTime. note container.LastModifiedTime will not be updated, so please examine the ContainerProperties.LastModifiedTime property.
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.- Proposed As Answer byAnton Staykov Wednesday, November 04, 2009 8:41 AM
- Marked As Answer byMunish Bhargav Wednesday, November 04, 2009 11:40 AM
- The container's LastModifiedDate is only updated when the container itself is changed (such as metadata changes). To get the last modified date of the latest modified blob, you will have to enumerate through all the blobs, and investigate the blobs' LastModifiedDate. Yes, this is inconsistent with Windows file system. I'll pass it to our storage team to see if this behavior can be changed in the future.
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.- Unmarked As Answer byMunish Bhargav Wednesday, November 11, 2009 10:17 AM
- Marked As Answer byMunish Bhargav Wednesday, November 04, 2009 11:40 AM
- Marked As Answer byMunish Bhargav Wednesday, November 11, 2009 10:17 AM
- Looks like you haven't updated the metadata on the server. Unfortunately currently the StorageClient library does not expose an UpdateMetadata method. But you can create your own.
First in BlobStorage.cs, find the BlobContainer class, and add an abstract method:
public abstract bool UpdateMetadata(NameValueCollection metadata);
Then in RestBlobStorage.cs, find the BlobContainerRest class, and implement this method. The implementation is very similar to other operations.
public override bool UpdateMetadata(NameValueCollection metadata)
{
bool retval = false;
RetryPolicy(() =>
{
NameValueCollection queryParams = new NameValueCollection();
queryParams.Add(StorageHttpConstants.QueryParams.QueryParamComp, StorageHttpConstants.CompConstants.Metadata);
ResourceUriComponents uriComponents;
Uri uri = Utilities.CreateRequestUri(BaseUri, UsePathStyleUris, AccountName, ContainerName, null, Timeout, queryParams, out uriComponents);
HttpWebRequest request = Utilities.CreateHttpRequest(uri, StorageHttpConstants.HttpMethod.Put, Timeout);
Utilities.AddMetadataHeaders(request, metadata);
credentials.SignRequest(request, uriComponents);
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
retval = true;
}
else if ((response.StatusCode == HttpStatusCode.PreconditionFailed ||
response.StatusCode == HttpStatusCode.NotModified))
{
retval = false;
}
else
{
Utilities.ProcessUnexpectedStatusCode(response);
retval = false;
}
response.Close();
}
}
catch (IOException ioe)
{
throw new StorageServerException(StorageErrorCode.TransportError, "The connection may be lost",
default(HttpStatusCode), ioe);
}
catch (System.TimeoutException te)
{
throw new StorageServerException(StorageErrorCode.ServiceTimeout, "Timeout during blob metadata upload",
HttpStatusCode.RequestTimeout, te);
}
catch (WebException we)
{
if (we.Response != null)
{
HttpWebResponse response = (HttpWebResponse)we.Response;
if ((response.StatusCode == HttpStatusCode.PreconditionFailed ||
response.StatusCode == HttpStatusCode.NotModified))
{
retval = false;
return;
}
}
throw Utilities.TranslateWebException(we);
}
});
return retval;
}
Now in your main code, you can call container.UpdateMetadata(NameValueCollection) to update the metadata on the server.
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.- Marked As Answer byMunish Bhargav Wednesday, November 11, 2009 10:16 AM
All Replies
- Hello, you must call GetContainerProperties in order to get the actual LastModifiedTime.
ContainerProperties cp = container.GetContainerProperties();
Now you can access cp.LastModifiedTime. note container.LastModifiedTime will not be updated, so please examine the ContainerProperties.LastModifiedTime property.
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.- Proposed As Answer byAnton Staykov Wednesday, November 04, 2009 8:41 AM
- Marked As Answer byMunish Bhargav Wednesday, November 04, 2009 11:40 AM
- Thanks Yi-Lun Luo for reply,withContainerProperties cp = container.GetContainerProperties();MessageBox.Show(cp.LastModifiedDate.ToString());But I always got the created time of the container.But I Modify the container by adding no. of files after the time of creation but it does not show the Modify time of the container.RegardsMunish.
- The container's LastModifiedDate is only updated when the container itself is changed (such as metadata changes). To get the last modified date of the latest modified blob, you will have to enumerate through all the blobs, and investigate the blobs' LastModifiedDate. Yes, this is inconsistent with Windows file system. I'll pass it to our storage team to see if this behavior can be changed in the future.
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.- Unmarked As Answer byMunish Bhargav Wednesday, November 11, 2009 10:17 AM
- Marked As Answer byMunish Bhargav Wednesday, November 04, 2009 11:40 AM
- Marked As Answer byMunish Bhargav Wednesday, November 11, 2009 10:17 AM
- Hi Yi-Lun Luo,I harshly need Container's LastModifiedDate because enumerate through blobs and check its LastModifiedDatedoesnot work for me.As you mentioned the container's LastModifiedDate is only updated when the container itself is changed (such as metadata changes).so i update the metadata by adding the metadata via.
It do add the metadata and show it bycp.Metadata.Add("id","Modified");
"But it does not update the LastModifiedDate"cp.Metadata.count;
Please Yi-Lun Luo show me the right Way.Thanks - Looks like you haven't updated the metadata on the server. Unfortunately currently the StorageClient library does not expose an UpdateMetadata method. But you can create your own.
First in BlobStorage.cs, find the BlobContainer class, and add an abstract method:
public abstract bool UpdateMetadata(NameValueCollection metadata);
Then in RestBlobStorage.cs, find the BlobContainerRest class, and implement this method. The implementation is very similar to other operations.
public override bool UpdateMetadata(NameValueCollection metadata)
{
bool retval = false;
RetryPolicy(() =>
{
NameValueCollection queryParams = new NameValueCollection();
queryParams.Add(StorageHttpConstants.QueryParams.QueryParamComp, StorageHttpConstants.CompConstants.Metadata);
ResourceUriComponents uriComponents;
Uri uri = Utilities.CreateRequestUri(BaseUri, UsePathStyleUris, AccountName, ContainerName, null, Timeout, queryParams, out uriComponents);
HttpWebRequest request = Utilities.CreateHttpRequest(uri, StorageHttpConstants.HttpMethod.Put, Timeout);
Utilities.AddMetadataHeaders(request, metadata);
credentials.SignRequest(request, uriComponents);
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
retval = true;
}
else if ((response.StatusCode == HttpStatusCode.PreconditionFailed ||
response.StatusCode == HttpStatusCode.NotModified))
{
retval = false;
}
else
{
Utilities.ProcessUnexpectedStatusCode(response);
retval = false;
}
response.Close();
}
}
catch (IOException ioe)
{
throw new StorageServerException(StorageErrorCode.TransportError, "The connection may be lost",
default(HttpStatusCode), ioe);
}
catch (System.TimeoutException te)
{
throw new StorageServerException(StorageErrorCode.ServiceTimeout, "Timeout during blob metadata upload",
HttpStatusCode.RequestTimeout, te);
}
catch (WebException we)
{
if (we.Response != null)
{
HttpWebResponse response = (HttpWebResponse)we.Response;
if ((response.StatusCode == HttpStatusCode.PreconditionFailed ||
response.StatusCode == HttpStatusCode.NotModified))
{
retval = false;
return;
}
}
throw Utilities.TranslateWebException(we);
}
});
return retval;
}
Now in your main code, you can call container.UpdateMetadata(NameValueCollection) to update the metadata on the server.
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.- Marked As Answer byMunish Bhargav Wednesday, November 11, 2009 10:16 AM
- Thank you so much Yi-Lun Luo It really works.I like and prefer the Microsoft only due to the kind of help that Microsoft provide to theiruser. Its awesome. Microsoft is nothing its just people like you that makes it Microsoft.Thanks


