Debian Codegraph @

examples/s3/s3-outposts-put-get.go

raw
1 //go:build example
2 // +build example
3
4 /*
5 * MinIO Go Library for Amazon S3 Compatible Cloud Storage
6 * Copyright 2015-2024 MinIO, Inc.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 // This example uses the minio-go client against AWS S3 on Outposts.
22 // It performs a PutObject and GetObject to verify the client works with
23 // Outposts endpoints (s3-outposts signing and path-style requests).
24 //
25 // Set environment variables (do not commit real credentials):
26 //
27 // S3_OUTPOSTS_ENDPOINT - Outposts access point endpoint (e.g. myap-123.op-xxx.s3-outposts.region.amazonaws.com)
28 // S3_OUTPOSTS_BUCKET - Access point alias / bucket name (e.g. mybucket--op-xxx--op-s3)
29 // S3_OUTPOSTS_REGION - AWS region (e.g. eu-central-1). Optional if AWS_REGION is set.
30 // S3_OUTPOSTS_PROFILE - Optional. AWS credentials profile. If unset, uses AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY.
31 // AWS_ACCESS_KEY_ID - Required if S3_OUTPOSTS_PROFILE is unset
32 // AWS_SECRET_ACCESS_KEY - Required if S3_OUTPOSTS_PROFILE is unset
33 //
34 // Run from repo root (uses local minio-go):
35 //
36 // go run -tags example ./examples/s3/s3-outposts-put-get.go
37 //
38 // Or from examples/s3 (replace in go.mod points to parent minio-go):
39 //
40 // cd examples/s3 && go run -tags example s3-outposts-put-get.go
41 package main
42
43 import (
44 "context"
45 "fmt"
46 "io"
47 "log"
48 "os"
49 "strings"
50
51 "github.com/minio/minio-go/v7"
52 "github.com/minio/minio-go/v7/pkg/credentials"
53 )
54
55 func main() {
56 endpoint := os.Getenv("S3_OUTPOSTS_ENDPOINT")
57 bucket := os.Getenv("S3_OUTPOSTS_BUCKET")
58 region := os.Getenv("S3_OUTPOSTS_REGION")
59 if region == "" {
60 region = os.Getenv("AWS_REGION")
61 }
62 profile := os.Getenv("S3_OUTPOSTS_PROFILE")
63
64 if endpoint == "" || bucket == "" || region == "" {
65 log.Fatalf("Missing required env: set S3_OUTPOSTS_ENDPOINT, S3_OUTPOSTS_BUCKET, and S3_OUTPOSTS_REGION (or AWS_REGION)")
66 }
67
68 var creds *credentials.Credentials
69 if profile != "" {
70 creds = credentials.NewFileAWSCredentials("", profile)
71 } else {
72 accessKey := os.Getenv("AWS_ACCESS_KEY_ID")
73 secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
74 if accessKey == "" || secretKey == "" {
75 log.Fatalf("Set S3_OUTPOSTS_PROFILE or both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY")
76 }
77 creds = credentials.NewStaticV4(accessKey, secretKey, "")
78 }
79
80 client, err := minio.New(endpoint, &minio.Options{
81 Creds: creds,
82 Secure: true,
83 Region: region,
84 })
85 if err != nil {
86 log.Fatalf("New client: %v", err)
87 }
88
89 objectKey := "outposts-test/hello-minio-go.txt"
90 objectBody := "Hello from minio-go S3 on Outposts\n"
91
92 ctx := context.Background()
93
94 fmt.Println("PutObject...")
95 _, err = client.PutObject(ctx, bucket, objectKey, strings.NewReader(objectBody), int64(len(objectBody)), minio.PutObjectOptions{
96 ContentType: "text/plain",
97 })
98 if err != nil {
99 log.Fatalf("PutObject: %v", err)
100 }
101 fmt.Println("PutObject OK")
102
103 fmt.Println("GetObject...")
104 obj, err := client.GetObject(ctx, bucket, objectKey, minio.GetObjectOptions{})
105 if err != nil {
106 log.Fatalf("GetObject: %v", err)
107 }
108 defer obj.Close()
109
110 data, err := io.ReadAll(obj)
111 if err != nil {
112 log.Fatalf("Read body: %v", err)
113 }
114 fmt.Println("GetObject OK")
115 fmt.Println("Content:", string(data))
116 fmt.Println("Done. MinIO client works with S3 on Outposts.")
117 }