Skip to content

Compatibility with aiofiles #376

Open
@hwong557

Description

@hwong557

aiofiles is a Python project to perform IO asynchronously. I am able to asynchronously read files from s3, but I cannot write files to s3. The following code example fails:

import asyncio

import aiofiles
import aiohttp
from cloudpathlib import S3Path


async def main():
    chunk_size = 1000

    path = S3Path("s3://my-bucket/image.png")

    async with aiohttp.ClientSession() as session:
        async with session.get("http://httpbin.org/image/png") as resp:
            async with aiofiles.open(path, "wb") as f:
                async for chunk in resp.content.iter_chunked(chunk_size):
                    await f.write(chunk)


asyncio.run(main())

The error is:

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpq4ls9jhw/....../image.png'

I am using cloudpathlib==0.16.0 and aiofiles==23.2.1.

If I add a path.touch() before initializing the ClientSession, the code runs without error, but the file in s3 is zero bytes.

Thank you for your work on such a clean and easy to use library! It truly is a joy!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions