|
6 | 6 | "encoding/csv"
|
7 | 7 | "errors"
|
8 | 8 | "fmt"
|
| 9 | + "net/http" |
9 | 10 | "os"
|
10 | 11 | "path/filepath"
|
11 | 12 | "strings"
|
@@ -994,3 +995,53 @@ func TestSelectCSVHeaders(t *testing.T) {
|
994 | 995 | assert.Error(t, err)
|
995 | 996 | })
|
996 | 997 | }
|
| 998 | + |
| 999 | +func TestPresignPutObject(t *testing.T) { |
| 1000 | + t.Parallel() |
| 1001 | + ctx := ctxawslocal.WithContext( |
| 1002 | + context.Background(), |
| 1003 | + ctxawslocal.WithS3Endpoint("http://127.0.0.1:29000"), // use Minio |
| 1004 | + ctxawslocal.WithAccessKey("DUMMYACCESSKEYEXAMPLE"), |
| 1005 | + ctxawslocal.WithSecretAccessKey("DUMMYSECRETKEYEXAMPLE"), |
| 1006 | + ) |
| 1007 | + |
| 1008 | + uploadTxtByPresignedPutObjectURL := func(presignedURL string) error { |
| 1009 | + content := []byte("Hello World") |
| 1010 | + req, err := http.NewRequest(http.MethodPut, presignedURL, bytes.NewReader(content)) |
| 1011 | + if err != nil { |
| 1012 | + return err |
| 1013 | + } |
| 1014 | + |
| 1015 | + req.Header.Set("Content-Type", "text/plain") |
| 1016 | + resp, err := http.DefaultClient.Do(req) |
| 1017 | + if err != nil { |
| 1018 | + return err |
| 1019 | + } |
| 1020 | + |
| 1021 | + defer resp.Body.Close() |
| 1022 | + |
| 1023 | + if resp.StatusCode != http.StatusOK { |
| 1024 | + return fmt.Errorf("failed to upload file, status code: %d", resp.StatusCode) |
| 1025 | + } |
| 1026 | + |
| 1027 | + return nil |
| 1028 | + } |
| 1029 | + |
| 1030 | + confirmedUploadedObject := func(ctx context.Context, key awss3.Key) error { |
| 1031 | + _, err := awss3.HeadObject(ctx, TestRegion, TestBucket, key) |
| 1032 | + return err |
| 1033 | + } |
| 1034 | + |
| 1035 | + t.Run("Presign", func(t *testing.T) { |
| 1036 | + t.Parallel() |
| 1037 | + key := awss3.Key("test_presign_put_object_01.txt") |
| 1038 | + pURL, err := awss3.PresignPutObject(ctx, TestRegion, TestBucket, key) |
| 1039 | + assert.NoError(t, err) |
| 1040 | + assert.NotEmpty(t, pURL) |
| 1041 | + |
| 1042 | + err = uploadTxtByPresignedPutObjectURL(pURL) |
| 1043 | + assert.NoError(t, err) |
| 1044 | + err = confirmedUploadedObject(ctx, key) |
| 1045 | + assert.NoError(t, err) |
| 1046 | + }) |
| 1047 | +} |
0 commit comments