Result Storage
The Apify SDK has several result storage types that are useful for specific tasks. The data is stored either on local disk to a directory defined by the
APIFY_LOCAL_STORAGE_DIR
environment variable, or on the Apify platform under the user account
identified by the API token defined by the APIFY_TOKEN
environment variable. If neither of these variables is defined, by default Apify SDK sets
APIFY_LOCAL_STORAGE_DIR
to ./apify_storage
in the current working directory and prints a warning.
Typically, you will be developing the code on your local computer and thus set the APIFY_LOCAL_STORAGE_DIR
environment variable. Once the code is
ready, you will deploy it to the Apify platform, where it will automatically set the APIFY_TOKEN
environment variable and thus use cloud storage. No
code changes are needed.
Related links
- Apify platform storage documentation
- View storage in Apify app
- Key-value stores API reference
- Datasets API reference
Key-value store
The key-value store is used for saving and reading data records or files. Each data record is represented by a unique key and associated with a MIME content type. Key-value stores are ideal for saving screenshots of web pages, PDFs or to persist the state of crawlers.
Each actor run is associated with a default key-value store, which is created exclusively for the actor run. By convention, the actor run input
and output is stored in the default key-value store under the INPUT
and OUTPUT
key, respectively. Typically the input and output is a JSON file,
although it can be any other format.
In the Apify SDK, the key-value store is represented by the KeyValueStore
class. In order to simplify access to the default
key-value store, the SDK also provides Apify.getValue()
and
Apify.setValue()
functions.
In local configuration, the data is stored in the directory specified by the APIFY_LOCAL_STORAGE_DIR
environment variable as follows:
{APIFY_LOCAL_STORAGE_DIR}/key_value_stores/{STORE_ID}/{KEY}.{EXT}
Note that {STORE_ID}
is the name or ID of the key-value store. The default key value store has ID default
, unless you override it by setting the
APIFY_DEFAULT_KEY_VALUE_STORE_ID
environment variable. The {KEY}
is the key of the record and {EXT}
corresponds to the MIME content type of the
data value.
The following code demonstrates basic operations of key-value stores:
// Get actor input from the default key-value store
const input = await Apify.getInput();
// Write actor output to the default key-value store.
await Apify.setValue('OUTPUT', { myResult: 123 });
// Open a named key-value store
const store = await Apify.openKeyValueStore('some-name');
// Write record. JavaScript object is automatically converted to JSON,
// strings and binary buffers are stored as they are
await store.setValue('some-key', { foo: 'bar' });
// Read record. Note that JSON is automatically parsed to a JavaScript object,
// text data returned as a string and other data is returned as binary buffer
const value = await store.getValue('some-key');
// Delete record
await store.setValue('some-key', null);
To see a real-world example of how to get the input from the key-value store, see the Screenshots example.