Skip to content

Instantly share code, notes, and snippets.

@anxiousmodernman
Created August 9, 2015 16:39
Show Gist options
  • Save anxiousmodernman/e128e3d4b4f3a8057c02 to your computer and use it in GitHub Desktop.
Save anxiousmodernman/e128e3d4b4f3a8057c02 to your computer and use it in GitHub Desktop.
S3 upload with Go
func performUpload(path string) {
fmt.Println("uploading:", path)
// call this in a goroutine from the main service UploadToS3
creds := credentials.NewStaticCredentials(awsAccessKeyID, secretAccessKey, "")
// TODO extract this into a config provider
awsConfig := &aws.Config{
Credentials: creds,
Region: "us-east-1",
}
svc := s3.New(awsConfig)
_, fileName := filepath.Split(path)
params := &s3.CreateMultipartUploadInput{
Bucket: aws.String(targetBucket),
Key: aws.String(fileName),
}
_, err := svc.CreateMultipartUpload(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
// Generic AWS error with Code, Message, and original error (if any)
fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
if reqErr, ok := err.(awserr.RequestFailure); ok {
// A service error occurred
fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
}
} else {
fmt.Println("This should never happen.")
// This case should never be hit, the SDK should always return an
// error which satisfies the awserr.Error interface.
fmt.Println(err.Error())
}
}
}
@anxiousmodernman
Copy link
Author

It is recommended that I turn on logging.

svc := s3.New(aws.NewConfig().WithLogLevel(aws.LogDebugWithSigning)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment