代码之家  ›  专栏  ›  技术社区  ›  Pரதீப்

动态获取Azure Blob属性的值

  •  0
  • Pரதீப்  · 技术社区  · 7 年前

    有没有一种方法可以动态获取Azure Blob的属性而不显式提及它。

    例如,如果我想得到blob的创建日期,那么我需要写类似这样的东西

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Storage Account")
    CloudBlobClient sourceBlobClient = storageAccount.CreateCloudBlobClient();
    
    var sourceContainer = sourceBlobClient.GetContainerReference("Container Name");
    var blockBlob = blobContainer.GetBlockBlobReference("Blob Name");
    
    blockBlob.FetchAttributesAsync().Wait();
    var blobCreatedDate = blockBlob.Properties.Created;
    

    我尽量避免在最后一句话中明确提到“created”。

    有没有实现这一目标的指针?我们可以循环遍历blob的属性吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Pரதீப்    7 年前

    我终于能做到这一点

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Storage Account")
    CloudBlobClient sourceBlobClient = storageAccount.CreateCloudBlobClient();
    
    var sourceContainer = sourceBlobClient.GetContainerReference("Container Name");
    var blockBlob = blobContainer.GetBlockBlobReference("Blob Name");
    
    blockBlob.FetchAttributesAsync().Wait();
    //var blobCreatedDate = blockBlob.Properties.Created;
    var propName = "Created"
    Type tModelType = blockBlob.Properties.GetType();
    var propertyInfo = tModelType.GetProperty(propName);
    If  (propertyInfo != null) {
        var blobCreatedDate = propertyInfo.GetValue(blockBlob.Properties).ToString();
    }