github.com/kaydxh/golang@v0.0.131/pkg/storage/s3/s3_test.go (about)

     1  package s3_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	s3_ "github.com/kaydxh/golang/pkg/storage/s3"
     9  	viper_ "github.com/kaydxh/golang/pkg/viper"
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  func TestS3Upload(t *testing.T) {
    14  	cfgFile := "./s3.yaml"
    15  	config := s3_.NewConfig(s3_.WithViper(viper_.GetViper(cfgFile, "storage.s3")))
    16  
    17  	s3Bucket, err := config.Complete().New(context.Background())
    18  	if err != nil || s3Bucket == nil {
    19  		t.Fatalf("failed to new config err: %v", err)
    20  	}
    21  
    22  	testCases := []struct {
    23  		key  string
    24  		data []byte
    25  	}{
    26  		{
    27  			key:  "keytest",
    28  			data: []byte("123"),
    29  		},
    30  	}
    31  
    32  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    33  	defer cancel()
    34  
    35  	for i, testCase := range testCases {
    36  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
    37  			err = s3Bucket.WriteAll(ctx, testCase.key, testCase.data, nil)
    38  			if err != nil {
    39  				t.Fatalf("failed to WriteAll, got : %s", err)
    40  			}
    41  		})
    42  	}
    43  
    44  }
    45  
    46  func TestS3Down(t *testing.T) {
    47  	cfgFile := "./s3.yaml"
    48  	config := s3_.NewConfig(s3_.WithViper(viper_.GetViper(cfgFile, "storage.s3")))
    49  
    50  	s3Bucket, err := config.Complete().New(context.Background())
    51  	if err != nil {
    52  		t.Fatalf("failed to new config err: %v", err)
    53  	}
    54  
    55  	testCases := []struct {
    56  		key  string
    57  		data []byte
    58  	}{
    59  		{
    60  			key: "test_data/dongfangmingzhu.jpeg",
    61  		},
    62  	}
    63  
    64  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    65  	defer cancel()
    66  
    67  	for i, testCase := range testCases {
    68  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
    69  			data, err := s3Bucket.ReadAll(ctx, testCase.key)
    70  			if err != nil {
    71  				t.Fatalf("failed to WriteAll, got : %s", err)
    72  			}
    73  			t.Logf("data len: %v", len(data))
    74  		})
    75  	}
    76  
    77  }