Perl
Chilkat2 Perl
SFTP Upload and Download to a StringBuilder Object
See more SFTP Examples
Demonstrates how to SFTP upload from a Chilkat StringBuilder object, and download into a StringBuilder object.Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
my $sftp = Chilkat2::SFtp->new;
# Set some timeouts, in milliseconds:
$sftp->ConnectTimeoutMs(5000);
$sftp->IdleTimeoutMs(10000);
# Connect to the SSH server and then authenticate.
my $port = 22;
my $success = $sftp->Connect('MY-SSH-SERVER-DOMAIN-OR-IP', $port);
if ($success) {
$success = $sftp->AuthenticatePw('MY-SSH-LOGIN', 'MY-SSH-PASSWORD');
if ($success) {
$success = $sftp->InitializeSftp();
}
}
if (!$success) {
print $sftp->LastErrorText, "\n";
exit;
}
my $sbA = Chilkat2::StringBuilder->new;
$sbA->LoadFile('qa_data/hamlet.xml', 'utf-8');
# Upload the contents of sbA to the SSH/SFTP server.
my $bIncludeBOM = 0;
my $remotePath = 'sftpTesting/hamlet.xml';
$success = $sftp->UploadSb($sbA, $remotePath, 'utf-8', $bIncludeBOM);
if (!$success) {
print $sftp->LastErrorText, "\n";
exit;
}
# Download the file..
my $sbB = Chilkat2::StringBuilder->new;
$success = $sftp->DownloadSb($remotePath, 'utf-8', $sbB);
if (!$success) {
print $sftp->LastErrorText, "\n";
exit;
}
# Verify that sbA and sbB have the exact same contents.
print 'size of sbA: ', $sbA->Length, "\n";
my $bCaseSensitive = 1;
if ($sbA->ContentsEqualSb($sbB, $bCaseSensitive)) {
print "Contents are equal. Success.\n";
} else {
print "Contents are NOT equal. Failed.\n";
}
$sftp->Disconnect();