Skip to content

BigQuery upload_from_file unicode file-like must be opened in binary-mode if it's more than RESUMABLE_UPLOAD_THRESHOLD, otherwise str-mode. #1760

Description

@joar

Steps to reproduce

importsysfromgcloud.bigqueryimportClient, SchemaFieldfromgcloud.bigquery.jobimportCreateDisposition, WriteDispositioncsv_filename='sandwiches.csv'iflen(sys.argv) >1:
csv_filename=sys.argv[1]
bq=Client()
ds=bq.dataset('test_unicode')
ds.location='EU'deftest_unicode_upload(filename, mode):
ifnotds.exists():
print('Creating dataset: {}'.format(ds.name))
ds.create()
fields= [
SchemaField('name', 'STRING'),
SchemaField('main_ingredient', 'STRING'),
]
table=ds.table('sandwiches', fields)
print('Uploading CSV: {}, mode={!r}'.format(csv_filename, mode))
table.upload_from_file(
open(filename, mode),
encoding='UTF-8',
source_format='CSV',
write_disposition=WriteDisposition.WRITE_TRUNCATE,
create_disposition=CreateDisposition.CREATE_IF_NEEDED)
test_unicode_upload(csv_filename, 'r') # Workstest_unicode_upload(csv_filename, 'rb')
# Fails in http.client.HTTPConnection()._send_request## /usr/lib/python3.4/http/client.py in _send_request(self, method, url,# body, headers)# 1178 if isinstance(body, str):# 1179 # RFC 2616 Section 3.7.1 says that text default has a# 1180 # default charset of iso-8859-1.# -> 1181 body = body.encode('iso-8859-1')# 1182 self.endheaders(body)## UnicodeEncodeError: 'latin-1' codec can't encode characters in position#649-650: ordinal not in range(256)

sandwiches.csv

name,main_ingredientRäksmörgås,RäkorBaguette,Bröd

Expected behavior

When i send in a binary-mode file-like I expect upload_from_file to pass the data through to BigQuery as-is, and that the BigQuery load job will decode it for me using encoding=.

Enviroment

$ python --versionPython 3.4.3+
$ pip freeze | egrep 'httplib2|gcloud'gcloud==0.13.0httplib2==0.9.2

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the BigQuery API.api: storageIssues related to the Cloud Storage API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions