本文整理汇总了Golang中github.com/pkg/sftp.Create函数的典型用法代码示例。如果您正苦于以下问题:Golang Create函数的具体用法?Golang Create怎么用?Golang Create使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Create函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Example
func Example(conn *ssh.ClientConn) {
// open an SFTP sesison over an existing ssh connection.
sftp, err := sftp.NewClient(conn)
if err != nil {
log.Fatal(err)
}
defer sftp.Close()
// walk a directory
w := sftp.Walk("/home/user")
for w.Step() {
if w.Err() != nil {
continue
}
log.Println(w.Path())
}
// leave your mark
f, err := sftp.Create("hello.txt")
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte("Hello world!")); err != nil {
log.Fatal(err)
}
// check it's there
fi, err := sftp.Lstat("hello.txt")
if err != nil {
log.Fatal(err)
}
log.Println(fi)
}
开发者ID:kr,项目名称:sftp,代码行数:33,代码来源:example_test.go
示例2: copyFile
func (c *DockerDeployClient) copyFile(source string, target string) error {
sftp, err := sftp.NewClient(c.sshClient)
if err != nil {
log.Fatalf("Could not initialize SFTP connection: %v", err)
}
defer sftp.Close()
tf, err := sftp.Create(target)
if err != nil {
return err
}
defer tf.Close()
sf, err := os.Open(source)
if err != nil {
return err
}
defer sf.Close()
n, err := io.Copy(tf, sf)
if err != nil {
return err
}
log.Printf("Artifact from %v to %v copied. %v Bytes transferred.", source, target, n)
return nil
}
开发者ID:herzog31,项目名称:docker-deploy-bamboo-go,代码行数:27,代码来源:client.go
示例3: UploadScript
func (i *blueBoxInstance) UploadScript(ctx gocontext.Context, script []byte) error {
client, err := i.sshClient(ctx)
if err != nil {
return err
}
defer client.Close()
sftp, err := sftp.NewClient(client)
if err != nil {
return err
}
defer sftp.Close()
_, err = sftp.Lstat("build.sh")
if err == nil {
return ErrStaleVM
}
f, err := sftp.Create("build.sh")
if err != nil {
return err
}
_, err = f.Write(script)
if err != nil {
return err
}
return err
}
开发者ID:henrikhodne-abandoned,项目名称:worker,代码行数:30,代码来源:bluebox.go
示例4: Put
func (conn *SshConfig) Put(local, remote string) error {
sftp, err := conn.transport()
if err != nil {
return err
}
defer sftp.Close()
src, err := ioutil.ReadFile(local)
if err != nil {
return err
}
dst, err := sftp.Create(remote)
if err != nil {
return errors.New("Could not create the remote file.")
}
fmt.Printf("%s --> %s:%s\n", local, conn.Name, remote)
if _, err := dst.Write(src); err != nil {
return err
}
return nil
}
开发者ID:kbdhero,项目名称:cogs,代码行数:25,代码来源:ftp.go
示例5: UploadFile
func (c *sshClientImpl) UploadFile(username string, password string, ip string, srcFile string, destFile string) error {
config := &myssh.ClientConfig{
User: username,
Auth: []myssh.AuthMethod{
myssh.Password(password),
},
}
if !IsIP(ip) {
return errors.New("invalid IP address")
}
if IsDir(srcFile) || IsDir(destFile) {
return errors.New("Is a directory")
}
client, err := myssh.Dial("tcp", ip+":22", config)
if err != nil {
log.Fatal(err)
return err
}
defer client.Close()
sftp, err := sftp.NewClient(client)
if err != nil {
log.Fatal(err)
return err
}
defer sftp.Close()
data, err := ioutil.ReadFile(srcFile)
if err != nil {
log.Fatal(err)
return err
}
f, err := sftp.Create(destFile)
if err != nil {
log.Fatal(err)
return err
}
defer f.Close()
_, err = f.Write([]byte(data))
if err != nil {
log.Fatal(err)
return err
}
_, err = sftp.Lstat(destFile)
if err != nil {
log.Fatal(err)
return err
}
return nil
}
开发者ID:ardnaxelarak,项目名称:bosh-softlayer-cpi,代码行数:55,代码来源:ssh_client.go
示例6: main
func main() {
if err := resolveSettings(pars); err != nil {
log.Fatal(err)
}
conn, err := initSSH(pars)
if err != nil {
log.Fatal(err)
}
sftp, err := sftp.NewClient(conn)
if err != nil {
log.Fatal(err)
}
defer sftp.Close()
finStat, err := os.Stat(inPath)
if err != nil {
log.Fatal(err)
}
origSize := finStat.Size()
fin, err := os.Open(inPath)
if err != nil {
log.Fatal(err)
}
defer fin.Close()
outPath := path.Clean(path.Join(pars.DstFolder, filepath.Base(inPath)))
fout, err := sftp.Create(outPath)
if err != nil {
log.Fatal(err)
}
if _, err := io.Copy(fout, fin); err != nil {
log.Fatal(err)
}
foutStat, err := sftp.Lstat(outPath)
if err != nil {
log.Fatal(err)
}
if finalSize := foutStat.Size(); origSize != finalSize {
if err := sftp.Remove(outPath); err != nil {
log.Printf("[ERROR] Unable to remove target file: %v", err)
}
log.Fatalf("Failed to upload %s to %s: expected %d bytes, got %d (missing %d)",
inPath, outPath, origSize, finalSize, origSize-finalSize)
}
fmt.Println("Successfully uploaded", outPath)
}
开发者ID:alexaandru,项目名称:up,代码行数:51,代码来源:main.go
示例7: UploadScript
func (i *jupiterBrainInstance) UploadScript(ctx context.Context, script []byte) error {
client, err := i.sshClient()
if err != nil {
return err
}
defer client.Close()
sftp, err := sftp.NewClient(client)
if err != nil {
return err
}
defer sftp.Close()
_, err = sftp.Lstat("build.sh")
if err == nil {
return ErrStaleVM
}
f, err := sftp.Create("build.sh")
if err != nil {
return err
}
_, err = f.Write(script)
if err != nil {
return err
}
f, err = sftp.Create("wrapper.sh")
if err != nil {
return err
}
_, err = fmt.Fprintf(f, wrapperSh)
return err
}
开发者ID:henrikhodne-abandoned,项目名称:worker,代码行数:37,代码来源:jupiterbrain.go
示例8: deploy_local
func deploy_local(deploy_config setup.DeployConfiguration, commands setup.CommandConfig) {
connection, err := NewConnection(deploy_config.TargetHost, deploy_config.TargetPort, deploy_config.Username, deploy_config.Password, deploy_config.PublicKeyPath)
if err != nil {
log.Panic(err)
}
//If this is the first time deploying, we need to create the path to the target directory
if !CheckTargetDirectory(connection, deploy_config) {
cmd := fmt.Sprintf("mkdir -p %s", deploy_config.TargetDirectory)
ExecuteCmd(connection, cmd, nil, nil)
//cmd = fmt.Sprintf("mkdir -p %s", deploy_config.TargetDirectory+"/current")
//ExecuteCmd(connection, cmd, nil, nil)
}
new_version := create_version(deploy_config)
sftp, err := sftp.NewClient(connection)
if err != nil {
log.Fatal(err)
}
defer sftp.Close()
files := list_local_directory(deploy_config.Ignores)
//TODO use "current" instead of the new_version directory, since we have a symbolic link
for _, file := range files {
content, _ := ioutil.ReadFile(file)
file = strings.Replace(file, `\`, `/`, -1)
target := deploy_config.TargetDirectory + "/" + new_version + "/" + file
tree := deploy_config.TargetDirectory + "/" + new_version + "/"
for _, dir := range strings.Split(file, "/")[:len(strings.Split(file, "/"))-1] {
tree += dir + "/"
log.Printf("Creating directory: %s\n", tree)
err := sftp.Mkdir(tree)
if err != nil {
}
}
log.Printf("Creating file: %s\n", target)
f, err := sftp.Create(target)
if err != nil {
log.Panic(err)
}
if _, err := f.Write([]byte(content)); err != nil {
log.Panic(err)
}
}
}
开发者ID:newxan,项目名称:gopistrano,代码行数:49,代码来源:transfer.go
示例9: copyToRemote
func copyToRemote(c *cli.Context) {
for _, flag := range []string{"keyfile", "to", "host", "user"} {
if c.Generic(flag) == nil {
log.Fatalf("flag %s is required\n", flag)
}
}
// process the keyfile
buf, err := ioutil.ReadFile(c.String("keyfile"))
if err != nil {
log.Fatalf("error in reading private key file %s\n", err)
}
key, err := ssh.ParsePrivateKey(buf)
if err != nil {
log.Fatalf("error in parsing private key %s\n", key)
}
// client config
config := &ssh.ClientConfig{
User: c.String("user"),
Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
}
// connection
client, err := ssh.Dial("tcp", c.String("host")+":22", config)
if err != nil {
log.Fatalf("error in ssh connection %s\n", err)
}
defer client.Close()
// sftp handler
sftp, err := sftp.NewClient(client)
if err != nil {
log.Fatalf("error in sftp connection %s\n", err)
}
defer sftp.Close()
// Remote file
r, err := sftp.Create(filepath.Join(c.String("to"), "go_sftp.txt"))
if err != nil {
log.Fatalf("error in creating remote file %s\n", err)
}
l := strings.NewReader("Writing through golang sftp")
if _, err := io.Copy(r, l); err != nil {
log.Fatalf("error in writing file to remote system %s\n", err)
}
log.Println("written new file to remote system")
}
开发者ID:dictyBase,项目名称:webhooks,代码行数:43,代码来源:scp.go
示例10: Put
// source is the source of the file/folder
// destination is the path of the FOLDER where source will be transferred to
// NOTE: source will keep it original name when transferred
func (sshTransport *SSHTransport) Put(source, destination string, srcType string) error {
client, session, err := sshTransport.getClientSession()
if err != nil {
return HenchErr(err, map[string]interface{}{
"host": sshTransport.Host,
}, fmt.Sprintf("Couldn't dial into %s", sshTransport.Host))
}
defer client.Close()
defer session.Close()
sftp, err := sftp.NewClient(client)
if err != nil {
return HenchErr(err, map[string]interface{}{
"host": sshTransport.Host,
}, "Error creating sftp client")
}
defer sftp.Close()
dstPieces := strings.Split(destination, "/")
// it will always equal home
if dstPieces[0] == "${HOME}" {
dstPieces[0] = "."
}
newDst := strings.Join(dstPieces, "/")
// if the src is a dir
// tar the dir, copy it over, untar it
// remove tar files on both ends
if srcType == "dir" {
// This is done so hosts won't remove tars used by other file
tarredFile := sshTransport.Host + "_" + filepath.Base(source) + ".tar"
// create the local tar
err := tarit(source, tarredFile, nil)
if err != nil {
return HenchErr(err, map[string]interface{}{
"dir": source,
}, "Failed to tar dir to transport")
}
defer os.Remove(tarredFile)
sourceBuf, err := ioutil.ReadFile(tarredFile)
if err != nil {
return HenchErr(err, map[string]interface{}{
"dir": source,
"file": tarredFile,
}, "Failed to read tar")
}
tarredFilePath := filepath.Join(newDst, tarredFile)
f, err := sftp.Create(tarredFilePath)
if err != nil {
return HenchErr(err, map[string]interface{}{
"host": sshTransport.Host,
"file": newDst,
}, "Failed to create remote file")
}
if _, err := f.Write(sourceBuf); err != nil {
return HenchErr(err, map[string]interface{}{
"host": sshTransport.Host,
"file": destination,
"source": source,
}, "Error writing to remote file ")
}
cmd := fmt.Sprintf("tar -xvf %s -C %s && rm -rf %s", tarredFilePath, newDst, tarredFilePath)
_, err = session.CombinedOutput(cmd)
if err != nil {
return HenchErr(err, map[string]interface{}{
"host": sshTransport.Host,
"file": tarredFilePath,
}, "While untarring on remote")
}
} else {
f, err := sftp.Create(filepath.Join(newDst, filepath.Base(source)))
if err != nil {
return HenchErr(err, map[string]interface{}{
"host": sshTransport.Host,
"file": destination,
}, "Failed to create remote file")
}
sourceBuf, err := ioutil.ReadFile(source)
if err != nil {
return HenchErr(err, map[string]interface{}{
"file": source,
}, "")
}
if _, err := f.Write(sourceBuf); err != nil {
return HenchErr(err, map[string]interface{}{
"host": sshTransport.Host,
"file": destination,
//.........这里部分代码省略.........
开发者ID:apigee,项目名称:henchman,代码行数:101,代码来源:ssh.go
示例11:
Expect(err).NotTo(HaveOccurred())
Expect(accepted).To(BeTrue())
})
It("starts an sftp server in write mode", func() {
tempDir, err := ioutil.TempDir("", "sftp")
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tempDir)
sftp, err := sftp.NewClient(client)
Expect(err).NotTo(HaveOccurred())
defer sftp.Close()
By("creating the file")
target := filepath.Join(tempDir, "textfile.txt")
file, err := sftp.Create(target)
Expect(err).NotTo(HaveOccurred())
fileContents := []byte("---\nthis is a simple file\n\n")
_, err = file.Write(fileContents)
Expect(err).NotTo(HaveOccurred())
err = file.Close()
Expect(err).NotTo(HaveOccurred())
Expect(ioutil.ReadFile(target)).To(Equal(fileContents))
By("reading the file")
file, err = sftp.Open(target)
Expect(err).NotTo(HaveOccurred())
开发者ID:benjaminharnett,项目名称:diego-ssh,代码行数:30,代码来源:session_channel_handler_test.go
示例12: Handle
func (r *Resource) Handle() {
Log := log.New(os.Stdout, fmt.Sprintf("%s[%d] ", r.Parent.URL, r.DeviceID), log.Ltime|log.Lshortfile)
for {
time.Sleep(1 * time.Second)
if !r.Parent.Enabled || r.InUse {
continue
}
// Get Job
var jobInstance *JobInstance = nil
select {
case r := <-r.JobQueue:
jobInstance = r
default:
}
if jobInstance == nil {
select {
case a := <-JobQueue:
jobInstance = a
default:
}
}
if jobInstance == nil {
continue
}
r.InUse = true
Log.Println(jobInstance)
if jobInstance.PID == -1 {
// Send Model Data
Log.Println("Uploading Model")
sftp, err := sftp.NewClient(r.Parent.Client)
if err != nil {
Log.Fatal(err)
}
sftp.Mkdir(sftp.Join(r.Parent.WorkingDirectory, "model"))
sftp.Mkdir(sftp.Join(r.Parent.WorkingDirectory, "model", strings.ToLower(jobInstance.Parent.Model.Name)))
for _, file := range jobInstance.Parent.Model.Files {
fIn, err := os.Open(fmt.Sprintf("data/%s/%s", jobInstance.Parent.Model.Name, file))
if err != nil {
Log.Fatalln(err)
}
fOut, err := sftp.Create(sftp.Join(r.Parent.WorkingDirectory, "model", strings.ToLower(jobInstance.Parent.Model.Name), file))
if err != nil {
Log.Fatalln(err)
}
io.Copy(fOut, fIn)
fIn.Close()
fIn.Close()
}
// Setup Job Type Parameters
executable := ""
configuration := ""
if jobInstance.Parent.Template.IsProtoMol() {
executable = "ProtoMol"
configuration = "sim.conf"
} else {
executable = "python"
configuration = "sim.py"
}
// Send Template Data
template, err := jobInstance.Parent.Template.Process(r.DeviceID, *jobInstance.Parent)
if err != nil {
Log.Fatalln(err)
}
sftp.Mkdir(sftp.Join(r.Parent.WorkingDirectory, "job"))
sftp.Mkdir(sftp.Join(r.Parent.WorkingDirectory, "job", strings.ToLower(jobInstance.Parent.Name)))
sftp.Mkdir(sftp.Join(r.Parent.WorkingDirectory, "job", strings.ToLower(jobInstance.Parent.Name), strings.ToLower(fmt.Sprintf("%d", jobInstance.NumberInSequence()))))
fOut, err := sftp.Create(sftp.Join(r.Parent.WorkingDirectory, "job", strings.ToLower(jobInstance.Parent.Name), strings.ToLower(fmt.Sprintf("%d", jobInstance.NumberInSequence())), configuration))
if err != nil {
Log.Fatalln(err)
}
fOut.Write(template)
fOut.Close()
sftp.Close()
// Start job and retrieve PID
Log.Println("Starting Job")
session, err := r.Parent.Client.NewSession()
if err != nil {
Log.Fatalln(err)
}
//.........这里部分代码省略.........
开发者ID:LCLS,项目名称:GPUManager,代码行数:101,代码来源:server_resource.go
示例13: Synchronise
//.........这里部分代码省略.........
// 1. Create all new directories
for _, remoteFileNormalised := range downloadRemoteFiles {
// Skip all files
if !remoteFilesNormalised[remoteFileNormalised].IsDir() {
continue
}
newLocalDir := filepath.Join(localDir, strings.Replace(normalised2remoteFiles[remoteFileNormalised], remoteDir, ``, 1))
log.Printf("Try to create the new local directory %s...\n", newLocalDir)
if mkdirError := os.MkdirAll(newLocalDir, os.ModeDir); mkdirError != nil {
log.Printf("Was not able to create the local directory %s: %s\n", newLocalDir, mkdirError.Error())
}
}
// 2. All new files
for _, remoteFileNormalised := range downloadRemoteFiles {
// Skip all directories
if remoteFilesNormalised[remoteFileNormalised].IsDir() {
continue
}
log.Printf("Try to download the new remote file %s...\n", normalised2remoteFiles[remoteFileNormalised])
remoteFileHandle, remoteFileHandleError := sftp.Open(normalised2remoteFiles[remoteFileNormalised])
if remoteFileHandleError != nil {
log.Printf("Was not able to open the remote file %s: %s\n", normalised2remoteFiles[remoteFileNormalised], remoteFileHandleError.Error())
continue
}
_, filename := filepath.Split(normalised2remoteFiles[remoteFileNormalised])
path, _ := filepath.Split(strings.Replace(normalised2remoteFiles[remoteFileNormalised], remoteDir, ``, 1))
newLocalFile := filepath.Join(localDir, path, filename)
localFileHandle, localFileHandleError := os.Create(newLocalFile)
if localFileHandleError != nil {
log.Printf("Was not able to create the local file %s: %s\n", newLocalFile, localFileHandleError.Error())
remoteFileHandle.Close()
continue
}
_, copyError := io.Copy(localFileHandle, remoteFileHandle)
if copyError != nil {
log.Printf("Was not able to download the new remote file %s to the local file %s: %s\n", normalised2remoteFiles[remoteFileNormalised], newLocalFile, copyError.Error())
remoteFileHandle.Close()
localFileHandle.Close()
continue
}
remoteFileHandle.Close()
localFileHandle.Close()
}
}
}
}
//
// Upload new files ============================================================================================
//
uploadLocalFiles := make([]string, 0)
for localFileNormalised, _ := range localFilesNormalised {
remoteFileNormaliesed := remoteFilesNormalised[localFileNormalised]
if remoteFileNormaliesed == nil {
uploadLocalFiles = append(uploadLocalFiles, localFileNormalised)
}
}
开发者ID:SommerEngineering,项目名称:Sync,代码行数:67,代码来源:Synchronise.go
注:本文中的github.com/pkg/sftp.Create函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论