github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/blob/s3blob/example_test.go (about)

     1  // Copyright 2018 The Go Cloud Development Kit Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package s3blob_test
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	awsv2cfg "github.com/aws/aws-sdk-go-v2/config"
    22  	s3v2 "github.com/aws/aws-sdk-go-v2/service/s3"
    23  	"github.com/aws/aws-sdk-go/aws"
    24  	"github.com/aws/aws-sdk-go/aws/session"
    25  	"gocloud.dev/blob"
    26  	"gocloud.dev/blob/s3blob"
    27  )
    28  
    29  func ExampleOpenBucket() {
    30  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    31  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    32  	ctx := context.Background()
    33  
    34  	// Establish an AWS session.
    35  	// See https://docs.aws.amazon.com/sdk-for-go/api/aws/session/ for more info.
    36  	// The region must match the region for "my-bucket".
    37  	sess, err := session.NewSession(&aws.Config{
    38  		Region: aws.String("us-west-1"),
    39  	})
    40  	if err != nil {
    41  		log.Fatal(err)
    42  	}
    43  
    44  	// Create a *blob.Bucket.
    45  	bucket, err := s3blob.OpenBucket(ctx, sess, "my-bucket", nil)
    46  	if err != nil {
    47  		log.Fatal(err)
    48  	}
    49  	defer bucket.Close()
    50  }
    51  
    52  func ExampleOpenBucketV2() {
    53  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    54  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    55  
    56  	// Establish a AWS V2 Config.
    57  	// See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.
    58  	ctx := context.Background()
    59  	cfg, err := awsv2cfg.LoadDefaultConfig(ctx)
    60  	if err != nil {
    61  		log.Fatal(err)
    62  	}
    63  
    64  	// Create a *blob.Bucket.
    65  	clientV2 := s3v2.NewFromConfig(cfg)
    66  	bucket, err := s3blob.OpenBucketV2(ctx, clientV2, "my-bucket", nil)
    67  	if err != nil {
    68  		log.Fatal(err)
    69  	}
    70  	defer bucket.Close()
    71  }
    72  
    73  func Example_openBucketFromURL() {
    74  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    75  	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/blob/s3blob"
    76  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    77  	ctx := context.Background()
    78  
    79  	// blob.OpenBucket creates a *blob.Bucket from a URL.
    80  	bucket, err := blob.OpenBucket(ctx, "s3://my-bucket?region=us-west-1")
    81  	if err != nil {
    82  		log.Fatal(err)
    83  	}
    84  	defer bucket.Close()
    85  
    86  	// Forcing AWS SDK V2.
    87  	bucket, err = blob.OpenBucket(ctx, "s3://my-bucket?region=us-west-1&awssdk=2")
    88  	if err != nil {
    89  		log.Fatal(err)
    90  	}
    91  	defer bucket.Close()
    92  }