static helpers newClient creates new S3 client on a per-region basis or, more precisely, per (region, endpoint) pair - and note that s3 endpoint is per-bucket configurable. If the client already exists newClient simply returns it. From S3 SDK: "S3 methods are safe to use concurrently. It is not s
(conf sessConf, tag string)
| 480 | // "S3 methods are safe to use concurrently. It is not safe to |
| 481 | // modify mutate any of the struct's properties though." |
| 482 | func newClient(conf sessConf, tag string) (svc *s3.S3, region string, err error) { |
| 483 | endpoint := s3Endpoint |
| 484 | region = conf.region |
| 485 | if conf.bck != nil && conf.bck.Props != nil { |
| 486 | if region == "" { |
| 487 | region = conf.bck.Props.Extra.AWS.CloudRegion |
| 488 | } |
| 489 | if conf.bck.Props.Extra.AWS.Endpoint != "" { |
| 490 | endpoint = conf.bck.Props.Extra.AWS.Endpoint |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | // reuse |
| 495 | if region != "" { |
| 496 | cmu.RLock() |
| 497 | svc = clients[region][endpoint] |
| 498 | cmu.RUnlock() |
| 499 | if svc != nil { |
| 500 | return |
| 501 | } |
| 502 | } |
| 503 | // create |
| 504 | var ( |
| 505 | sess = _session(endpoint) |
| 506 | awsConf = &aws.Config{} |
| 507 | ) |
| 508 | if region == "" { |
| 509 | if tag != "" { |
| 510 | err = fmt.Errorf("%s: unknown region for bucket %s -- proceeding with default", tag, conf.bck) |
| 511 | } |
| 512 | svc = s3.New(sess) |
| 513 | return |
| 514 | } |
| 515 | // ok |
| 516 | awsConf.Region = aws.String(region) |
| 517 | svc = s3.New(sess, awsConf) |
| 518 | debug.Assertf(region == *svc.Config.Region, "%s != %s", region, *svc.Config.Region) |
| 519 | |
| 520 | cmu.Lock() |
| 521 | eps := clients[region] |
| 522 | if eps == nil { |
| 523 | eps = make(map[string]*s3.S3, 1) |
| 524 | clients[region] = eps |
| 525 | } |
| 526 | eps[endpoint] = svc |
| 527 | cmu.Unlock() |
| 528 | return |
| 529 | } |
| 530 | |
| 531 | // Create session using default creds from ~/.aws/credentials and environment variables. |
| 532 | func _session(endpoint string) *session.Session { |
no test coverage detected