Skip to content

Instantly share code, notes, and snippets.

@patterns
Created February 3, 2017 05:31
Show Gist options
  • Save patterns/d6a3aa8c1621399e3a02c90ec163aeb4 to your computer and use it in GitHub Desktop.
Save patterns/d6a3aa8c1621399e3a02c90ec163aeb4 to your computer and use it in GitHub Desktop.
AWS S3 file storage example from the Go docs
package main
import (
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
// "github.com/aws/aws-sdk-go/service/sqs"
)
// Uploads a file to a specific bucket in S3 with the filename
// as the Object's key. After it's uploaded a message will be sent
// to a queue.
func main() {
if len(os.Args) != 4 {
log.Fatalln("Usage:", os.Args[0], "<bucket> <queue> <file>")
}
file, err := os.Open(os.Args[3])
if err != nil {
log.Fatal("Open failed:", err)
}
defer file.Close()
// Upload the file to S3 using the S3 Manager
uploader := s3manager.NewUploader(session.New(&aws.Config{Region: aws.String("us-west-2")}))
uploadRes, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(os.Args[1]),
Key: aws.String(file.Name()),
Body: file,
})
if err != nil {
log.Fatalln("Upload failed:", err)
}
fmt.Printf("resp- %v\n", uploadRes)
/*
// Get the Queue's URL that the message will be posted to
svc := sqs.New(session.New())
urlRes, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{
QueueName: aws.String(os.Args[2]),
})
if err != nil {
log.Fatalln("GetQueueURL failed:", err)
}
// Send the Message to the Queue
_, err = svc.SendMessage(&sqs.SendMessageInput{
MessageBody: &uploadRes.Location,
QueueUrl: urlRes.QueueUrl,
})
if err != nil {
log.Fatalln("SendMessage failed:", err)
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment