Django-storages: S3Boto3StorageFile always returns bytes iterator (not string)

Created on 9 Oct 2017  路  8Comments  路  Source: jschneier/django-storages

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.

s3boto

Most helpful comment

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

All 8 comments

+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.

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

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

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

Also blocking for us, any updates? Thanks!

Fixing in #827.

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?

Can you open a PR?

Was this page helpful?
0 / 5 - 0 ratings