Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix: `KVS.getPublicUrl()` reads the public URL directly from storage client (#3286)

Jindřich Bär34d579b9fdf3
diff --git a/docs/upgrading/upgrading_v4.md b/docs/upgrading/upgrading_v4.md
index 35292bd78..2a378d0e1 100644
--- a/docs/upgrading/upgrading_v4.md
+++ b/docs/upgrading/upgrading_v4.md
@@ -107,3 +107,6 @@ The `CrawlingContext.response` property is now of type [`Response`](https://deve
The crawling context in the `FileDownload` crawler no longer includes the `body` and `stream` properties. These can be accessed directly via the `response` property instead, e.g. `context.response.bytes()` or `context.response.body`.
+## `KeyValueStore.getPublicUrl` is now async
+
+The `KeyValueStore.getPublicUrl` method is now asynchronous and reads the public URL directly from the storage client.
diff --git a/packages/core/src/crawlers/error_snapshotter.ts b/packages/core/src/crawlers/error_snapshotter.ts
index 427a53043..441597cc8 100644
--- a/packages/core/src/crawlers/error_snapshotter.ts
+++ b/packages/core/src/crawlers/error_snapshotter.ts
@@ -88,9 +88,9 @@ export class ErrorSnapshotter {
return {
screenshotFileName,
- screenshotFileUrl: screenshotFileName && keyValueStore.getPublicUrl(screenshotFileName),
+ screenshotFileUrl: screenshotFileName && (await keyValueStore.getPublicUrl(screenshotFileName)),
htmlFileName,
- htmlFileUrl: htmlFileName && keyValueStore.getPublicUrl(htmlFileName),
+ htmlFileUrl: htmlFileName && (await keyValueStore.getPublicUrl(htmlFileName)),
};
} catch {
return {};
diff --git a/packages/core/src/storages/key_value_store.ts b/packages/core/src/storages/key_value_store.ts
index 9f1deda29..1752eef23 100644
--- a/packages/core/src/storages/key_value_store.ts
+++ b/packages/core/src/storages/key_value_store.ts
@@ -573,10 +573,13 @@ export class KeyValueStore {
/**
* Returns a file URL for the given key.
+ *
+ * If the record does not exist or has no associated file path (i.e., it is not stored as a file), returns `undefined`.
+ *
+ * @param key The key of the record to generate the public URL for.
*/
- getPublicUrl(key: string): string {
- const name = this.name ?? this.config.get('defaultKeyValueStoreId');
- return `file://${process.cwd()}/storage/key_value_stores/${name}/${key}`;
+ async getPublicUrl(key: string): Promise<string | undefined> {
+ return this.client.getRecordPublicUrl(key);
}
/**
diff --git a/packages/memory-storage/src/fs/key-value-store/fs.ts b/packages/memory-storage/src/fs/key-value-store/fs.ts
index aaa55d9cd..8bf024add 100644
--- a/packages/memory-storage/src/fs/key-value-store/fs.ts
+++ b/packages/memory-storage/src/fs/key-value-store/fs.ts
@@ -33,8 +33,9 @@ export class KeyValueFileSystemEntry implements StorageImplementation<InternalKe
file = await readFile(this.filePath);
} catch {
try {
+ const noExtFilePath = resolve(this.storeDirectory, this.rawRecord.key);
// Try without extension
- file = await readFile(resolveWithinDirectory(this.storeDirectory, this.rawRecord.key));
+ file = await readFile(noExtFilePath);
memoryStorageLog.warning(
[
`Key-value entry "${this.rawRecord.key}" for store ${basename(
@@ -44,6 +45,7 @@ export class KeyValueFileSystemEntry implements StorageImplementation<InternalKe
].join('\n'),
);
file = file.toString('utf-8');
+ this.filePath = noExtFilePath;
} catch {
// This is impossible to happen, but just in case
throw new Error(`Could not find file at ${this.filePath}`);
@@ -55,6 +57,7 @@ export class KeyValueFileSystemEntry implements StorageImplementation<InternalKe
return {
...this.rawRecord,
value: file,
+ filePath: this.filePath,
};
}
diff --git a/packages/memory-storage/src/resource-clients/key-value-store.ts b/packages/memory-storage/src/resource-clients/key-value-store.ts
index 1b0343681..f7d66f145 100644
--- a/packages/memory-storage/src/resource-clients/key-value-store.ts
+++ b/packages/memory-storage/src/resource-clients/key-value-store.ts
@@ -40,6 +40,7 @@ export interface InternalKeyRecord {
value: Buffer | string;
contentType?: string;
extension: string;
+ filePath?: string;
}
export class KeyValueStoreClient extends BaseClient {
@@ -329,6 +330,27 @@ export class KeyValueStoreClient extends BaseClient {
};
}
+ /**
+ * Generates a public file:// URL for accessing a specific record in the key-value store.
+ *
+ * Returns `undefined` if the record does not exist or has no associated file path (i.e., it is not stored as a file).
+ * @param key The key of the record to generate the public URL for.
+ */
+ async getRecordPublicUrl(key: string): Promise<string | undefined> {
+ s.string().parse(key);
+
+ // Check by id
+ const existingStoreById = await findOrCacheKeyValueStoreByPossibleId(this.client, this.name ?? this.id);
+
+ if (!existingStoreById) {
+ this.throwOnNonExisting(StorageTypes.KeyValueStore);
+ }
+
+ const storageEntry = await existingStoreById.keyValueEntries.get(key)?.get();
+
+ return storageEntry?.filePath;
+ }
+
/**
* Tests whether a record with the given key exists in the key-value store without retrieving its value.
*
diff --git a/packages/types/src/storages.ts b/packages/types/src/storages.ts
index c061b9d79..fc5879020 100644
--- a/packages/types/src/storages.ts
+++ b/packages/types/src/storages.ts
@@ -176,6 +176,7 @@ export interface KeyValueStoreClient {
values?(options?: KeyValueStoreClientListOptions): AsyncIterable<unknown> & Promise<unknown[]>;
entries?(options?: KeyValueStoreClientListOptions): AsyncIterable<[string, unknown]> & Promise<[string, unknown][]>;
recordExists(key: string): Promise<boolean>;
+ getRecordPublicUrl(key: string): Promise<string | undefined>;
getRecord(key: string, options?: KeyValueStoreClientGetRecordOptions): Promise<KeyValueStoreRecord | undefined>;
setRecord(record: KeyValueStoreRecord, options?: KeyValueStoreRecordOptions): Promise<void>;
deleteRecord(key: string): Promise<void>;