-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added aws script-golang for uploading multiple files together using g…
…oroutines
- Loading branch information
1 parent
31fd3fb
commit a20e470
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module main | ||
|
||
go 1.21.5 | ||
|
||
require ( | ||
github.com/aws/aws-lambda-go v1.44.0 // indirect | ||
github.com/aws/aws-sdk-go-v2 v1.24.1 // indirect | ||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.4 // indirect | ||
github.com/aws/aws-sdk-go-v2/config v1.26.3 // indirect | ||
github.com/aws/aws-sdk-go-v2/credentials v1.16.14 // indirect | ||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.11 // indirect | ||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.10 // indirect | ||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.10 // indirect | ||
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2 // indirect | ||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.2.10 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.4 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.2.10 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.10 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.16.10 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/s3 v1.48.0 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/sso v1.18.6 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.6 // indirect | ||
github.com/aws/aws-sdk-go-v2/service/sts v1.26.7 // indirect | ||
github.com/aws/smithy-go v1.19.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
) | ||
|
||
func main() { | ||
// Load the Shared AWS Configuration (~/.aws/config) | ||
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile("<Your-Profile-name>")) | ||
//https://github.com/aws/aws-sdk-go-v2/blob/main/service/s3/api_op_GetBucketNotificationConfiguration.go | ||
|
||
if err != nil { | ||
log.Fatal("coming from here") | ||
log.Fatal(err) | ||
} | ||
|
||
// Create an Amazon S3 service client | ||
client := s3.NewFromConfig(cfg) | ||
|
||
fmt.Println("S3 event notification configured successfully.") | ||
|
||
type result struct { | ||
Output *s3.PutObjectOutput | ||
Err error | ||
} | ||
results := make(chan result, 2) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(2) | ||
go func() { | ||
defer wg.Done() | ||
content, err := ioutil.ReadFile("chad.jpeg") | ||
if err != nil { | ||
results <- result{Err: err} | ||
return | ||
} | ||
output, err := client.PutObject(context.TODO(), &s3.PutObjectInput{ | ||
Bucket: aws.String("<Your-bucket-name>"), | ||
Key: aws.String("pfdffdcxdflus"), | ||
Body: strings.NewReader(string(content)), | ||
}) | ||
results <- result{Output: output, Err: err} | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
content, err := ioutil.ReadFile("giving a talk.jpg") | ||
if err != nil { | ||
results <- result{Err: err} | ||
return | ||
} | ||
output, err := client.PutObject(context.TODO(), &s3.PutObjectInput{ | ||
Bucket: aws.String("<Your-bucket-name>"), | ||
Key: aws.String("minffdcxdfdus"), | ||
Body: strings.NewReader(string(content)), | ||
}) | ||
results <- result{Output: output, Err: err} | ||
}() | ||
wg.Wait() | ||
|
||
close(results) | ||
for result := range results { | ||
if result.Err != nil { | ||
log.Printf("error: %v", result.Err) | ||
continue | ||
} | ||
fmt.Printf("etag: %v", aws.ToString(result.Output.ETag)) | ||
} | ||
} | ||
|
||
//https://aws.github.io/aws-sdk-go-v2/docs/getting-started/ |