Pull
The Pull
command in the DID Spaces SDK allows you to synchronize data from your DID Space to a local folder, ensuring your local content is updated with the latest versions from the remote. This command focuses on retrieving new or modified files from the DID Space and writing them to your local file system.
For information on pushing local changes to your DID Space, see Push. To understand the foundational concepts of synchronization, refer to Sync.
Usage#
To pull content from your DID Space, you utilize the SyncFolderPullCommand
class. Instantiate it with the necessary input parameters and then call its request()
method.
Parameters#
The SyncFolderPullCommandInput
interface extends SyncFolderBaseCommandInput
and defines the required and optional parameters for a pull operation.
Name | Type | Description |
---|---|---|
|
| Required. The remote path in your DID Space to pull data from. This should be a folder path and typically ends with a |
|
| Required. The local folder path where the data will be pulled to. This folder will be created if it does not exist, and its content will be updated. |
|
| Optional. The maximum number of concurrent file operations (downloads). Defaults to |
|
| Optional. The number of times to retry a failed file operation. Defaults to |
|
| Optional. A function that filters remote objects before synchronization. Only objects for which this function returns |
|
| Optional. If |
|
| Optional. Note: For pull operations, this parameter is currently ignored. The |
|
| Optional. A temporary directory used for intermediate file operations. Defaults to a system-defined temporary directory. |
|
| Optional. A callback function invoked periodically to report the progress of the sync operation. |
|
| Optional. A callback function invoked after each individual object has been successfully pulled (downloaded). |
|
| Optional. A callback function invoked if the entire sync operation fails after all retries are exhausted. |
Returns#
The request()
method of SyncFolderPullCommand
returns a Promise
that resolves to a SyncFolderPullCommandOutput
object, which provides details about the completed operation.
Name | Type | Description |
---|---|---|
|
| The HTTP status code of the overall operation. |
|
| A descriptive message for the operation's status. |
|
| The error stack trace if an error occurred. |
|
| The total number of files that failed to sync. |
|
| The total number of files processed for synchronization (pulled or determined to be up-to-date). |
|
| The total duration of the sync operation in seconds. |
|
| The total size (in bytes) of all objects successfully pulled. |
Example Output
{
"statusCode": 200,
"statusMessage": "OK",
"data": {
"errorCount": 0,
"count": 50,
"duration": 15.3,
"size": 1024000
}
}
Example#
The following example demonstrates how to pull all content from a specific remote folder in your DID Space to a local directory, with progress reporting.
import { SpaceClient } from '@arcblock/did-spaces-client';
import { SyncFolderPullCommand } from '@arcblock/did-spaces-client/commands/sync/sync-folder-pull';
async function pullSpaceContent() {
const client = new SpaceClient({
endpoint: 'https://your-did-space-node.com',
// Add your DID authentication context here (e.g., wallet, signer)
});
const sourceRemotePath = '/my-space-folder/'; // Must end with a slash
const targetLocalPath = './local-data/';
try {
const command = new SyncFolderPullCommand(
{
source: sourceRemotePath,
target: targetLocalPath,
concurrency: 5,
debug: true,
onProgress: async (params) => {
console.log(`Pulling progress: ${params.completed}/${params.total} files completed. Current file: ${params.key}`);
},
onAfterUpload: async (params) => {
console.log(`Successfully pulled: ${params.key}`);
},
onFailed: async (error) => {
console.error('Pull operation failed:', error.message);
}
},
client.context
);
console.log(`Starting pull from ${sourceRemotePath} to ${targetLocalPath}...`);
const result = await command.request();
if (result.statusCode === 200) {
console.log('Pull operation completed successfully!');
console.log(`Total files pulled: ${result.data.count}`);
console.log(`Total data size: ${result.data.size} bytes`);
console.log(`Duration: ${result.data.duration} seconds`);
} else {
console.error(`Pull operation failed: ${result.statusMessage}`);
console.error(result.stack);
}
} catch (error) {
console.error('An unexpected error occurred during pull:', error);
}
}
pullSpaceContent();
This example initializes SpaceClient
and then creates and executes a SyncFolderPullCommand
. It configures a progress callback to log the status of the pull and an onAfterUpload
callback to confirm each file's completion.
How It Works#
The SyncFolderPullCommand
orchestrates a multi-step process to ensure efficient and reliable data synchronization:
- Retrieve Remote Objects: The command first fetches a comprehensive map of all objects and their metadata (key, size, last modified, hash) from the specified
source
path in your DID Space using theListObjectsCommand
. - Retrieve Local Objects: Simultaneously, it scans the
target
local folder to create a similar map of local files and their metadata. - Determine Sync Actions: It then compares the remote and local object maps. For each object, it determines if it needs to be pulled (remote exists, local doesn't; or remote is newer/different in size/hash).
- Execute Pulls: For each object marked for pulling, the command initiates a download from the DID Space using
GetObjectCommand
, streams the content to the corresponding local file, and verifies its integrity if a hash is provided by the server. The process is optimized with a concurrency queue. - Report Progress: Throughout the operation, registered
onProgress
andonAfterUpload
callbacks are triggered to provide real-time updates on the synchronization status.
It's important to note that the SyncFolderPullCommand
is designed to only pull new or updated files. It will not delete local files that are no longer present in the remote DID Space. If you require bidirectional synchronization or deletion of local files that mirror remote deletions, you would need to implement additional logic or use a different sync strategy.
Conclusion#
The SyncFolderPullCommand
is a robust tool for keeping your local data synchronized with your DID Space. By understanding its parameters and operational flow, you can effectively manage the integrity and currency of your local file system.
For more advanced synchronization scenarios, explore the Incremental Sync documentation.