Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"400 Bad Request" error when uploading media from an in-memory binary stream in v4.0.0 #1667

Closed
dprgarner opened this issue Sep 26, 2021 · 2 comments
Labels
Invalid This is not valid Question This is a question

Comments

@dprgarner
Copy link

I've recently tried to upgrade an app to use Tweepy v4, but I've been unable to as it throws errors whenever I try to upload media.

I'm attempting to upload a file that's been downloaded from AWS S3 and written to an in-memory binary stream. A simplified version of the code is as follows:

# The AWS S3 Client
s3 = boto3.client("s3")

with io.BytesIO(b"") as in_memory_bytes_io:
    # This downloads an image from S3 and writes the binary bytes to the in-memory IO stream.
    # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.download_fileobj
    s3.download_fileobj("mybucket", "myfile.png", in_memory_bytes_io)

    # With Tweepy v3 this works fine, but with Tweepy v4 this raises a "400 Bad Request" error.
    uploaded_image = api.media_upload(
        filename="test.png",
        file=in_memory_bytes_io,
        chunked=True,
    )

I've ruled out issues with the S3 download method because this approach worked fine with Tweepy v3, and also because I see the same error if I fill in_memory_bytes_io in another way, e.g. if I replaced the s3.download_fileobj with the following:

with open("./mylocalfile.png", "rb") as local_file:
    in_memory_bytes_io.write(local_file.read())

The underlying error from the Twitter API response contains the message "Segments do not add up to provided total file size". I guess changes were made in this version which means that it no longer accurately finds the file's total size?

Any help would be much appreciated.

@Harmon758
Copy link
Member

Tweepy v4.0.0 no longer automatically seeks to the beginning of the file object when uploading media.
This was an intentional change to allow uploading starting from a specific position in the stream.

Since you're writing to the BytesIO instance after it's initialized, the file pointer is at the end after the write, and media_upload is attempting to upload the equivalent of an empty file.
Adding in_memory_bytes_io.seek(0) before the media_upload call should fix this.

@Harmon758 Harmon758 added Invalid This is not valid Question This is a question labels Sep 26, 2021
@dprgarner
Copy link
Author

Seeking to the beginning of the stream fixed the issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Invalid This is not valid Question This is a question
Projects
None yet
Development

No branches or pull requests

2 participants