SQL Server
SQL Server
Demonstrate S3_DownloadBytes
Demonstrates how to download a file into memory from the Amazon S3 service.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 @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat HTTP API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @http, 'AwsAccessKey', 'AWS_ACCESS_KEY'
EXEC sp_OASetProperty @http, 'AwsSecretKey', 'AWS_SECRET_KEY'
DECLARE @bucketName nvarchar(4000)
SELECT @bucketName = 'chilkat.qa'
DECLARE @objectName nvarchar(4000)
SELECT @objectName = 'images/sea_creatures/starfish.jpg'
DECLARE @localFilePath nvarchar(4000)
SELECT @localFilePath = 'qa_output/starfish.jpg'
EXEC sp_OAMethod @http, 'S3_DownloadBytes', @jpgBytes OUT, @bucketName, @objectName
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
RETURN
END
PRINT 'Download successful.'
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
EXEC sp_OAMethod @fac, 'WriteEntireFile', @success OUT, @localFilePath, @jpgBytes
PRINT 'File saved success = ' + @success
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @fac
END
GO