SQL Server
SQL Server
Resume an Interrupted SFTP Download
See more SFTP Examples
Demonstrates the Chilkat SFtp.ResumeDownloadFileByName method, which resumes an interrupted download. The first argument is the remote file path and the second is the local file path. Chilkat uses the existing local file size as the remote starting offset.
Note: This example uses relative local paths, which are resolved against the application's current working directory. Absolute local paths may also be used. Supply the paths appropriate to your own environment.
Background: The download counterpart to resuming an upload: it compares the local file size against the remote and fetches only what is missing — a full download if the local file is absent or empty, the remaining bytes appended if it is shorter, and nothing if it is already complete. As with resumed uploads, the check is size-based and assumes the partial file is a correct prefix of the remote file.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the SFtp.ResumeDownloadFileByName method, which resumes an interrupted download.
-- The 1st argument is the remote file path and the 2nd is the local file path.
DECLARE @sftp int
EXEC @hr = sp_OACreate 'Chilkat.SFtp', @sftp OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Connect, authenticate, and initialize the SFTP subsystem.
DECLARE @port int
SELECT @port = 22
EXEC sp_OAMethod @sftp, 'Connect', @success OUT, 'sftp.example.com', @port
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sftp
RETURN
END
-- Normally you would not hard-code the password in source. You should instead obtain it
-- from an interactive prompt, environment variable, or a secrets vault.
DECLARE @password nvarchar(4000)
SELECT @password = 'mySshPassword'
EXEC sp_OAMethod @sftp, 'AuthenticatePw', @success OUT, 'mySshLogin', @password
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sftp
RETURN
END
EXEC sp_OAMethod @sftp, 'InitializeSftp', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sftp
RETURN
END
-- Resume downloading the remote file. Chilkat uses the existing local file size as the remote
-- starting offset, so only the remaining bytes are appended. If the local file is missing or
-- empty, a normal full download is performed.
EXEC sp_OAMethod @sftp, 'ResumeDownloadFileByName', @success OUT, 'subdir/largefile.zip', 'qa_output/largefile.zip'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sftp
RETURN
END
PRINT 'Download resumed and completed.'
EXEC sp_OAMethod @sftp, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @sftp
END
GO