Disini saya akan menggunakan aplikasi SFTP yang biasa saya
gunakan untuk meremote file yaitu WinSCP.
- Download WinSCP Disini
- Install pada komputer
- Remote Server menggunakan WinSCP untuk
mendapatkan SSH-RSA Key guna otentikasi nantinya. Formatnya seperti di bawah
inissh-rsa 2048
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
- Jika sudah login, masuk ke menu “Command” lalu
Server/Protocol Information
Upload File Linux Server VB .NET 1 - 1. Kita pindah ke Visual Studio, buka project lalu masuk ke Tools à Library Package Manager àPackage ManagerAkan tampil gambar seperti di bawah ini
- 1. Install kembali WinSCP dengan mengetikan “Install-Package WinSCP” (Pastikan koneksi internet berjalan baik dan tunggu hingga selesai)Jika telah selesai maka kita tinggal masuk ke coding yang akan digunakan
Public Function upload_file_to_server(ByVal filename As String)
Try
Dim mySessionOptions As New SessionOptions
With mySessionOptions
.Protocol = Protocol.Sftp
.HostName = "HOSTNAME SERVER"
.UserName = "USER"
.Password = "PASSWORD"
'Isikan langkah nomor 4 kesini
.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
End With
Using mySession As Session = New Session
' Connect
mySession.Open(mySessionOptions)
Dim stamp As String = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture)
Dim remotePath As String = "/var/www/html/foto/"
Dim localPath As String = filename
' Manual "remote to local" synchronization.
' You can achieve the same using:
' mySession.SynchronizeDirectories( _
' SynchronizationMode.Local, localPath, remotePath, False, False, SynchronizationCriteria.Time, _
' New TransferOptions With { .FileMask = fileName }).Check
If mySession.FileExists(remotePath) Then
Dim upload As Boolean
If Not File.Exists(localPath) Then
Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath)
upload = True
Else
Dim remoteWriteTime As DateTime = mySession.GetFileInfo(remotePath).LastWriteTime
Dim localWriteTime As DateTime = File.GetLastWriteTime(localPath)
If remoteWriteTime > localWriteTime Then
Console.WriteLine( _
"File {0} as well as local backup {1} exist, " & _
"but remote file is newer ({2}) than local backup ({3})", _
remotePath, localPath, remoteWriteTime, localWriteTime)
upload = True
Else
Console.WriteLine( _
"File {0} as well as local backup {1} exist, " & _
"but remote file is not newer ({2}) than local backup ({3})", _
remotePath, localPath, remoteWriteTime, localWriteTime)
upload = False
End If
End If
If upload Then
Dim myTransferOptions As New TransferOptions
myTransferOptions.TransferMode = TransferMode.Binary
Dim transferResult As TransferOperationResult
transferResult = mySession.PutFiles(filename, remotePath, False, myTransferOptions)
transferResult.Check()
MsgBox("Upload berhasil.", MsgBoxStyle.Information, "Server Information")
End If
Else
MsgBox("File {0} Belum Ada.", MsgBoxStyle.Exclamation, "Peringatan")
Console.WriteLine("File {0} Belum Ada.", remotePath)
End If
End Using
Return 0
Catch ex As Exception
Console.WriteLine("Error: {0}", ex)
MsgBox(ex.Message)
Return 1
End Try
End Function
0 komentar:
Posting Komentar