Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

Upload files to share point without credentials stored in the package

$
0
0

I have few ETLs that upload files to share point using a script task(VB code). My code looks like:

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO
Imports System.Net<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

    Enum ScriptResults
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    End Enum


    Public Sub Main()
        ' Script entry-point, this will simply set the file variables
        ' then call a sub-routine to transfer the file
        Try
            Dim localFile As String = CStr(Dts.Variables("SPSourceFile").Value)
            Dim remoteFile As String = CStr(Dts.Variables("SPDestFile").Value)

            UploadDocument(localFile, remoteFile)

            Dts.TaskResult = ScriptResults.Success
        Catch
            ' Catch the exception & return a failure result
            Debug.WriteLine(Err.Description)
            Dts.TaskResult = ScriptResults.Failure
        End Try

    End Sub

    Public Sub UploadDocument(ByVal localFile As String, _
                ByVal remoteFile As String)
        ' Read the local file into a byte stream array
        ' the file contents will be copied from the source into memory in the 'filecontents' object
        ' further details about the stream object can be found here:
        ' http://msdn.microsoft.com/en-us/magazine/cc163710.aspx
        Dim r As Byte()
        Dim stream As System.IO.FileStream = New System.IO.FileStream(localFile, _
                    System.IO.FileMode.Open, System.IO.FileAccess.Read)
        Dim reader As System.IO.BinaryReader = New System.IO.BinaryReader(stream)
        Dim filecontents As Byte() = reader.ReadBytes(CInt(stream.Length))
        reader.Close()
        stream.Close()

        ' Set the variable for the target client, server name & document library specifically
        Dim sSPURL As String = CStr(Dts.Variables("SPServerName").Value)
        Dim sDocLib As String = CStr(Dts.Variables("SPDocLibrary").Value)
        Dim sDocLibFolder As String = CStr(Dts.Variables("SPDocLibraryFolder").Value)
        Dim sRemoteFileURL As String

        ' Get the current credentials, this allows windows authetication
        ' credentials to run an unattended admin account in a sql agent job without storing ant passwords
        Dim NC As New NetworkCredential
        NC = System.Net.CredentialCache.DefaultCredentials

        ' Set the remote url for the webclient object
        ' and do some string parsing to set a valid url
        sRemoteFileURL = sSPURL & "/" & sDocLib & sDocLibFolder & _"/" & Trim(LTrim(RTrim(remoteFile)))
        sRemoteFileURL = Replace(sRemoteFileURL, " ", "%20")
        sRemoteFileURL = Replace(sRemoteFileURL, "\", "/")

        ' Create a webclient object to 
        ' send the byte stream to a remote url
        ' set the credential in the web client object
        ' then call the upload method to send the byte stream
        Dim m_WC As WebClient = New WebClient
        m_WC.Credentials = NC
        r = m_WC.UploadData(sRemoteFileURL, "PUT", filecontents)

    End Sub

End Class

This code works perfect. The only problem is, since the credentials were retained from cache and not hard coded whenever the server is rebooted(happens over the weekend night) the credentials were flushed and all the ETLs using the script to upload files to share point fails. Can someone suggest a workaround?


Viewing all articles
Browse latest Browse all 24688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>