下载相机图片功能
This commit is contained in:
1 parent
ff9315ba63
commit
083857d130
13 files changed
+929
-25
No files matched your search
@@ -0,0 +1,127 @@
|
||||
package masterFunc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/pkg/sftp"
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// DownLoadCameraJpgBySftp 使用 SFTP 连接下载远程文件
|
||||
func DownLoadMasterJpgBySftp(id int) error {
|
||||
ip := "192.168.88.2:22" // SFTP 服务器地址 (通常是 SSH 协议端口)
|
||||
username := "pi"
|
||||
password := "picc2008"
|
||||
|
||||
// 配置 SSH 客户端
|
||||
sshConfig := &ssh.ClientConfig{
|
||||
User: username,
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.Password(password),
|
||||
},
|
||||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // 忽略主机密钥验证(不建议用于生产环境)
|
||||
}
|
||||
|
||||
// 连接到 SSH 服务器
|
||||
client, err := ssh.Dial("tcp", ip, sshConfig)
|
||||
if err != nil {
|
||||
fmt.Println("ssh.Dial error:", err)
|
||||
return err
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// 创建 SFTP 客户端
|
||||
sftpClient, err := sftp.NewClient(client)
|
||||
if err != nil {
|
||||
fmt.Println("sftp.NewClient error:", err)
|
||||
return err
|
||||
}
|
||||
defer sftpClient.Close()
|
||||
|
||||
fmt.Println("id:", id)
|
||||
|
||||
// 设定远程目录路径
|
||||
remotePath := "/home/pi/photos/" + fmt.Sprint(id)
|
||||
// 本地目录路径
|
||||
localPathPhoto1 := "./downloads/" + fmt.Sprint(id) + "/photo1"
|
||||
localPathPhoto2 := "./downloads/" + fmt.Sprint(id) + "/photo2"
|
||||
|
||||
// 创建本地目录
|
||||
err = os.MkdirAll(localPathPhoto1, os.ModePerm)
|
||||
if err != nil {
|
||||
fmt.Println("os.MkdirAll error:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(localPathPhoto2, os.ModePerm)
|
||||
if err != nil {
|
||||
fmt.Println("os.MkdirAll error:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// 获取远程目录中的文件和子目录
|
||||
entries, err := sftpClient.ReadDir(remotePath)
|
||||
if err != nil {
|
||||
fmt.Println("sftpClient.ReadDir error:", err)
|
||||
return err
|
||||
}
|
||||
g.Dump("entries", entries, remotePath)
|
||||
|
||||
// 遍历远程目录中的所有文件
|
||||
for _, entry := range entries {
|
||||
localFilePath := ""
|
||||
|
||||
// 判断文件名称是否包含 _1.jpg
|
||||
if strings.Contains(entry.Name(), "_1.jpg") {
|
||||
// 生成本地文件路径
|
||||
localFilePath = filepath.Join(localPathPhoto1, entry.Name())
|
||||
}
|
||||
|
||||
// 判断文件名称是否包含 _8.jpg
|
||||
if strings.Contains(entry.Name(), "_8.jpg") {
|
||||
localFilePath = filepath.Join(localPathPhoto2, entry.Name())
|
||||
}
|
||||
|
||||
// 下载文件
|
||||
err := DownloadFile(sftpClient, remotePath+"/"+entry.Name(), localFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DownloadFile 下载单个文件
|
||||
func DownloadFile(client *sftp.Client, remoteFile, localFile string) error {
|
||||
// 创建本地文件
|
||||
file, err := os.Create(localFile)
|
||||
if err != nil {
|
||||
fmt.Println("os.Create error:", err)
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 打开远程文件
|
||||
remoteFileHandle, err := client.Open(remoteFile)
|
||||
if err != nil {
|
||||
fmt.Println("client.Open error:", err)
|
||||
return err
|
||||
}
|
||||
defer remoteFileHandle.Close()
|
||||
|
||||
// 将文件内容从远程文件复制到本地文件
|
||||
_, err = io.Copy(file, remoteFileHandle)
|
||||
if err != nil {
|
||||
fmt.Println("io.Copy error:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Downloaded file:", localFile)
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user