Sample code for 30+ languages & platforms
PowerBuilder

Set a Remote File's Owner and Group

See more SFTP Examples

Demonstrates the Chilkat SFtp.SetOwnerAndGroup method, which sets the owner and group of a remote item. The arguments are the remote path (or handle), whether the first argument is a handle, and the new owner and group values.

Background: This is the SFTP equivalent of chown. Changing ownership is a privileged operation on Unix systems — ordinary users generally cannot give their files away — so expect it to fail unless the authenticated account has the necessary rights. It is most often used by administrative jobs that upload files which must then belong to a service account, such as a web server user.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password
integer li_IsHandle

li_Success = 0

//  Demonstrates the SFtp.SetOwnerAndGroup method, which sets the owner and group of a remote item.
//  The 1st argument is the remote path (or handle), the 2nd (isHandle) indicates which, and the
//  3rd and 4th are the new owner and group.

loo_Sftp = create oleobject
li_rc = loo_Sftp.ConnectToNewObject("Chilkat.SFtp")
if li_rc < 0 then
    destroy loo_Sftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Connect, authenticate, and initialize the SFTP subsystem.
li_Port = 22
li_Success = loo_Sftp.Connect("sftp.example.com",li_Port)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Normally you would not hard-code the password in source.  You should instead obtain it
//  from an interactive prompt, environment variable, or a secrets vault.
ls_Password = "mySshPassword"

li_Success = loo_Sftp.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

li_Success = loo_Sftp.InitializeSftp()
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  The 1st argument is a path, so isHandle is 0.  Changing ownership typically requires
//  elevated privileges on the server.
li_IsHandle = 0
li_Success = loo_Sftp.SetOwnerAndGroup("subdir/report.pdf",li_IsHandle,"www-data","www-data")
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

Write-Debug "Owner and group set."

loo_Sftp.Disconnect()


destroy loo_Sftp