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

S3Boto3StorageFile always returns bytes iterator (not string) #404

Closed
icpetcu opened this issue Oct 9, 2017 · 8 comments
Closed

S3Boto3StorageFile always returns bytes iterator (not string) #404

icpetcu opened this issue Oct 9, 2017 · 8 comments
Labels

Comments

@icpetcu
Copy link

icpetcu commented Oct 9, 2017

When I run this code:

import csv
storage = S3Boto3Storage()
with storage.open('test.csv', 'r') as f:
    reader = csv.reader(f)
    print (list(reader))

I get the following message:

Error: iterator should return strings, not bytes (did you open the file in text mode?)

The problem is that the _file property of S3Boto3StorageFile is always instantiated in 'w+b' mode in order to write the contents from the s3boto3 Object. Therefore, when I read from that file, the iterator always returns bytes, and not string.

I've worked around this problem by using the codecs library:

import codecs
import csv
storage = S3Boto3Storage()
with storage.open('test.csv') as f:
    reader = csv.reader(codecs.iterdecode(f, 'utf-8'))
    print (list(reader))

But I still think this issue should be resolved, since the S3Boto3Storage _open() function has a mode parameter.

@jcushman
Copy link
Contributor

+1, I was bit by this as well. With regular file storage, storage.open('test.csv', 'r').read() returns a unicode str, while storage.open('test.csv', 'rb').read() returns bytestring. With S3Boto3Storage both return bytestring. S3Boto3Storage should follow the same function signature as file storage.

@sww314 sww314 added s3boto and removed s3boto labels Jul 12, 2018
@jschneier
Copy link
Owner

I think we can just pass mode=self._mode to SpooledTemporaryFile.

@nnseva
Copy link
Contributor

nnseva commented Jan 25, 2019

Hi everybody,

I've fixed the issue and made a PR (see above). Using S3Boto3Storage is now transparent, compatible with the Django provided FileStorage and doesn't require additional code. Please push package originator to include a PR into the next release

@nicusX
Copy link

nicusX commented May 21, 2019

Any chance the PR is going to be merged soon?
This is really a blocking issue for many use-cases

@chescales
Copy link

Also blocking for us, any updates? Thanks!

@jschneier
Copy link
Owner

Fixing in #827.

@aquadila
Copy link

aquadila commented Mar 27, 2020

I'm experiencing this very same issue with the GoogleCloudStorage backend. Any chance of a fix there too?

Should I open a new issue perhaps?

@jschneier
Copy link
Owner

Can you open a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants