Hitachi Content Platform​

 View Only

 Dot net (core) references and documentation

Ramesh Tamma's profile image
Ramesh Tamma posted 09-26-2023 14:55

I am new to HCP platform and we are in Dot net technology suite. More specifically, I was looking for examples and reference documentation on "HCP Metadata query API" to retrieve metadata on an object. I see mostly Java based examples, can someone provide .net based examples?

Thanks in advance,

Ramesh T

Benjamin Clifford's profile image
Benjamin Clifford

Hi Ramesh, we are always happy to have new members of the HCP community, welcome! I suspect what you are looking for is just standard S3, not the Metadata Query Engine API (MQE). MQE provides some advanced capabilities to search a bucket based on the object's metadata, or even the object contents. It will also allow you to search for objects by operation, like "show me all objects PUT in <bucket> since <time>". For MQE API requests I understand that the RestSharp package is a good general purpose REST API client.

Viewing an object's metadata is a simple S3 HEAD request on the object key. For this I would strongly encourage you to look at the AWSSDK.S3 package. You can add it to your project with the NuGet package manager. 

Here is some sample C# code to list all objects and any custom metadata (x-amz-meta-*). You can tweak it if there is other metadata you are interested in.

using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using Amazon.S3;
using Amazon.S3.Model;

namespace S3_C__HCP
{
  class Program
  {
    public static void Main(string[] args)
    {
       const String userName = "";
      const String password = "";
      const String hcpTenantName = "";
      const String hcpHost = "hcp.example.com";
      const bool useSSL = true; 

      String accessKey = getAccessKey(userName);
      String secretKey = getSecretKey(password);

      try {
        AmazonS3Config config = getConfig(hcpTenantName, hcpHost, useSSL);
        AmazonS3Client client = new AmazonS3Client(accessKey, secretKey, config);
        listObjects(client);
      } catch (AmazonS3Exception e) {
        Console.WriteLine("\n\n" + e);
      }
      Console.ReadKey(true);
    }

    private static void getObjectMetadata(AmazonS3Client client, String bucketName, String objKey)
    {
      GetObjectMetadataRequest request = new GetObjectMetadataRequest();
      request.BucketName = bucketName;
      request.Key = objKey;
      MetadataCollection x_amz_MD = client.GetObjectMetadata(request).Metadata;
      IEnumerator x_amz_MD_Keys = x_amz_MD.Keys.GetEnumerator();
      while (x_amz_MD_Keys.MoveNext())
      {
        String x_amz_Key = x_amz_MD_Keys.Current;
        String x_amz_Val = x_amz_MD[x_amz_Key];
        Console.WriteLine("Custom x-amz MD: " + x_amz_Key + ": " + x_amz_Val);
      }
    }

    private static void listObjects(AmazonS3Client client)
    {
      ListBucketsResponse buckets = client.ListBuckets();
      List.Enumerator buckEnum = buckets.Buckets.GetEnumerator();
      while (buckEnum.MoveNext())
      {
        S3Bucket bucket = buckEnum.Current;
        Console.WriteLine(bucket.BucketName);
        List.Enumerator objresponse = client.ListObjects(bucket.BucketName).S3Objects.GetEnumerator();
        while (objresponse.MoveNext())
        {
          S3Object s3obj = objresponse.Current;
          Console.WriteLine("\t" + s3obj.Key);
          getObjectMetadata(client, bucket.BucketName, s3obj.Key);
        }
      }
    }

    private static AmazonS3Config getConfig(String tenantName, String hcpHost, bool useSSL)
        {
      AmazonS3Config config = new AmazonS3Config();
      if (useSSL) {
        // If using self signed certificates, trust the certificate
        ServicePointManager.ServerCertificateValidationCallback = ((sender, cert, chain, errors) => cert.Subject.Contains(hcpHost));
        config.ServiceURL = "https://" + tenantName + "." + hcpHost + "/";
      } else {
        config.ServiceURL = "http://" + tenantName + "." + hcpHost + "/";
        config.UseHttp = true;
      }
      return config;
    }

    /// <summary>Converts user name to HCP's S3 Access Key format</summary>
    private static String getAccessKey(String userName)
    {
      return Convert.ToBase64String(Encoding.UTF8.GetBytes(userName));
    }
    /// <summary>Converts password to HCP's S3 Secret Key format</summary>
    private static String getSecretKey(String password)
    {
      MD5 md5 = System.Security.Cryptography.MD5.Create();
      return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(password))).Replace("-", String.Empty).ToLower();
    }
  }
}