Skip to content

Commit 71745b0

Browse files
authored
Fix #1292 files_upload_v2 does not work with io.BytesIO file parameters (#1294)
1 parent db17415 commit 71745b0

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

integration_tests/web/test_files_upload_v2.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import os
33
import unittest
4+
from io import BytesIO
45

56
import pytest
67

@@ -44,12 +45,12 @@ def test_uploading_text_files(self):
4445
)
4546
self.assertIsNotNone(upload)
4647

47-
def test_uploading_text_files_legacy(self):
48-
client = self.legacy_client
49-
file = __file__
48+
def test_uploading_bytes_io(self):
49+
client = self.sync_client
5050
upload = client.files_upload_v2(
5151
channels=self.channel_id,
52-
file=file,
52+
file=BytesIO(bytearray("This is a test!", "utf-8")),
53+
filename="test.txt",
5354
title="Test code",
5455
)
5556
self.assertIsNotNone(upload)

slack_sdk/web/internal_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import warnings
88
from asyncio import Future
99
from http.client import HTTPResponse
10+
from io import IOBase
1011
from ssl import SSLContext
1112
from typing import Any, Dict, Optional, Sequence, Union
1213
from urllib.parse import urljoin
@@ -314,8 +315,14 @@ def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[A
314315
if isinstance(file, str): # filepath
315316
with open(file.encode("utf-8", "ignore"), "rb") as readable:
316317
data = readable.read()
317-
else:
318+
elif isinstance(file, bytes):
318319
data = file
320+
elif isinstance(file, IOBase):
321+
data = file.read()
322+
if isinstance(data, str):
323+
data = data.encode()
324+
else:
325+
raise SlackRequestError("file parameter must be any of filepath, bytes, and io.IOBase")
319326
elif content is not None:
320327
if isinstance(content, str):
321328
data = content.encode("utf-8")

0 commit comments

Comments
 (0)