Zig
Zig
AWS Transfer for SFTP (Amazon S3)
See more SFTP Examples
Once you've setup your AWS Transfer for SFTP in the AWS Console, interacting with it is no different than any other SSH/SFTP server. AWS will provide a private key in PEM format. It is used for authentication (instead of a password).Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const sftp = try chilkat.SFtp.init();
defer sftp.deinit();
// Connect to the AWS SFTP server.
// Change the domain to your value.
const domain = "s-123456df999999fab.server.transfer.eu-west-2.amazonaws.com";
const port = 22;
sftp.connect(domain, port) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
// Load your AWS SFTP private key PEM file..
const ssh_key = try chilkat.SshKey.init();
defer ssh_key.deinit();
const key_text = ssh_key.loadText(alloc, "qa_data/pem/s3_sftp_privateKey.pem") catch {
std.debug.print("{s}\n", .{try ssh_key.getLastErrorText(alloc)});
return;
};
ssh_key.fromOpenSshPrivateKey(key_text) catch {
std.debug.print("{s}\n", .{try ssh_key.getLastErrorText(alloc)});
return;
};
// Authenticate with the SSH server using the private key.
sftp.authenticatePk("myUsername", ssh_key) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
// After authenticating, the SFTP subsystem must be initialized:
sftp.initializeSftp() catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
// Upload from the local file to the SSH server.
// Important -- the remote filepath is the 1st argument,
// the local filepath is the 2nd argument;
const remote_file_path = "hamlet.xml";
const local_file_path = "c:/temp/hamlet.xml";
sftp.uploadFileByName(remote_file_path, local_file_path) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
std.debug.print("Success.\n", .{});
}