目录

Updating an Asset (C/C++)

Available APIs

You can use OH_Asset_Update to update an asset.

The following table describes the attributes for updating an asset.

In the following table, the attributes ASSET_TAG_ALIAS and those starting with ASSET_TAG_DATA_LABEL are custom asset attributes reserved for services. These attributes are not encrypted. Therefore, do not put sensitive personal data in these attributes.

Example

Before updating an asset, ensure that the asset exists. For details about how to add an asset, see Adding an Asset. Otherwise, the NOT_FOUND error (24000002) is reported.

Update asset demo_alias as follows: change the asset plaintext to demo_pwd_new and additional attribute to demo_label_new.

For details about how to update an asset in a group, see Updating an asset in a Group.

  1. Link the dynamic libraries in the CMake script.

    target_link_libraries(entry PUBLIC libasset_ndk.z.so)
  2. Include header files.

    #include "napi/native_api.h"
    #include <string.h>
    #include "asset/asset_api.h"
  3. Update an asset.

    static napi_value UpdateAsset(napi_env env, napi_callback_info info)
    {
        const char *aliasStr = "demo_alias";
        const char *secretStr = "demo_pwd_new";
        const char *labelStr = "demo_label_new";
    
        Asset_Blob alias = {(uint32_t)(strlen(aliasStr)), (uint8_t *)aliasStr};
        Asset_Blob new_secret = {(uint32_t)(strlen(secretStr)), (uint8_t *)secretStr};
        Asset_Blob new_label = {(uint32_t)(strlen(labelStr)), (uint8_t *)labelStr};
        Asset_Attr query[] = {{.tag = ASSET_TAG_ALIAS, .value.blob = alias }};
        Asset_Attr attributesToUpdate[] = {
            {.tag = ASSET_TAG_SECRET, .value.blob = new_secret},
            {.tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = new_label},
        };
    
        int32_t updateResult = OH_Asset_Update(query, sizeof(query) / sizeof(query[0]), attributesToUpdate,
                                               sizeof(attributesToUpdate) / sizeof(attributesToUpdate[0]));
        napi_value ret;
        napi_create_int32(env, updateResult, &ret);
        return ret;
    }