Skip to content

Build and run agents you can see, understand and trust.

fix(rag): measure legacy S3 blobs in their URI bucket (#3598)

haotianqi666b876c3a01071
diff --git a/src/agentscope/app/rag/blob_store/_s3.py b/src/agentscope/app/rag/blob_store/_s3.py
index dbef355e9..05883605d 100644
--- a/src/agentscope/app/rag/blob_store/_s3.py
+++ b/src/agentscope/app/rag/blob_store/_s3.py
@@ -258,12 +258,13 @@ class S3BlobStore(BlobStoreBase):
One ``HEAD`` — the same call :meth:`exists` makes, so a caller
that needs both can ask for the size and treat a number as
- proof of existence.
+ proof of existence. Like :meth:`open`, use the URI's bucket so
+ documents in a legacy bucket remain downloadable after migration.
"""
- key = self._key_from_uri(uri, self._bucket)
+ bucket, key = self._parse_uri(uri)
async with self._client() as s3:
try:
- head = await s3.head_object(Bucket=self._bucket, Key=key)
+ head = await s3.head_object(Bucket=bucket, Key=key)
except Exception: # noqa: BLE001 — see ``exists``
return None
return head.get("ContentLength")
diff --git a/tests/blob_store_s3_test.py b/tests/blob_store_s3_test.py
index 30b3ff1cf..5cd1a4c62 100644
--- a/tests/blob_store_s3_test.py
+++ b/tests/blob_store_s3_test.py
@@ -153,6 +153,29 @@ class S3BlobStoreTest(IsolatedAsyncioTestCase):
await store.delete(uri)
self.assertIsNone(await store.size(uri))
+ async def test_size_uses_the_bucket_in_the_uri(self) -> None:
+ """A legacy blob's measured length must match the bytes open yields."""
+ client = boto3.client(
+ "s3",
+ region_name=_REGION,
+ endpoint_url=self._endpoint_url,
+ )
+ client.create_bucket(Bucket="legacy-blobs")
+ key = "kb/abc/doc-legacy"
+ payload = b"original document in the legacy bucket"
+ client.put_object(Bucket="legacy-blobs", Key=key, Body=payload)
+ uri = f"s3://legacy-blobs/{key}"
+
+ async with self._store() as store:
+ # The same key in the current bucket must not supply the size.
+ await store.write_stream(key, io.BytesIO(b"new"))
+ async with store.open(uri) as stream:
+ self.assertEqual(await stream.read(), payload)
+ self.assertEqual(await store.size(uri), len(payload))
+
+ client.delete_object(Bucket="legacy-blobs", Key=key)
+ self.assertIsNone(await store.size(uri))
+
async def test_bad_uri_scheme_rejected(self) -> None:
"""Non-``s3://`` URIs are refused before any network call."""
async with self._store() as store: