Skip to content

Conversation

hzhreal
Copy link

@hzhreal hzhreal commented Oct 15, 2025

Summary

The point of this PR is to offer an option for developers to download attachments in chunks instead of processing the whole file in memory at once to reduce possible overhead.

Changes:

  • Add the method HTTPClient.get_from_cdn_stream that yields data in chunks
  • Add the method Attachment.read_chunked to process data in chunks
  • Modify the method Attachment.save giving the option to write data to a file in chunks through an optional parameter

Information

  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed).
  • This PR is not a code change (e.g. documentation, README, typehinting,
    examples, ...).

Checklist

  • I have searched the open pull requests for duplicates.
  • If code changes were made then they have been tested.
  • I have updated the documentation to reflect the changes.
  • If type: ignore comments were used, a comment is also left explaining why.
  • I have updated the changelog to include these changes.

@hzhreal hzhreal requested a review from a team as a code owner October 15, 2025 20:45
@pycord-app
Copy link

pycord-app bot commented Oct 15, 2025

Thanks for opening this pull request!
Please make sure you have read the Contributing Guidelines and Code of Conduct.

This pull request can be checked-out with:

git fetch origin pull/2956/head:pr-2956
git checkout pr-2956

This pull request can be installed with:

pip install git+https://github.com/Pycord-Development/pycord@refs/pull/2956/head

@hzhreal
Copy link
Author

hzhreal commented Oct 15, 2025

Test code:

import discord
import os
import aiofiles

bot = commands.Bot()

CHUNKSIZE = 1024 * 1024

@bot.slash_command()
async def download(ctx: discord.ApplicationContext, f: discord.Attachment) -> None:
    size = f.size
    written = 0
    progress = "{}/{}"
    await ctx.respond(progress.format(written, size))
    async with aiofiles.open("test1.bin", "wb") as out:
        async for chunk in f.read_chunked(chunksize=CHUNKSIZE):
            await out.write(chunk)
            chunksize = len(chunk)
            written += chunksize
            await ctx.edit(content=progress.format(written, size))
    await f.save("test2.bin", chunksize=chunksize)

bot.run(os.getenv("TOKEN"))

@Paillat-dev Paillat-dev added hold: changelog This pull request is missing a changelog entry hold: testing This pull request requires further testing priority: low Low Priority labels Oct 15, 2025
@hzhreal hzhreal requested review from a team as code owners October 16, 2025 12:59
@Paillat-dev
Copy link
Member

@hzhreal Could you please fix the merge conflicts ? Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hold: changelog This pull request is missing a changelog entry hold: testing This pull request requires further testing PA: All Contributors pending PA: Maintainers pending priority: low Low Priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants