Skip to main content

SFTP

SFTP services support. (only works on unix)

caution

Maximum number of file holdings is depending on the remote system configuration.

For example, the default value is 255 in macOS, and 1024 in linux. If you want to open lots of files, you should pay attention to close the file after using it.

Capabilities

This service can be used to:

  • stat
  • read
  • write
  • append
  • create_dir
  • delete
  • copy
  • rename
  • list
  • scan
  • presign
  • blocking

Configuration

  • endpoint: Set the endpoint for connection. The format is same as openssh, using either [user@]hostname or ssh://[user@]hostname[:port]. A username or port that is specified in the endpoint overrides the one set in the builder (but does not change the builder).
  • root: Set the work directory for backend. It uses the default directory set by the remote sftp-server as default
  • user: Set the login user
  • key: Set the public key for login
  • known_hosts_strategy: Set the strategy for known hosts, default to Strict
  • enable_copy: Set whether the remote server has copy-file extension

For security reasons, it doesn't support password login, you can use public key or ssh-copy-id instead.

You can refer to [SftpBuilder]'s docs for more information

Example

Via Builder

use anyhow::Result;
use opendal::services::Sftp;
use opendal::Operator;

#[tokio::main]
async fn main() -> Result<()> {
let mut builder = Sftp::default();

builder.endpoint("127.0.0.1").user("test").key("test_key");

let op: Operator = Operator::new(builder)?.finish();
Ok(())
}

Via Config

use anyhow::Result;
use opendal::Operator;
use opendal::Scheme;
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<()> {
let mut map = HashMap::new();
map.insert("endpoint".to_string(), "127.0.0.1".to_string());
map.insert("user".to_string(), "test".to_string());
map.insert("key".to_string(), "test_key".to_string());

let op: Operator = Operator::via_map(Scheme::Sftp, map)?;
Ok(())
}