Ask a questionAsk a question
 

AnswerHow to add LastModifiedTime to Blob Container.

  • Wednesday, November 04, 2009 7:36 AMMunish Bhargav Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    As we know we have the LastModifiedTime Property with Blob Container. But Whenever I 
    trying to retrieve the container.LastModifiedTime property it always returned {1/1/0001 12:00:00 AM}.
    So I want to know that 
    how to add the LastModifiedTime Property of the blobContainer so that we can retrieve the Last Modified Time
    of the blobContainer whenever required.
    Regards 
    Munish Bhargav
           

Answers

  • Wednesday, November 04, 2009 8:29 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Wednesday, November 04, 2009 10:50 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Wednesday, November 11, 2009 9:06 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.

All Replies

  • Wednesday, November 04, 2009 8:29 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Wednesday, November 04, 2009 9:29 AMMunish Bhargav Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Yi-Lun Luo for reply,
    with 
    ContainerProperties 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.

    Regards
    Munish. 
  • Wednesday, November 04, 2009 10:50 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Wednesday, November 11, 2009 8:49 AMMunish Bhargav Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi Yi-Lun Luo,
    I harshly need Container's LastModifiedDate because enumerate through blobs and check its LastModifiedDate
    doesnot 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.
    cp.Metadata.Add("id","Modified"); 
    
    It do add the metadata and show it by 
    cp.Metadata.count;
    
    "But it does not update the LastModifiedDate"
    Please Yi-Lun Luo show me the right Way.
    Thanks  
  • Wednesday, November 11, 2009 9:06 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Wednesday, November 11, 2009 10:16 AMMunish Bhargav Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 their 
    user. Its awesome. Microsoft is nothing its just people like you that makes it Microsoft.
    Thanks