Search code examples
google-apigoogle-cloud-storagepython-asyncioaiohttp

How can I update an object metadata through Google Cloud Storage using Python aiohttp PATCH request?


I'm trying to update an object metadata through Google Cloud Storage using Python aiohttp PATCH request, code attempt as follow.

async def send_init_value_map(sequence_name):
    """Update a blob's metadata."""
    initial_value_map = {"metadata", "value": {'test':0}}
    async with ClientSession() as session:
        await session.patch(url=api_url, headers=headers, data=initial_value_map)
    return initial_value_map

The url and headers in the patch request are good, as they worked in GET request. However, I'm not sure if I'm passing the data field right, the change didn't take effect.

Info on PATCH within aiohttp documentation is limited, as the format for data field input is not specified. enter image description here

I'm not going with the Google API Client option because I need to use aiohttp for async function implementation.

Thank you in advance for you inputs.


Solution

  • I had to convert the data dictionary to json first. After that, the data was successfully updated.

    async def send_init_value_map(sequence_name):
    """Update a blob's metadata."""
    initial_value_map = {"metadata", "value": {'test':0}}
    async with ClientSession() as session:
        await session.patch(url=api_url, headers=headers, data=json.dumps(initial_value_map))
    return initial_value_map