From 4bd3d1ddf21196b075bbd84cdcb553c5d7355b93 Mon Sep 17 00:00:00 2001 From: cojenco Date: Fri, 22 Jul 2022 05:28:12 -0700 Subject: [PATCH] docs: open file-like objects in byte mode for uploads (#824) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit File-like objects should be opened in binary mode for `blob.upload_from_file()` - cpython standard library accorded with [RFC 2616 Section 3.7.1](https://datatracker.ietf.org/doc/html/rfc2616#section-3.7.1) states the text default charset of iso-8859-1 - add clarifying notes in docstring - update code sample Fixes #818 🦕 --- google/cloud/storage/blob.py | 2 +- samples/snippets/storage_upload_from_stream.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/google/cloud/storage/blob.py b/google/cloud/storage/blob.py index f47c09181..205d4aeb2 100644 --- a/google/cloud/storage/blob.py +++ b/google/cloud/storage/blob.py @@ -2456,7 +2456,7 @@ def upload_from_file( to that project. :type file_obj: file - :param file_obj: A file handle open for reading. + :param file_obj: A file handle opened in binary mode for reading. :type rewind: bool :param rewind: diff --git a/samples/snippets/storage_upload_from_stream.py b/samples/snippets/storage_upload_from_stream.py index e2d31a5e3..08eb25889 100644 --- a/samples/snippets/storage_upload_from_stream.py +++ b/samples/snippets/storage_upload_from_stream.py @@ -25,8 +25,8 @@ def upload_blob_from_stream(bucket_name, file_obj, destination_blob_name): # The stream or file (file-like object) from which to read # import io - # file_obj = io.StringIO() - # file_obj.write("This is test data.") + # file_obj = io.BytesIO() + # file_obj.write(b"This is test data.") # The desired name of the uploaded GCS object (blob) # destination_blob_name = "storage-object-name"