init
commit
1bdaf41c4b
|
@ -0,0 +1,8 @@
|
|||
module go-s3-upload
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go v1.50.7 // indirect
|
||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||
)
|
|
@ -0,0 +1,16 @@
|
|||
github.com/aws/aws-sdk-go v1.50.7 h1:odKb+uneeGgF2jgAerKjFzpljiyZxleV4SHB7oBK+YA=
|
||||
github.com/aws/aws-sdk-go v1.50.7/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
|
||||
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
|
||||
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
|
||||
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
|
||||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
@ -0,0 +1,69 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
if len(os.Args) != 7 {
|
||||
fmt.Println("Usage: go run main.go <aws-region> <aws-access-key-id> <aws-secret-access-key> <bucket-name> </folder/file_name.ext OnS3> <file-path>")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Extract bucket name and file path from command-line arguments
|
||||
awsRegion := os.Args[1]
|
||||
awsAccessKeyID := os.Args[2]
|
||||
awsSecretAccessKey := os.Args[3]
|
||||
bucketName := os.Args[4]
|
||||
folderOnS3 := os.Args[5]
|
||||
filePath := os.Args[6]
|
||||
|
||||
// Create an AWS session using provided credentials
|
||||
sess, err := session.NewSession(&aws.Config{
|
||||
Region: aws.String(awsRegion),
|
||||
Credentials: credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, ""),
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("Error creating session:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Create an S3 service client
|
||||
s3Client := s3.New(sess)
|
||||
|
||||
// Open the file for reading
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
fmt.Println("Error opening file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Get the file size
|
||||
fileInfo, _ := file.Stat()
|
||||
size := fileInfo.Size()
|
||||
|
||||
// Create an S3 upload parameters object
|
||||
params := &s3.PutObjectInput{
|
||||
Bucket: aws.String(bucketName),
|
||||
Key: aws.String(folderOnS3),
|
||||
// ACL: aws.String("bucket-owner-full-control"),
|
||||
Body: file,
|
||||
ContentLength: aws.Int64(size),
|
||||
}
|
||||
|
||||
// Perform the upload
|
||||
_, err = s3Client.PutObject(params)
|
||||
if err != nil {
|
||||
fmt.Println("Error uploading file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("File uploaded successfully!")
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
go mod init go-s3-upload
|
||||
go get github.com/aws/aws-sdk-go/service/s3
|
||||
|
||||
go run main.go <aws-region> <aws-access-key-id> <aws-secret-access-key> <bucket-name> </folder/file_name.ext OnS3> <file-path>
|
Loading…
Reference in New Issue