Skip to content

Commit 6f1de75

Browse files
authored
fix presigned urls + add very necessary test (#864)
1 parent 230ed64 commit 6f1de75

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

py/llama_cloud_services/files/client.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ async def get_file(self, file_id: str) -> File:
3535
file_id, project_id=self.project_id, organization_id=self.organization_id
3636
)
3737

38+
async def read_file_content(self, file_id: str) -> bytes:
39+
presigned_url = await self.client.files.read_file_content(
40+
file_id,
41+
project_id=self.project_id,
42+
organization_id=self.organization_id,
43+
)
44+
httpx_client = self.client._client_wrapper.httpx_client
45+
response = await httpx_client.get(presigned_url.url)
46+
response.raise_for_status()
47+
return response.content
48+
3849
async def upload_file(
3950
self, file_path: str, external_file_id: Optional[str] = None
4051
) -> File:
@@ -67,7 +78,11 @@ async def upload_buffer(
6778
),
6879
)
6980
httpx_client = self.client._client_wrapper.httpx_client
70-
await httpx_client.post(presigned_url.url, data=buffer)
81+
upload_response = await httpx_client.put(
82+
presigned_url.url,
83+
data=buffer.read(),
84+
)
85+
upload_response.raise_for_status()
7186
return await self.client.files.get_file(
7287
presigned_url.file_id,
7388
project_id=self.project_id,

py/tests/test_files_client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,18 @@ async def test_upload_with_default_external_id(file_client: FileClient, test_fil
130130
assert isinstance(uploaded_file, File)
131131
assert uploaded_file.name == os.path.basename(test_file)
132132
assert uploaded_file.external_file_id == test_file
133+
134+
135+
@parametrize_use_presigned_url
136+
@pytest.mark.asyncio
137+
async def test_read_file_content(file_client: FileClient, test_file: str):
138+
"""Test reading a file content"""
139+
# Upload a file first
140+
external_file_id = f"test_read_file_content_{os.getpid()}"
141+
uploaded_file = await file_client.upload_file(test_file, external_file_id)
142+
143+
# Read the file content
144+
file_content = await file_client.read_file_content(uploaded_file.id)
145+
with open(test_file, "rb") as f:
146+
expected_file_content = f.read()
147+
assert file_content == expected_file_content

py/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)