Packages
The following packages are needed; there are currently two versions, if you write new code you should use the second version, as the first one will no longer be supported shortly
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
Most of the packages are from s3, the rest support creating a client etc.
Endpoint
If you use AWS no endpoint info is needed. However for any other providers, endpoint must be specified, as, although they implement S3, obviously the endpoint does not have anything to do with Amazon’s
Here is the endpoint for Linode:
https://us-east-1.linodeobjects.com
Coding
package main import ( "context" "fmt" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/credentials" "github.com/aws/aws-sdk-go-v2/service/s3" "os" ) func main() { proceed() } func proceed() { ctx := context.Background() cfg, err := client := s3.NewFromConfig(cfg, func(o *s3.Options) { o.Region = "us-east-1" o.BaseEndpoint = aws.String("https://us-east-1.linodeobjects.com") accessKey := os.Getenv("CURRENT_KEY") accessSecret := os.Getenv("CURRENT_SECRET") o.Credentials = credentials.NewStaticCredentialsProvider(accessKey, accessSecret, "") }) // ok, list objects output, err := client.ListObjectsV2(ctx, &s3.ListObjectsV2Input{ Bucket: aws.String("bucket-storage"), }) if err != nil { fmt.Println("there was an error... ", err.Error()) } else { fmt.Println("list bucket with success") for _, object := range output.Contents { fmt.Println(*object.Key) } } // ok, now upload the object fileName := os.Getenv("FILE_NAME") open, err := os.Open(fileName) if err != nil { fmt.Println("there was an error opening the file") } _, err = client.PutObject(ctx, &s3.PutObjectInput{ Bucket: aws.String("bucket-storage"), Key: aws.String("current.txt"), Body: open, }) if err != nil { fmt.Println("error putting the object into the bucket") } // now get the file getOutput, err := client.GetObject(ctx, &s3.GetObjectInput{ Bucket: aws.String("bucket-storage"), Key: aws.String("current.txt"), }) if err != nil { fmt.Println("error: %w", err) } else { b := make([]byte, 1000) info, _ := getOutput.Body.Read(b) fmt.Println(fmt.Sprintf("read: %d bytes that are >>>%s<<<", info, string(b[0:info]))) } // and now delete the file _, err = client.DeleteObject(ctx, &s3.DeleteObjectInput{ Bucket: aws.String("bucket-storage"), Key: aws.String("current.txt"), }) if err != nil { fmt.Println(fmt.Errorf("error encountered %w", err)) } else { fmt.Println("deleted...") } }