How to set a field of an asset from within a plugin I am using the following code to save an asset that has been downloaded to the storage/runtime/temp directory from an external API

$response = craft()-&gt;assets-&gt;insertFileByLocalPath(
    $tempPath,
    $photo['FileName'],
    $folderId,
    AssetConflictResolution::Replace
);

// if the response is a success, get the file id
if ($response &amp;&amp; $response-&gt;isSuccess()) {
    $fileIds[] = $response-&gt;getDataItem('fileId');
}


In this example $photo[] array is the response from the api with all photo information. What I need to do is also save $photo['PhotoId'] to a field I have setup on the photo asset source in Craft.

I can't however find an example of how to achieve this. Does anyone know how?
To save a custom field value on an Asset, you'll first need to pull the actual AssetFileModel, set the custom attribute on the Asset's ContentModel, and finally re-save the Asset.

The following example assumes you've set a field photoId on your asset source:

$fileId = $response-&gt;getDataItem('fileId');
$asset = craft()-&gt;assets-&gt;getFileById($fileId);
$asset-&gt;getContent()-&gt;setAttribute('photoId', $photo['PhotoId']);
$success = craft()-&gt;assets-&gt;storeFile($asset);