From my understanding, you wanted to upload an object with custom metadata and then retrieve it once needed.
The custom metadata won't be added if you pass your key-value pairs into upload()
like this:
metadata_dict = {
"foo" : "bar",
"color": "green"
}
The reason is because the library is looking for the metadata
key within the dictionary. See code snippet here. I reviewed the library and in this case, the request body becomes:
{"foo": "bar", "color": "green", "name": "filename.txt"}
Where the correct one must be:
{"metadata": {"foo": "bar", "color": "green"}, "name": "filename.txt"}
The solution is to configure your dictionary like this:
meta_dict = {
"metadata":{
"foo" : "bar",
"color": "green"
}
}
Full code example (credits to the original answer):
import asyncio
import aiohttp
# pip install aiofile
from aiofile import AIOFile
# pip install gcloud-aio-storage
from gcloud.aio.storage import Storage
import logging
import sys
BUCKET_NAME = 'BUCKET_NAME'
FILE_NAME = 'FILE_NAME.txt'
meta_dict = {
"metadata":{
"foo" : "bar",
"color": "green"
}
}
async def async_upload_to_bucket(blob_name, file_obj, meta_dict):
async with aiohttp.ClientSession() as session:
storage = Storage(session=session)
status = await storage.upload(BUCKET_NAME, blob_name, file_obj, metadata=meta_dict)
print(status['metadata'])
return status['selfLink']
async def main():
async with AIOFile(FILE_NAME, mode='r') as afp:
f = await afp.read()
url = await async_upload_to_bucket(FILE_NAME, f, meta_dict)
print(url)
# Python 3.7+
asyncio.run(main())
print("success")