6

I am trying to modify some binary data submitted by user form, and write it to Google Cloud Storage. I tried to follow Google document's example, but upon writing I got errors such as:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 34: ordinal not in range.

My code is simply as below

gcs_file = gcs.open(filename,'w',content_type='audio/mp3')
gcs_file.write(buf)
gcs_file.close()

I tried to open file with 'wb' mode but got a "Invalid mode wb." error.

I found a similar question at GCS's maillist which was on Java. There the GCS develop team's suggest was to use writeChannel.write() instead of PrintWriter. Could anybody suggest how to make it work in Python?

user2686101
  • 352
  • 1
  • 3
  • 13

1 Answers1

7

I suppose the problem is that gcs_file.write() method expects data of type "str". Since type of your buf is "unicode" and apparently contains some Unicode chars (maybe in ID3 tags), you get UnicodeDecodeError. So you just need to encode buf to UTF-8:

gcs_file = gcs.open(filename,'w',content_type='audio/mp3')
gcs_file.write(buf.encode('utf-8'))
gcs_file.close()
Denisigo
  • 640
  • 4
  • 13
  • Adding the `encode` line solved the problem for me. I got error during upload of some of the image files. My error was: `Expected str but got ` I use GAE Python – gsinha May 30 '14 at 18:38
  • @Denisigo. Sorry for coming back late, just found this question remains open. In my case, because I revised app architecture, the coded changed so much that I don't have chance to verify your answer, but it seems quite promising. – user2686101 Oct 04 '14 at 14:30