Python-slack-sdk: Error 'no_file_data' in files_upload method when sending binary data in 'file'

Created on 22 Jun 2020  路  5Comments  路  Source: slackapi/python-slack-sdk

Description

Error 'no_file_data' in files_upload method when sending binary data in 'file'

What type of issue is this? (place an x in one of the [ ])

  • [x] bug
  • [ ] enhancement (feature request)
  • [ ] question
  • [ ] documentation related
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x] I've read and agree to the Code of Conduct.
  • [x] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

slackclient version: 2.6.0 and newer

python version: 3.6

OS version(s): Ubuntu 18.04.4

Steps to reproduce:

  1. read file into python bytes
  2. set bytes into 'file' field in files_upload method params
  3. send request to slack

If I use version 2.5.0 (and earlier) -> bug is not reproduced
Setting file path into 'file' instead of bytes also fixes the problem, but I need to send files not saved on the disk, so this is not an acceptable alternative.

Expected result:

File is uploaded

Actual result:

error:
slack.errors.SlackApiError: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'no_file_data'}

Attachments:

2x bug web-client

All 5 comments

@sofya-salmanova
Thanks for taking the time to report this and I'm sorry for the inconvenience. As you pointed out, this is an incompatibility issue since v2.6.0. We'll be releasing a new patch version including a fix for it at the latest within a few business days.

As a workaround, utilizing io.BytesIO works for you. If you're in a hurry, please try this workaround out.

import logging
logging.basicConfig(level=logging.DEBUG)

from slack import WebClient
import os
client = WebClient(token=os.environ["SLACK_BOT_TOKEN"])

str = "This is a test"
bytes = bytearray(str, "utf-8")

# This doesn't work with 2.6.0+ as mentioned in this issue
res = client.files_upload(file=bytes, filename="test.txt", filetype="text")

# A workaround
import io
bio = io.BytesIO(bytes)
res = client.files_upload(file=bio, filename="test.txt", filetype="text")

@seratch's workaround still required on 2.7.3. Is that expected?

@tplants

This issue is about sending a bytearray object as below. It works already. You don't need to use io.BytesIO anymore.

str = "This is a test"
bytes = bytearray(str, "utf-8")

# This doesn't work with 2.6.0+ as mentioned in this issue
res = client.files_upload(file=bytes, filename="test.txt", filetype="text")

If you're facing a different issue, could you share code reproducing your issue?

Oh my apologies, I'm dealing with the output of io.BytesIO().getValue() in my code, so something like this:

# this is _bytes_ not a _bytearray_
file = io.BytesIO(bytes_like_object).getValue()

And am seeing the same basic error:

# This worked previously but does not work now.
res = client.files_upload(file=file, filename="test.xlsx", filetype="xlsx")

# This works now.
res = client.files_upload(file=io.BytesIO(file), filename="test.xlsx", filetype="xlsx")

@tplants thanks for reporting this! I confirmed the issue and will quickly fix it in the next release coming by the end of this week.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

divyatman picture divyatman  路  4Comments

kompotkot picture kompotkot  路  4Comments

naveenjafer picture naveenjafer  路  4Comments

sushiparlour picture sushiparlour  路  5Comments

marshallino16 picture marshallino16  路  3Comments