0

I am trying to upload an excel and pdf file from local disk to google cloud storage . Excel is creating using pandas library in python. When i try to upload the generated file it is giving this error

'ascii' codec can't decode byte 0xd0 in position 217: ordinal not in range(128) 

I am using flexible appengine. Here is the upload file code

def upload_to_gcs(file_path,file_name,file_type,bucket_name):
try:
    import google.cloud.storage
    storage_client = google.cloud.storage.Client.from_service_account_json(
        os.getcwd()+'relative_path_of_service_json_file')
    bucket = storage_client.get_bucket('bucket_name')
    d = bucket.blob(bucket_name+'/'+file_name+'.'+file_type)
    d.upload_from_filename(file_path)
except Exception, e:
        print str(e)

Thanks in advance

Maria
  • 89
  • 2
  • 10

1 Answers1

0

The error is saying that there's a character that can't be translated to ASCII. Normally, this kind of errors appear due to special characters in the file name or due to how the file is encoded. Check if there's any character that ASCII might be having issues with in the file or the file name.

Here's another StackOverflow question about the same error. In this question the OP is using gsutil command, but at the end both the Python Client Library and the command do the same thing, call the Cloud Storage API, so the solution can be the same for both.

Other SO answers to similar errors tackle the solutions programatically. Using decode("utf-8") or decode(encoding='unicode-escape'). This solutions can also help in your case, you should give them a try.

If you still can't find the root cause of the issue, try uploading the file through the API directly. This way you can check if the issue is still present without using the Python Client Library.

Guillermo Cacheda
  • 2,047
  • 10
  • 21
  • But the same function is working fine when I run independently. When I run from localhost or from server API , it is throwing this error. – Maria Nov 11 '18 at 05:17
  • @Maria what do you mean by "run independently"? When you run the script outside GCP or GAE? If that's the case I don't know why it would work independently, but the error is still most likely due to an issue with a character. There are more SO releted questions with answer that deal with the issue programatically ([here](https://stackoverflow.com/questions/20906948/google-app-engine-unicodedecodeerror-ascii-codec-cant-decode-byte-0xe2-in-p) and [here](https://stackoverflow.com/questions/31000262/unicodedecodeerror-ascii-codec-cant-decode-byte-0xe2-in-position-14-ordinal)) – Guillermo Cacheda Nov 11 '18 at 10:22
  • @Maria just realized that the reason why it works for you outside GAE might be due to Python's version. Maybe the Python version you are using locally is not the same you are using in GAE – Guillermo Cacheda Nov 11 '18 at 10:28