Add structured metadata to upload in SDK , C# not applying

xstever
xstever Member Posts: 7
edited October 14 in Developer APIs

I have my code below, but the metadata is not applying when doing the upload. Can anyone help?

RawUploadParams rawUploadParams = new RawUploadParams();
rawUploadParams.MetadataFields = new StringDictionary();
rawUploadParams.MetadataFields.Add("InternalID", "001");

// Upload an image and log the response to the console
//=================
var uploadParams = new ImageUploadParams()
{
//File = new FileDescription(@"https://cloudinary-devs.github.io/cld-docs-assets/assets/images/cld-sample.jpg"),
File = new FileDescription(newPhoto.FileName, newPhoto.OpenReadStream()),
UseFilename = true,
UniqueFilename = false,
Overwrite = true,
MetadataFields = rawUploadParams.MetadataFields,
Folder = "/" + $"{Guid.NewGuid().ToString()}"
};

//uploadParams.me
var uploadResult = _Cloudinary.Upload(uploadParams);

Tagged:

Comments

  • Tamara
    Tamara Member, Cloudinary Staff Posts: 117
    edited September 22

    Hi @xstever,

    You should use the Context or cloudinary.AddMetadataField(MetadataFieldCreateParams field); fields to apply metadata when uploading instead of MetadataFields. You can read more here.

    Also, you don't need to set RawUploadParams separately. You can directly add the metadata dictionary to uploadParams.

    Give this a try, and your metadata should be applied correctly during the upload process.

    Let me know if you need further assistance!
    Best,

    Tamara

  • xstever
    xstever Member Posts: 7

    Thanks Tamara,

    The Context field worked but the Metadata field says it's deprecated. Will the "Metadata" field populate the structured metadata fields?

    Best,

    Steve

  • Tamara
    Tamara Member, Cloudinary Staff Posts: 117

    Hi @xstever, sorry, I had a syntax error 🙃

    I updated my previous reply, thanks for the heads up!

    Best regards,

    Tamara

  • xstever
    xstever Member Posts: 7

    Thanks Tamara, I'm still having trouble updating the Structural Metadata part. I maybe missing something here

  • xstever
    xstever Member Posts: 7

    I created a Structured Metadata called "UserID", there is no type to populate this with the _Cloudinary.AddMetadataField method to tie it to the upload image. Do I need to upload the image first then make the API call? Hopefully this can done via C#

  • Tom
    Tom Member, Cloudinary Staff Posts: 103

    Hi @xstever ,

    Thanks for replying.

    You can define the metadata while uploading the picture as per https://cloudinary.com/documentation/image_upload_api_reference#upload , see the .NET sample.

    You can also use metadata/explicit API to just update the metadata for existing assets.

    I am not seeing any recent uploads where you've tried to update this structured metadata field either, can you please make a new one and I will check if there was something wrong?

    I look forward to your response. 

    Kind Regards,

    Thomas

  • xstever
    xstever Member Posts: 7
    edited October 5

    Thanks Thomas, but it's not clear how to update a structured metadata field and not a contextual metadata field in C#. Can you give me an example in the comments. I'd like to do it without the API call and C# ( SDK).

    Best,

    Steve

  • Tamara
    Tamara Member, Cloudinary Staff Posts: 117

    Hi Steve,

    To update a structured metadata field, you have two options:

    1. Via the DAM dashboard: You can easily update the field directly through your account dashboard.
    2. Via code: If you'd prefer to handle this programmatically, you can update a structured metadata field using its external ID. Below is an example where we update the metadata field with the ID 'in_stock':
    var metadataFieldUpdateParams = new StringMetadataFieldUpdateParams() {
        Label = "Now in stock",
        Mandatory = true
    };
    cloudinary.UpdateMetadataField("in_stock", metadataFieldUpdateParams);
    

    Let me know if you need further assistance!

    Best regards,

    Tamara

  • xstever
    xstever Member Posts: 7

    Thanks Tamara. But how is the code above targeting the image I need to update the structured metadata fields on? The method is not asking for parameters that related to the image on the fly upload. I feel something is still missing.

    For example below is my code. I have a created a structured metadata that is read-only but can be only populated via code. The field is called "ImageThalloID". I need to populate this with a unique GUID everytime an image is uploaded.

    RawUploadParams rawUploadParams = new RawUploadParams();
    rawUploadParams.MetadataFields = new StringDictionary();
    rawUploadParams.MetadataFields.Add("UserId", $"{Guid.NewGuid().ToString("N")}");

    // Upload an image and log the response to the console
    //=================
    var uploadParams = new ImageUploadParams()
    {

    File = new FileDescription(newPhoto.FileName, newPhoto.OpenReadStream()),
    UseFilename = true,
    UniqueFilename = true,
    Overwrite = true,
    Context = rawUploadParams.MetadataFields

    };

    var uploadResult = _Cloudinary.Upload(uploadParams);

    var metadataFieldUpdateParams = new StringMetadataFieldUpdateParams()
    {
    Label = "ImageThalloID",
    ExternalId = "ImageThalloID",
    Mandatory = true

    };

    var structredMetadata = _Cloudinary.UpdateMetadataField("ImageThalloID", metadataFieldUpdateParams);

    Thanks for your help again!

  • DannyFromCloudinary
    DannyFromCloudinary Member, Cloudinary Staff Posts: 142

    Hi @xstever. Apologies for the confusion. Tamara's answer was specifically around updating the value(s) of a structured metadata field, rather than updating the **** of an uploaded asset.

    If you wanted to set the structured metadata upon upload, you would include metadata as an optional upload parameter, with the structured metadata's unique identifier and the value desired. I don't have a C#/.NET environment, so I haven't tested this, but the upload should look something like this:

    var uploadParams = new ImageUploadParams()
    {
        File = new FileDescription(@"assets/your-image.jpg"),
        Metadata = new Metadata()
        {
            { "sku", "my SKU value" }
        }
    };
    
    var uploadResult = await cloudinary.UploadAsync(uploadParams);
    

    To update the structured metadata of an existing asset, you would need to use the explicit() method of the Upload API: https://cloudinary.com/documentation/image_upload_api_reference#explicit, again specifying the external ID and the value you want it to have, as well as the public ID of the asset you're trying to update. Again, this is untested, but assuming your target public ID is "bike" and you want to update the structured metadata field with ID sku:

    var explicitParams = new ExplicitParams("bike")
    {
        Type = "upload",
        Metadata = new Metadata()
        {
            { "sku", "my new SKU value" }
        }
    };
    
    var explicitResult = await cloudinary.ExplicitAsync(explicitParams);
    Console.WriteLine(explicitResult.JsonObj);
    

    I hope this helps, Please let us know if you have any other questions.

  • xstever
    xstever Member Posts: 7

    Hi Danny,

    No worries…this is exactly what I was looking for. Did a test in C# and this worked perfectly.

    Thank you all for the documentation, just needed the right method I was looking for.

    Best

    Steve

  • DannyFromCloudinary
    DannyFromCloudinary Member, Cloudinary Staff Posts: 142

    You're very welcome Steve! I'm glad I was able to help :)

This discussion has been closed.