SQL Server Google Drive: Download a File (Stream to the Filesystem)

Back to Index

Downloads the content of a file by ID. (In this example the file id = 1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl) The file is streamed directly to the filesystem. Note: The alt=media query param must be used to download the file content (as opposed to the file metadata).

Documentation: https://developers.google.com/drive/v3/reference/files/get


CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    --   Provide a previously obtained OAuth2 access token.
    DECLARE @oauth2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.OAuth2', @oauth2 OUT

    EXEC sp_OASetProperty @oauth2, 'AccessToken', 'OAUTH2_ACCESS_TOKEN'
    EXEC sp_OAMethod @rest, 'SetAuthOAuth2', @success OUT, STR(@oauth2)

    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'www.googleapis.com', 443, 1, 1
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        RETURN
      END

    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'includeTeamDriveItems', 'true'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'supportsTeamDrives', 'true'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'alt', 'media'

    --  First send the HTTP request.
    EXEC sp_OAMethod @rest, 'SendReqNoBody', @success OUT, 'GET', '/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl'
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        RETURN
      END

    --  Read the response header.  If the response status code is success, stream to the file.
    --  Otherwise receive the error response text.
    DECLARE @statusCode int
    EXEC sp_OAMethod @rest, 'ReadResponseHeader', @statusCode OUT
    IF STR(@statusCode) < 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        RETURN
      END
    IF STR(@statusCode) <> 200
      BEGIN

        PRINT 'Received error response code: ' + STR(@statusCode)
        --  Read the error response body.
        DECLARE @sbErrResponse int
        EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbErrResponse OUT

        EXEC sp_OAMethod @rest, 'ReadRespSb', @success OUT, STR(@sbErrResponse)
        IF STR(@success) <> 1
          BEGIN
            EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @oauth2
            EXEC @hr = sp_OADestroy @sbErrResponse
            RETURN
          END

        EXEC sp_OAMethod @sbErrResponse, 'GetAsString', @sTmp0 OUT
        PRINT 'Error response:' + @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        EXEC @hr = sp_OADestroy @sbErrResponse
        RETURN
      END
    --  Stream the response body to the output file.
    DECLARE @respBodyStream int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Stream', @respBodyStream OUT

    EXEC sp_OASetProperty @respBodyStream, 'SinkFile', '/someDirectory/penguins.jpg'
    EXEC sp_OAMethod @rest, 'ReadRespBodyStream', @success OUT, STR(@respBodyStream), 1
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        EXEC @hr = sp_OADestroy @sbErrResponse
        EXEC @hr = sp_OADestroy @respBodyStream
        RETURN
      END


    PRINT 'Example Completed.'

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @oauth2
    EXEC @hr = sp_OADestroy @sbErrResponse
    EXEC @hr = sp_OADestroy @respBodyStream


END
GO