SQL Server Requires Chilkat v11.7.0+
SQL Server
Box: Upload a Large File in Parts and Commit the Session
See more Box Examples
Run the reusable setup once per HttpCurl object, then select names and execute the operation. The setup is repeated here only to make this published example independently runnable. Resolve documents by name, then perform the explicit chunked-upload session, part, and commit loop. Supports 20 MB through 2 GB using int. Saves recovery information and honors bounded commit Retry-After.
See the Box API guide for setup and the complete workflow.
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
DECLARE @sTmp0 nvarchar(4000)
DECLARE @sTmp1 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Requires Chilkat 11.7.0 or later and a previously unlocked Chilkat bundle.
-- See Global Unlock Sample.
-- Guide: Box API Guide.
SELECT @success = 0
-- BEGIN ONE-TIME SETUP
-- This block is repeated so each published example can run on its own.
-- In an application, run the setup example once and pass the SAME curl object
-- to operation code below. Do not recreate it for every operation.
-- See Reusable Box HttpCurl Setup.
DECLARE @curl int
EXEC @hr = sp_OACreate 'Chilkat.HttpCurl', @curl OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'access_token', 'BOX_ACCESS_TOKEN'
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'parent_name', '/'
-- Box's fixed root ID is an implementation detail; callers supply names.
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'parent_id', '0'
DECLARE @setupCommand int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @setupCommand OUT
DECLARE @setupReplacements int
-- Register findParent; registration sends no request.
EXEC sp_OAMethod @setupCommand, 'SetString', @success OUT, 'curl -X GET "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"'
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @setupCommand, 'Replace', @setupReplacements OUT, 'ACCESS_TOKEN', @sTmp0
EXEC sp_OAMethod @setupCommand, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @curl, 'AddFunction', @success OUT, 'findParent', @sTmp0
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
-- Register findFolder; registration sends no request.
EXEC sp_OAMethod @setupCommand, 'SetString', @success OUT, 'curl -X GET "https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"'
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @setupCommand, 'Replace', @setupReplacements OUT, 'ACCESS_TOKEN', @sTmp0
EXEC sp_OAMethod @setupCommand, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @curl, 'AddFunction', @success OUT, 'findFolder', @sTmp0
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
-- Register findFile; registration sends no request.
EXEC sp_OAMethod @setupCommand, 'SetString', @success OUT, 'curl -X GET "https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"'
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @setupCommand, 'Replace', @setupReplacements OUT, 'ACCESS_TOKEN', @sTmp0
EXEC sp_OAMethod @setupCommand, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @curl, 'AddFunction', @success OUT, 'findFile', @sTmp0
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
-- Register findDestination; registration sends no request.
EXEC sp_OAMethod @setupCommand, 'SetString', @success OUT, 'curl -X GET "https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name" -H "Authorization: Bearer ACCESS_TOKEN"'
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @setupCommand, 'Replace', @setupReplacements OUT, 'ACCESS_TOKEN', @sTmp0
EXEC sp_OAMethod @setupCommand, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @curl, 'AddFunction', @success OUT, 'findDestination', @sTmp0
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
-- Register getFileDetails; registration sends no request.
EXEC sp_OAMethod @setupCommand, 'SetString', @success OUT, 'curl -X GET "https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version" -H "Authorization: Bearer ACCESS_TOKEN"'
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @setupCommand, 'Replace', @setupReplacements OUT, 'ACCESS_TOKEN', @sTmp0
EXEC sp_OAMethod @setupCommand, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @curl, 'AddFunction', @success OUT, 'getFileDetails', @sTmp0
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
-- Register getEnterprise; registration sends no request.
EXEC sp_OAMethod @setupCommand, 'SetString', @success OUT, 'curl -X GET "https://api.box.com/2.0/users/me?fields=enterprise" -H "Authorization: Bearer ACCESS_TOKEN"'
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @setupCommand, 'Replace', @setupReplacements OUT, 'ACCESS_TOKEN', @sTmp0
EXEC sp_OAMethod @setupCommand, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @curl, 'AddFunction', @success OUT, 'getEnterprise', @sTmp0
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
-- Register output rules once. Chilkat 11.7.0 evaluates {{name}} variables
-- when extracting each output, using the current value at that time.
EXEC sp_OAMethod @curl, 'AddOutput2', @success OUT, 'findParent', 'entries', 'name', '{{parent_name}}', 1, 'id', 'parent_id'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
EXEC sp_OAMethod @curl, 'AddOutput2', @success OUT, 'findFolder', 'entries', 'name', '{{folder_name}}', 1, 'id', 'folder_id'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
EXEC sp_OAMethod @curl, 'AddOutput2', @success OUT, 'findFile', 'entries', 'name', '{{file_name}}', 1, 'id', 'file_id'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
EXEC sp_OAMethod @curl, 'AddOutput2', @success OUT, 'findDestination', 'entries', 'name', '{{destination_name}}', 1, 'id', 'destination_id'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
EXEC sp_OAMethod @curl, 'AddOutput', @success OUT, 'getFileDetails', 'file_version.id', 'file_version_id'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
EXEC sp_OAMethod @curl, 'AddOutput', @success OUT, 'getFileDetails', 'sha1', 'file_sha1'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
EXEC sp_OAMethod @curl, 'AddOutput', @success OUT, 'getEnterprise', 'enterprise.id', 'enterprise_id'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @curl, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
RETURN
END
-- END ONE-TIME SETUP
-- The name lookups below assume each listing fits in one page (1000 items).
-- Parent and destination names select root children; folder_name selects a child
-- of parent_name. "/" selects the root parent. These are not arbitrary paths.
-- BEGIN OPERATION -- keep the configured object when reusing it.
-- Inputs are names. Retain cached IDs when the selected names have not changed.
DECLARE @parentName nvarchar(4000)
SELECT @parentName = '/'
DECLARE @folderName nvarchar(4000)
SELECT @folderName = 'documents'
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'parent_name'
EXEC sp_OAMethod @curl, 'VarDefined', @iTmp0 OUT, 'parent_id'
IF (@sTmp0 <> @parentName) or ((@parentName = '/') and (@iTmp0 = 0))
BEGIN
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'parent_name', @parentName
EXEC sp_OAMethod @curl, 'ClearVar', NULL, 'parent_id'
EXEC sp_OAMethod @curl, 'ClearVar', NULL, 'folder_id'
EXEC sp_OAMethod @curl, 'ClearVar', NULL, 'file_id'
EXEC sp_OAMethod @curl, 'ClearVar', NULL, 'file_version_id'
EXEC sp_OAMethod @curl, 'ClearVar', NULL, 'file_sha1'
IF @parentName = '/'
BEGIN
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'parent_id', '0'
END
END
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'folder_name'
IF @sTmp0 <> @folderName
BEGIN
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'folder_name', @folderName
EXEC sp_OAMethod @curl, 'ClearVar', NULL, 'folder_id'
EXEC sp_OAMethod @curl, 'ClearVar', NULL, 'file_id'
EXEC sp_OAMethod @curl, 'ClearVar', NULL, 'file_version_id'
EXEC sp_OAMethod @curl, 'ClearVar', NULL, 'file_sha1'
END
-- Resolve the named destination first. HttpCurl does not expand variables
-- inside multipart -F attributes in the tested build, so this upload is two phases.
EXEC sp_OAMethod @curl, 'ClearTargetOutput', NULL, '*'
DECLARE @authorizedCommand1 int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @authorizedCommand1 OUT
EXEC sp_OAMethod @authorizedCommand1, 'SetString', @success OUT, 'curl -X GET "https://api.box.com/2.0/folders/{{folder_id}}?fields=id" -H "Authorization: Bearer {{access_token}}"'
DECLARE @headerReplacement1 int
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @authorizedCommand1, 'Replace', @headerReplacement1 OUT, '{{access_token}}', @sTmp0
EXEC sp_OAMethod @authorizedCommand1, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @curl, 'DoYourThing', @success OUT, @sTmp0
EXEC sp_OAGetProperty @curl, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) or (@iTmp0 <> 200)
BEGIN
EXEC sp_OAGetProperty @curl, 'StatusCode', @iTmp0 OUT
PRINT 'Box request failed. HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @curl, 'FailReason', @iTmp0 OUT
PRINT 'Failure reason: ' + @iTmp0
EXEC sp_OAGetProperty @curl, 'ResponseBodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
RETURN
END
-- This example limits size to 2 GB so offsets fit all generated languages' integers.
-- The source must not change during hashing or upload. Create qa_output first.
DECLARE @localPath nvarchar(4000)
SELECT @localPath = 'qa_data/large-document.zip'
DECLARE @file int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @file OUT
-- Check the decimal size before converting so an oversized file cannot overflow int.
DECLARE @sizeString nvarchar(4000)
EXEC sp_OAMethod @file, 'FileSizeStr', @sizeString OUT, @localPath
EXEC sp_OAGetProperty @file, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
EXEC sp_OAGetProperty @file, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
RETURN
END
DECLARE @sizeText int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sizeText OUT
EXEC sp_OAMethod @sizeText, 'SetString', @success OUT, @sizeString
DECLARE @sizeMatch int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @sizeMatch OUT
EXEC sp_OAMethod @sizeText, 'RegexMatch', @iTmp0 OUT, '^([0-9]{1,9}|1[0-9]{9}|2000000000)$', @sizeMatch, 100
IF @iTmp0 <> 1
BEGIN
PRINT 'Choose an input between 20 MB and 2 GB for this example.'
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
RETURN
END
DECLARE @fileSize int
SELECT @fileSize = @
IF @fileSize < 20971520
BEGIN
PRINT 'Choose an input between 20 MB and 2 GB for this example.'
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
RETURN
END
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
DECLARE @response int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @response OUT
DECLARE @request int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @request OUT
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'folder_id'
EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'folder_id', @sTmp0
EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'file_name', 'large-document.zip'
EXEC sp_OAMethod @request, 'UpdateInt', @success OUT, 'file_size', @fileSize
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://upload.box.com/api/2.0/files/upload_sessions', @request, 'application/json', @response
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
RETURN
END
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 201
BEGIN
EXEC sp_OAGetProperty @response, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
RETURN
END
DECLARE @session int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @session OUT
EXEC sp_OAGetProperty @response, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @session, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @session, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
RETURN
END
EXEC sp_OAMethod @session, 'WriteFile', @success OUT, 'qa_output/box_upload_session.json'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @session, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
RETURN
END
DECLARE @partSize int
EXEC sp_OAMethod @session, 'IntOf', @partSize OUT, 'part_size'
DECLARE @uploadUrl nvarchar(4000)
EXEC sp_OAMethod @session, 'StringOf', @uploadUrl OUT, 'session_endpoints.upload_part'
DECLARE @commitUrl nvarchar(4000)
EXEC sp_OAMethod @session, 'StringOf', @commitUrl OUT, 'session_endpoints.commit'
IF (@partSize <= 0) or (@uploadUrl = '') or (@commitUrl = '')
BEGIN
PRINT 'Invalid session response.'
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
RETURN
END
DECLARE @hash int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @hash OUT
EXEC sp_OASetProperty @hash, 'HashAlgorithm', 'sha1'
EXEC sp_OASetProperty @hash, 'EncodingMode', 'base64'
DECLARE @fileDigest nvarchar(4000)
EXEC sp_OAMethod @hash, 'HashFileENC', @fileDigest OUT, @localPath
EXEC sp_OAGetProperty @hash, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
EXEC sp_OAGetProperty @hash, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
RETURN
END
EXEC sp_OAMethod @file, 'FileOpen', @success OUT, @localPath, 2147483648, 1, 3, 0
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @file, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
RETURN
END
DECLARE @commit int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @commit OUT
DECLARE @partResponse int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @partResponse OUT
DECLARE @chunk int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @chunk OUT
DECLARE @range int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @range OUT
DECLARE @digestHeader int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @digestHeader OUT
DECLARE @offset int
SELECT @offset = 0
DECLARE @partIndex int
SELECT @partIndex = 0
DECLARE @partLength int
DECLARE @lastByte int
DECLARE @partDigest nvarchar(4000)
WHILE @offset < @fileSize
BEGIN
EXEC sp_OAMethod @chunk, 'Clear', @success OUT
EXEC sp_OAMethod @file, 'FileReadBd', @success OUT, @partSize, @chunk
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @file, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC sp_OAMethod @file, 'FileClose', NULL
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
RETURN
END
EXEC sp_OAGetProperty @chunk, 'NumBytes', @partLength OUT
IF @partLength <= 0
BEGIN
PRINT 'Unexpected end of file.'
EXEC sp_OAMethod @file, 'FileClose', NULL
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
RETURN
END
SELECT @lastByte = @offset + (@partLength - 1)
EXEC sp_OAMethod @range, 'Clear', NULL
EXEC sp_OAMethod @range, 'Append', @success OUT, 'bytes '
EXEC sp_OAMethod @range, 'AppendInt', @success OUT, @offset
EXEC sp_OAMethod @range, 'Append', @success OUT, '-'
EXEC sp_OAMethod @range, 'AppendInt', @success OUT, @lastByte
EXEC sp_OAMethod @range, 'Append', @success OUT, '/'
EXEC sp_OAMethod @range, 'AppendInt', @success OUT, @fileSize
EXEC sp_OAMethod @hash, 'HashBdENC', @partDigest OUT, @chunk
EXEC sp_OAGetProperty @hash, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
EXEC sp_OAGetProperty @hash, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC sp_OAMethod @file, 'FileClose', NULL
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
RETURN
END
EXEC sp_OAMethod @digestHeader, 'SetString', @success OUT, 'sha='
EXEC sp_OAMethod @digestHeader, 'Append', @success OUT, @partDigest
EXEC sp_OAMethod @digestHeader, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Digest', @sTmp0
EXEC sp_OAMethod @range, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Range', @sTmp0
EXEC sp_OAMethod @http, 'HttpBd', @success OUT, 'PUT', @uploadUrl, @chunk, 'application/octet-stream', @response
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) or (@iTmp0 <> 200)
BEGIN
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
PRINT 'Part failed. HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @response, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Inspect the saved session and server list_parts before retrying this part.'
EXEC sp_OAMethod @file, 'FileClose', NULL
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
RETURN
END
EXEC sp_OAGetProperty @response, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @partResponse, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
EXEC sp_OAMethod @file, 'FileClose', NULL
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
RETURN
END
EXEC sp_OASetProperty @commit, 'I', @partIndex
-- Copy the server's acknowledged part fields into the commit manifest.
EXEC sp_OAMethod @partResponse, 'StringOf', @sTmp0 OUT, 'part.part_id'
EXEC sp_OAMethod @commit, 'UpdateString', @success OUT, 'parts[i].part_id', @sTmp0
EXEC sp_OAMethod @partResponse, 'IntOf', @iTmp0 OUT, 'part.offset'
EXEC sp_OAMethod @commit, 'UpdateInt', @success OUT, 'parts[i].offset', @iTmp0
EXEC sp_OAMethod @partResponse, 'IntOf', @iTmp0 OUT, 'part.size'
EXEC sp_OAMethod @commit, 'UpdateInt', @success OUT, 'parts[i].size', @iTmp0
EXEC sp_OAMethod @partResponse, 'StringOf', @sTmp0 OUT, 'part.sha1'
EXEC sp_OAMethod @commit, 'UpdateString', @success OUT, 'parts[i].sha1', @sTmp0
EXEC sp_OAMethod @commit, 'WriteFile', @success OUT, 'qa_output/box_upload_parts.json'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @commit, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC sp_OAMethod @file, 'FileClose', NULL
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
RETURN
END
SELECT @offset = @offset + @partLength
SELECT @partIndex = @partIndex + 1
END
EXEC sp_OAMethod @file, 'FileClose', NULL
EXEC sp_OAMethod @http, 'RemoveRequestHeader', NULL, 'Content-Range'
EXEC sp_OAMethod @digestHeader, 'SetString', @success OUT, 'sha='
EXEC sp_OAMethod @digestHeader, 'Append', @success OUT, @fileDigest
EXEC sp_OAMethod @digestHeader, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Digest', @sTmp0
DECLARE @attempts int
SELECT @attempts = 0
DECLARE @waitSeconds int
DECLARE @retryHeader int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @retryHeader OUT
DECLARE @retryMatch int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @retryMatch OUT
DECLARE @completed int
SELECT @completed = 0
WHILE (@attempts < 10) and (@completed = 0)
BEGIN
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @commitUrl, @commit, 'application/json', @response
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
PRINT 'Commit failed: ' + @iTmp0
EXEC sp_OAGetProperty @response, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
EXEC @hr = sp_OADestroy @retryHeader
EXEC @hr = sp_OADestroy @retryMatch
RETURN
END
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 201
BEGIN
SELECT @completed = 1
END
ELSE
BEGIN
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 202
BEGIN
EXEC sp_OAGetProperty @response, 'StatusCode', @iTmp0 OUT
PRINT 'Unexpected commit status: ' + @iTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
EXEC @hr = sp_OADestroy @retryHeader
EXEC @hr = sp_OADestroy @retryMatch
RETURN
END
-- Box's asynchronous commit response supplies Retry-After in seconds.
EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
EXEC sp_OAMethod @retryHeader, 'SetString', @success OUT, @sTmp0
EXEC sp_OAMethod @retryHeader, 'RegexMatch', @iTmp0 OUT, '^[0-9]{1,2}$', @retryMatch, 100
IF @iTmp0 <> 1
BEGIN
EXEC sp_OAMethod @retryHeader, 'GetAsString', @sTmp0 OUT
PRINT 'Commit pending. Preserve the session and inspect Retry-After before resuming: ' + @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
EXEC @hr = sp_OADestroy @retryHeader
EXEC @hr = sp_OADestroy @retryMatch
RETURN
END
EXEC sp_OAMethod @retryHeader, 'GetAsString', @sTmp0 OUT
SELECT @waitSeconds = EXEC sp_OAMethod @retryHeader, 'GetAsString', @sTmp1 OUT
@
IF (@waitSeconds < 1) or (@waitSeconds > 60)
BEGIN
EXEC sp_OAMethod @response, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Commit pending. Resume commit after Retry-After: ' + @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
EXEC @hr = sp_OADestroy @retryHeader
EXEC @hr = sp_OADestroy @retryMatch
RETURN
END
EXEC sp_OAMethod @http, 'SleepMs', NULL, @waitSeconds * 1000
END
SELECT @attempts = @attempts + 1
END
IF @completed = 0
BEGIN
PRINT 'Commit still pending; retain session and part manifests to resume commit later.'
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
EXEC @hr = sp_OADestroy @retryHeader
EXEC @hr = sp_OADestroy @retryMatch
RETURN
END
EXEC sp_OAGetProperty @response, 'BodyStr', @sTmp0 OUT
PRINT 'Upload committed: ' + @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @setupCommand
EXEC @hr = sp_OADestroy @authorizedCommand1
EXEC @hr = sp_OADestroy @file
EXEC @hr = sp_OADestroy @sizeText
EXEC @hr = sp_OADestroy @sizeMatch
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @response
EXEC @hr = sp_OADestroy @request
EXEC @hr = sp_OADestroy @session
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @commit
EXEC @hr = sp_OADestroy @partResponse
EXEC @hr = sp_OADestroy @chunk
EXEC @hr = sp_OADestroy @range
EXEC @hr = sp_OADestroy @digestHeader
EXEC @hr = sp_OADestroy @retryHeader
EXEC @hr = sp_OADestroy @retryMatch
END
GO