How to save/map uploaded image in term field of type image with storage location equals s3:// I'm having 1 field in a taxonomy = 'features' of type image. Image storage location is set to s3.

I have written a hook_form_alter for form_id = article (article is the content type)  where I have created an image field like:

$form['image_dir'] = [
    '#type'                 =&gt; 'managed_file',
    '#upload_location'      =&gt; 's3://s3-bucket',
    '#multiple'             =&gt; FALSE,
    '#description'          =&gt; t('Allowed extensions: gif png jpg jpeg'),
    '#upload_validators'    =&gt; [
      'file_validate_is_image'      =&gt; array(),
      'file_validate_extensions'    =&gt; array('gif png jpg jpeg'),
      'file_validate_size'          =&gt; array(25600000)
    ],

     ];


Now in my form submission, I'm trying to save/map this image into my term image field (field_vocab_image) . For this, I'm trying something like:

$term = Term::create([
    'name' =&gt; $term_name,
    'vid' =&gt; 'my_vocab_name',
    'field_vocab_image' =&gt; $img,
  ]);
  $inserted = $term-&gt;save();


where $img = $form_state-&gt;getvalues()['image_dir']; this image is coming from the custom field created in hook_form_alter.

Above code is successfully saving term name but when I go to edit term , image field is empty. Image is not getting mapped.
An Image type field should contain a file fid of an image file. The following should resolve your issue:

$img = $form_state-&gt;getValue('image_dir');

if (!empty($img['fids'])) {
  $img = reset($img['fids']);
}