terricain / aioboto3

Wrapper to use boto3 resources with the aiobotocore async backend
Apache License 2.0
732 stars 75 forks source link

How to use Bucket.creation_date #226

Closed dorinclisu closed 3 years ago

dorinclisu commented 3 years ago

Simple attribute access:

async with aioboto3.resource('s3') as s3:
    bucket: Bucket = await s3.Bucket('bucketname')
    print(bucket.creation_date)

RuntimeWarning: coroutine 'AIOBoto3ResourceFactory._create_autoload_property.<locals>.property_loader' was never awaited

Awaiting attribute access:

async with aioboto3.resource('s3') as s3:
    bucket: Bucket = await s3.Bucket('bucketname')
    print(await bucket.creation_date)

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/relay/api/photos.py", line 245, in <module>
    asyncio.run(main())
  File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/relay/api/photos.py", line 234, in main
    logging.debug(await bucket.creation_date)
  File "/usr/local/lib/python3.9/site-packages/aioboto3/resources/factory.py", line 116, in property_loader
    await self.load()
  File "/usr/local/lib/python3.9/site-packages/boto3/s3/inject.py", line 73, in bucket_load
    for bucket_data in response['Buckets']:
TypeError: 'coroutine' object is not subscriptable
sys:1: RuntimeWarning: coroutine 'AioBaseClient._make_api_call' was never awaited
terricain commented 3 years ago

Change to print(await bucket.creation_date)

This is because upon accessing those properties it would synchronously load in the data required, and there wasn't a simple way to achieve that with async. I thought about doing it when you create the bucket, but then again, not everyone wants said attributes.

dorinclisu commented 3 years ago

Look again at my code, I've tried just that and it doesn't work.

terricain commented 3 years ago

ooooh, looks like https://github.com/boto/boto3/blob/develop/boto3/s3/inject.py#L57-L79 will need to be patched, will hopefully work on that this week

terricain commented 3 years ago

Fixed in v8.3.0