mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-09 22:39:53 +00:00
Conver to regular Go vendor + dep tool
This commit is contained in:
+6
@@ -0,0 +1,6 @@
|
||||
Custom Endpoint Example
|
||||
===
|
||||
|
||||
This example provides examples on how you can provide custom endpoints, and logic to how endpoints are resolved by the SDK.
|
||||
|
||||
The example creates multiple clients with different endpoint configuraiton. From a custom endpoint resolver that wraps the defeault resolver so that any S3 service client created uses the custom endpoint, to how you can provide your own logic to a single service's endpoint resolving.
|
||||
Generated
Vendored
+77
@@ -0,0 +1,77 @@
|
||||
// +build example
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/endpoints"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/dynamodb"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/aws/aws-sdk-go/service/sqs"
|
||||
)
|
||||
|
||||
func main() {
|
||||
defaultResolver := endpoints.DefaultResolver()
|
||||
s3CustResolverFn := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
|
||||
if service == "s3" {
|
||||
return endpoints.ResolvedEndpoint{
|
||||
URL: "s3.custom.endpoint.com",
|
||||
SigningRegion: "custom-signing-region",
|
||||
}, nil
|
||||
}
|
||||
|
||||
return defaultResolver.EndpointFor(service, region, optFns...)
|
||||
}
|
||||
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
||||
Config: aws.Config{
|
||||
Region: aws.String("us-west-2"),
|
||||
EndpointResolver: endpoints.ResolverFunc(s3CustResolverFn),
|
||||
},
|
||||
}))
|
||||
|
||||
// Create the S3 service client with the shared session. This will
|
||||
// automatically use the S3 custom endpoint configured in the custom
|
||||
// endpoint resolver wrapping the default endpoint resolver.
|
||||
s3Svc := s3.New(sess)
|
||||
// Operation calls will be made to the custom endpoint.
|
||||
s3Svc.GetObject(&s3.GetObjectInput{
|
||||
Bucket: aws.String("myBucket"),
|
||||
Key: aws.String("myObjectKey"),
|
||||
})
|
||||
|
||||
// Create the SQS service client with the shared session. This will
|
||||
// fallback to the default endpoint resolver because the customization
|
||||
// passes any non S3 service endpoint resolve to the default resolver.
|
||||
sqsSvc := sqs.New(sess)
|
||||
// Operation calls will be made to the default endpoint for SQS for the
|
||||
// region configured.
|
||||
sqsSvc.ReceiveMessage(&sqs.ReceiveMessageInput{
|
||||
QueueUrl: aws.String("my-queue-url"),
|
||||
})
|
||||
|
||||
// Create a DynamoDB service client that will use a custom endpoint
|
||||
// resolver that overrides the shared session's. This is useful when
|
||||
// custom endpoints are generated, or multiple endpoints are switched on
|
||||
// by a region value.
|
||||
ddbCustResolverFn := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
|
||||
return endpoints.ResolvedEndpoint{
|
||||
URL: "dynamodb.custom.endpoint",
|
||||
SigningRegion: "custom-signing-region",
|
||||
}, nil
|
||||
}
|
||||
ddbSvc := dynamodb.New(sess, &aws.Config{
|
||||
EndpointResolver: endpoints.ResolverFunc(ddbCustResolverFn),
|
||||
})
|
||||
// Operation calls will be made to the custom endpoint set in the
|
||||
// ddCustResolverFn.
|
||||
ddbSvc.ListTables(&dynamodb.ListTablesInput{})
|
||||
|
||||
// Setting Config's Endpoint will override the EndpointResolver. Forcing
|
||||
// the service clien to make all operation to the endpoint specified
|
||||
// the in the config.
|
||||
ddbSvcLocal := dynamodb.New(sess, &aws.Config{
|
||||
Endpoint: aws.String("http://localhost:8088"),
|
||||
})
|
||||
ddbSvcLocal.ListTables(&dynamodb.ListTablesInput{})
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
Enumerate Regions and Endpoints Example
|
||||
===
|
||||
|
||||
Demostrates how the SDK's endpoints can be enumerated over to discover regions, services, and endpoints defined by the SDK's Regions and Endpoints metadata.
|
||||
|
||||
Usage
|
||||
---
|
||||
|
||||
The following parameters can be used to enumerate the SDK's partition metadata.
|
||||
|
||||
Example:
|
||||
|
||||
go run enumEndpoints.go -p aws -services -r us-west-2
|
||||
|
||||
Output:
|
||||
|
||||
Services with endpoint us-west-2 in aws:
|
||||
ec2
|
||||
dynamodb
|
||||
s3
|
||||
...
|
||||
|
||||
CLI parameters
|
||||
---
|
||||
|
||||
```
|
||||
-p=id partition id, e.g: aws
|
||||
-r=id region id, e.g: us-west-2
|
||||
-s=id service id, e.g: s3
|
||||
|
||||
-partitions Lists all partitions.
|
||||
-regions Lists all regions in a partition. Requires partition ID.
|
||||
If service ID is also provided will show endpoints for a service.
|
||||
-services Lists all services in a partition. Requires partition ID.
|
||||
If region ID is also provided, will show services available in that region.
|
||||
```
|
||||
|
||||
Generated
Vendored
+126
@@ -0,0 +1,126 @@
|
||||
// +build example
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/endpoints"
|
||||
)
|
||||
|
||||
// Demostrates how the SDK's endpoints can be enumerated over to discover
|
||||
// regions, services, and endpoints defined by the SDK's Regions and Endpoints
|
||||
// metadata.
|
||||
//
|
||||
// Usage:
|
||||
// -p=id partition id, e.g: aws
|
||||
// -r=id region id, e.g: us-west-2
|
||||
// -s=id service id, e.g: s3
|
||||
//
|
||||
// -partitions Lists all partitions.
|
||||
// -regions Lists all regions in a partition. Requires partition ID.
|
||||
// If service ID is also provided will show endpoints for a service.
|
||||
// -services Lists all services in a partition. Requires partition ID.
|
||||
// If region ID is also provided, will show services available in that region.
|
||||
//
|
||||
// Example:
|
||||
// go run enumEndpoints.go -p aws -services -r us-west-2
|
||||
//
|
||||
// Output:
|
||||
// Services with endpoint us-west-2 in aws:
|
||||
// ...
|
||||
func main() {
|
||||
var partitionID, regionID, serviceID string
|
||||
flag.StringVar(&partitionID, "p", "", "Partition ID")
|
||||
flag.StringVar(®ionID, "r", "", "Region ID")
|
||||
flag.StringVar(&serviceID, "s", "", "Service ID")
|
||||
|
||||
var cmdPartitions, cmdRegions, cmdServices bool
|
||||
flag.BoolVar(&cmdPartitions, "partitions", false, "Lists partitions.")
|
||||
flag.BoolVar(&cmdRegions, "regions", false, "Lists regions of a partition. Requires partition ID to be provided. Will filter by a service if '-s' is set.")
|
||||
flag.BoolVar(&cmdServices, "services", false, "Lists services for a partition. Requires partition ID to be provided. Will filter by a region if '-r' is set.")
|
||||
flag.Parse()
|
||||
|
||||
partitions := endpoints.DefaultResolver().(endpoints.EnumPartitions).Partitions()
|
||||
|
||||
if cmdPartitions {
|
||||
printPartitions(partitions)
|
||||
}
|
||||
|
||||
if !(cmdRegions || cmdServices) {
|
||||
return
|
||||
}
|
||||
|
||||
p, ok := findPartition(partitions, partitionID)
|
||||
if !ok {
|
||||
fmt.Fprintf(os.Stderr, "Partition %q not found", partitionID)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if cmdRegions {
|
||||
printRegions(p, serviceID)
|
||||
}
|
||||
|
||||
if cmdServices {
|
||||
printServices(p, regionID)
|
||||
}
|
||||
}
|
||||
|
||||
func printPartitions(ps []endpoints.Partition) {
|
||||
fmt.Println("Partitions:")
|
||||
for _, p := range ps {
|
||||
fmt.Println(p.ID())
|
||||
}
|
||||
}
|
||||
|
||||
func printRegions(p endpoints.Partition, serviceID string) {
|
||||
if len(serviceID) != 0 {
|
||||
s, ok := p.Services()[serviceID]
|
||||
if !ok {
|
||||
fmt.Fprintf(os.Stderr, "service %q does not exist in partition %q", serviceID, p.ID())
|
||||
os.Exit(1)
|
||||
}
|
||||
es := s.Endpoints()
|
||||
fmt.Printf("Endpoints for %s in %s:\n", serviceID, p.ID())
|
||||
for _, e := range es {
|
||||
r, _ := e.ResolveEndpoint()
|
||||
fmt.Printf("%s: %s\n", e.ID(), r.URL)
|
||||
}
|
||||
|
||||
} else {
|
||||
rs := p.Regions()
|
||||
fmt.Printf("Regions in %s:\n", p.ID())
|
||||
for _, r := range rs {
|
||||
fmt.Println(r.ID())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func printServices(p endpoints.Partition, endpointID string) {
|
||||
ss := p.Services()
|
||||
|
||||
if len(endpointID) > 0 {
|
||||
fmt.Printf("Services with endpoint %s in %s:\n", endpointID, p.ID())
|
||||
} else {
|
||||
fmt.Printf("Services in %s:\n", p.ID())
|
||||
}
|
||||
|
||||
for id, s := range ss {
|
||||
if _, ok := s.Endpoints()[endpointID]; !ok && len(endpointID) > 0 {
|
||||
continue
|
||||
}
|
||||
fmt.Println(id)
|
||||
}
|
||||
}
|
||||
|
||||
func findPartition(ps []endpoints.Partition, partitionID string) (endpoints.Partition, bool) {
|
||||
for _, p := range ps {
|
||||
if p.ID() == partitionID {
|
||||
return p, true
|
||||
}
|
||||
}
|
||||
|
||||
return endpoints.Partition{}, false
|
||||
}
|
||||
Generated
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
# Handling Specific Service Error Codes
|
||||
|
||||
This examples highlights how you can use the `awserr.Error` type to perform logic based on specific error codes returned by service API operations.
|
||||
|
||||
In this example the `S3` `GetObject` API operation is used to request the contents of a object in S3. The example handles the `NoSuchBucket` and `NoSuchKey` error codes printing custom messages to stderr. If Any other error is received a generic message is printed.
|
||||
|
||||
## Usage
|
||||
|
||||
Will make a request to S3 for the contents of an object. If the request was successful, and the object was found the object's path and size will be printed to stdout.
|
||||
|
||||
If the object's bucket or key does not exist a specific error message will be printed to stderr for the error.
|
||||
|
||||
Any other error will be printed as an unknown error.
|
||||
|
||||
```sh
|
||||
go run -tags example handleServiceErrorCodes.go mybucket mykey
|
||||
```
|
||||
Generated
Vendored
+66
@@ -0,0 +1,66 @@
|
||||
// +build example
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
)
|
||||
|
||||
func exitErrorf(msg string, args ...interface{}) {
|
||||
fmt.Fprintf(os.Stderr, msg+"\n", args...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Will make a request to S3 for the contents of an object. If the request
|
||||
// was successful, and the object was found the object's path and size will be
|
||||
// printed to stdout.
|
||||
//
|
||||
// If the object's bucket or key does not exist a specific error message will
|
||||
// be printed to stderr for the error.
|
||||
//
|
||||
// Any other error will be printed as an unknown error.
|
||||
//
|
||||
// Usage: handleServiceErrorCodes <bucket> <key>
|
||||
func main() {
|
||||
if len(os.Args) < 3 {
|
||||
exitErrorf("Usage: %s <bucket> <key>", filepath.Base(os.Args[0]))
|
||||
}
|
||||
sess := session.Must(session.NewSession())
|
||||
|
||||
svc := s3.New(sess)
|
||||
resp, err := svc.GetObject(&s3.GetObjectInput{
|
||||
Bucket: aws.String(os.Args[1]),
|
||||
Key: aws.String(os.Args[2]),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
// Casting to the awserr.Error type will allow you to inspect the error
|
||||
// code returned by the service in code. The error code can be used
|
||||
// to switch on context specific functionality. In this case a context
|
||||
// specific error message is printed to the user based on the bucket
|
||||
// and key existing.
|
||||
//
|
||||
// For information on other S3 API error codes see:
|
||||
// http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html
|
||||
if aerr, ok := err.(awserr.Error); ok {
|
||||
switch aerr.Code() {
|
||||
case s3.ErrCodeNoSuchBucket:
|
||||
exitErrorf("bucket %s does not exist", os.Args[1])
|
||||
case s3.ErrCodeNoSuchKey:
|
||||
exitErrorf("object with key %s does not exist in bucket %s", os.Args[2], os.Args[1])
|
||||
}
|
||||
}
|
||||
exitErrorf("unknown error occurred, %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
fmt.Printf("s3://%s/%s exists. size: %d\n", os.Args[1], os.Args[2],
|
||||
aws.Int64Value(resp.ContentLength))
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
# Example
|
||||
|
||||
Uploads a file to S3 given a bucket and object key. Also takes a duration
|
||||
value to terminate the update if it doesn't complete within that time.
|
||||
|
||||
The AWS Region needs to be provided in the AWS shared config or on the
|
||||
environment variable as `AWS_REGION`. Credentials also must be provided
|
||||
Will default to shared config file, but can load from environment if provided.
|
||||
|
||||
## Usage:
|
||||
|
||||
# Upload myfile.txt to myBucket/myKey. Must complete within 10 minutes or will fail
|
||||
go run withContext.go -b mybucket -k myKey -d 10m < myfile.txt
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
// +build example,go1.7
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
)
|
||||
|
||||
// Uploads a file to S3 given a bucket and object key. Also takes a duration
|
||||
// value to terminate the update if it doesn't complete within that time.
|
||||
//
|
||||
// The AWS Region needs to be provided in the AWS shared config or on the
|
||||
// environment variable as `AWS_REGION`. Credentials also must be provided
|
||||
// Will default to shared config file, but can load from environment if provided.
|
||||
//
|
||||
// Usage:
|
||||
// # Upload myfile.txt to myBucket/myKey. Must complete within 10 minutes or will fail
|
||||
// go run withContext.go -b mybucket -k myKey -d 10m < myfile.txt
|
||||
func main() {
|
||||
var bucket, key string
|
||||
var timeout time.Duration
|
||||
|
||||
flag.StringVar(&bucket, "b", "", "Bucket name.")
|
||||
flag.StringVar(&key, "k", "", "Object key name.")
|
||||
flag.DurationVar(&timeout, "d", 0, "Upload timeout.")
|
||||
flag.Parse()
|
||||
|
||||
sess := session.Must(session.NewSession())
|
||||
svc := s3.New(sess)
|
||||
|
||||
ctx := context.Background()
|
||||
var cancelFn func()
|
||||
if timeout > 0 {
|
||||
ctx, cancelFn = context.WithTimeout(ctx, timeout)
|
||||
}
|
||||
|
||||
// Uploads the object to S3. The Context will interrupt the request
|
||||
resp, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
Body: os.Stdin,
|
||||
})
|
||||
|
||||
fmt.Println(resp, err)
|
||||
|
||||
// Cleanup context
|
||||
cancelFn()
|
||||
}
|
||||
Reference in New Issue
Block a user