Created
August 9, 2015 16:39
-
-
Save anxiousmodernman/e128e3d4b4f3a8057c02 to your computer and use it in GitHub Desktop.
S3 upload with Go
This file contains hidden or 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
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()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is recommended that I turn on logging.