-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_transfer.go
213 lines (178 loc) · 6.23 KB
/
file_transfer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"context"
"fmt"
"io"
"log"
"net/url"
"os"
"path/filepath"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/pkg/sftp"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
"io/ioutil"
)
// transferFile transfers a file using the specified protocol.
// Supported protocols: azureblob, cifs, sftp, s3, local.
func transferFile(protocol, source, destination string) error {
switch protocol {
case "azureblob":
return transferAzureBlob(source, destination)
case "cifs":
return transferCIFS(source, destination)
case "sftp":
return transferSFTP(source, destination)
case "s3":
return transferS3(source, destination)
case "local":
return transferLocal(source, destination)
default:
return fmt.Errorf("unsupported protocol: %s", protocol)
}
}
// transferAzureBlob uploads a file to Azure Blob Storage.
func transferAzureBlob(source, destination string) error {
log.Println("Transferring via Azure Blob Storage")
accountName := viper.GetString("azure.accountName")
accountKey := viper.GetString("azure.accountKey")
containerName := viper.GetString("azure.containerName")
if accountName == "" || accountKey == "" || containerName == "" {
return fmt.Errorf("Azure storage credentials not provided")
}
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
return fmt.Errorf("failed to create Azure credential: %v", err)
}
pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
url, _ := url.Parse(
fmt.Sprintf("https://%s.blob.core.windows.net/%s", accountName, containerName))
containerURL := azblob.NewContainerURL(*url, pipeline)
blobURL := containerURL.NewBlockBlobURL(filepath.Base(destination))
file, err := os.Open(source)
if err != nil {
return fmt.Errorf("failed to open source file: %v", err)
}
defer file.Close()
ctx := context.Background()
_, err = azblob.UploadFileToBlockBlob(ctx, file, blobURL, azblob.UploadToBlockBlobOptions{})
if err != nil {
return fmt.Errorf("failed to upload file to Azure Blob: %v", err)
}
log.Println("File transferred via Azure Blob Storage successfully")
return nil
}
// transferCIFS copies a file to a CIFS (SMB) network share.
func transferCIFS(source, destination string) error {
log.Println("Transferring via CIFS")
mountPoint := viper.GetString("cifs.mountPoint")
if mountPoint == "" {
return fmt.Errorf("CIFS mount point not configured")
}
destPath := filepath.Join(mountPoint, destination)
return transferLocal(source, destPath)
}
// transferSFTP uploads a file via SFTP.
func transferSFTP(source, destination string) error {
// Read the allowed host key from a file
publicKeyBytes, err := ioutil.ReadFile("allowed_hostkey.pub")
if err != nil {
return fmt.Errorf("failed to read allowed host key: %v", err)
}
// Parse the allowed host key
publicKey, err := ssh.ParsePublicKey(publicKeyBytes)
if err != nil {
return fmt.Errorf("failed to parse allowed host key: %v", err)
}
// SSH client configuration
config := &ssh.ClientConfig{
User: "username",
Auth: []ssh.AuthMethod{
ssh.Password("password"),
},
HostKeyCallback: ssh.FixedHostKey(publicKey),
}
// Connect to the SSH server
sshClient, err := ssh.Dial("tcp", "sftp.example.com:22", config)
if (err != nil) {
return fmt.Errorf("failed to dial: %v", err)
}
defer sshClient.Close()
// Create new SFTP client
sftpClient, err := sftp.NewClient(sshClient)
if (err != nil) {
return fmt.Errorf("failed to create sftp client: %v", err)
}
defer sftpClient.Close()
// Open source file
srcFile, err := os.Open(source)
if (err != nil) {
return fmt.Errorf("failed to open source file: %v", err)
}
defer srcFile.Close()
// Create destination file on the server
dstFile, err := sftpClient.Create(destination)
if (err != nil) {
return fmt.Errorf("failed to create destination file: %v", err)
}
defer dstFile.Close()
// Copy data from source file to destination file
_, err = io.Copy(dstFile, srcFile)
if (err != nil) {
return fmt.Errorf("failed to copy file: %v", err)
}
log.Println("File transferred via SFTP successfully")
return nil
}
// transferS3 uploads a file to Amazon S3.
func transferS3(source, destination string) error {
log.Println("Transferring via Amazon S3")
bucket := viper.GetString("aws.bucket")
region := viper.GetString("aws.region")
if bucket == "" || region == "" {
return fmt.Errorf("AWS S3 bucket or region not configured")
}
sess, err := session.NewSession(&aws.Config{Region: aws.String(region)})
if err != nil {
return fmt.Errorf("failed to create AWS session: %v", err)
}
uploader := s3.New(sess)
file, err := os.Open(source)
if err != nil {
return fmt.Errorf("failed to open source file: %v", err)
}
defer file.Close()
_, err = uploader.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(destination),
Body: file,
})
if err != nil {
return fmt.Errorf("failed to upload to S3: %v", err)
}
log.Println("File transferred via Amazon S3 successfully")
return nil
}
// transferLocal copies a file locally.
func transferLocal(source, destination string) error {
// Copying file from source to destination locally
inputFile, err := os.Open(source)
if err != nil {
return fmt.Errorf("failed to open source file: %v", err)
}
defer inputFile.Close()
outputFile, err := os.Create(destination)
if err != nil {
return fmt.Errorf("failed to create destination file: %v", err)
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
if err != nil {
return fmt.Errorf("failed to copy file: %v", err)
}
log.Println("File transferred locally successfully")
return nil
}