4
4
package scenarios
5
5
6
6
import (
7
+ "bytes"
7
8
"fmt"
8
9
"io"
9
10
"log"
11
+ "mime/multipart"
10
12
"net/http"
11
13
"os"
12
14
"strings"
@@ -23,6 +25,7 @@ import (
23
25
// unit testing.
24
26
type IHttpRequester interface {
25
27
Get (url string ) (resp * http.Response , err error )
28
+ Post (url , contentType string , body io.Reader ) (resp * http.Response , err error )
26
29
Put (url string , contentLength int64 , body io.Reader ) (resp * http.Response , err error )
27
30
Delete (url string ) (resp * http.Response , err error )
28
31
}
@@ -33,6 +36,15 @@ type HttpRequester struct{}
33
36
func (httpReq HttpRequester ) Get (url string ) (resp * http.Response , err error ) {
34
37
return http .Get (url )
35
38
}
39
+ func (httpReq HttpRequester ) Post (url , contentType string , body io.Reader ) (resp * http.Response , err error ) {
40
+ postRequest , err := http .NewRequest ("POST" , url , body )
41
+ if err != nil {
42
+ return nil , err
43
+ }
44
+ postRequest .Header .Set ("Content-Type" , contentType )
45
+ return http .DefaultClient .Do (postRequest )
46
+ }
47
+
36
48
func (httpReq HttpRequester ) Put (url string , contentLength int64 , body io.Reader ) (resp * http.Response , err error ) {
37
49
putRequest , err := http .NewRequest ("PUT" , url , body )
38
50
if err != nil {
@@ -51,6 +63,43 @@ func (httpReq HttpRequester) Delete(url string) (resp *http.Response, err error)
51
63
52
64
// snippet-end:[gov2.s3.IHttpRequester.helper]
53
65
66
+ // snippet-start:[gov2.s3.MultipartUpload.helper]
67
+ func sendMultipartRequest (url string , fields map [string ]string , file * os.File , filePath string , httpRequester IHttpRequester ) (* http.Response , error ) {
68
+ // Create a buffer to hold the multipart data
69
+ var requestBody bytes.Buffer
70
+ writer := multipart .NewWriter (& requestBody )
71
+
72
+ // Add form fields
73
+ for key , val := range fields {
74
+ err := writer .WriteField (key , val )
75
+ if err != nil {
76
+ return nil , err
77
+ }
78
+ }
79
+
80
+ // Always has to be named like this, and always has to be the last one
81
+ fileField := "file"
82
+ part , err := writer .CreateFormFile (fileField , filePath )
83
+ if err != nil {
84
+ return nil , err
85
+ }
86
+ _ , err = io .Copy (part , file )
87
+ if err != nil {
88
+ return nil , err
89
+ }
90
+
91
+ // Close the writer to finalize the multipart message
92
+ err = writer .Close ()
93
+ if err != nil {
94
+ return nil , err
95
+ }
96
+
97
+ // make the request
98
+ return httpRequester .Post (url , writer .FormDataContentType (), & requestBody )
99
+ }
100
+
101
+ // snippet-end:[gov2.s3.MultipartUpload.helper]
102
+
54
103
// snippet-start:[gov2.s3.Scenario_Presigning]
55
104
56
105
// RunPresigningScenario is an interactive example that shows you how to get presigned
@@ -76,7 +125,7 @@ func (httpReq HttpRequester) Delete(url string) (resp *http.Response, err error)
76
125
func RunPresigningScenario (sdkConfig aws.Config , questioner demotools.IQuestioner , httpRequester IHttpRequester ) {
77
126
defer func () {
78
127
if r := recover (); r != nil {
79
- fmt .Printf ("Something went wrong with the demo. " )
128
+ fmt .Printf ("Something went wrong with the demo" )
80
129
}
81
130
}()
82
131
@@ -159,6 +208,24 @@ func RunPresigningScenario(sdkConfig aws.Config, questioner demotools.IQuestione
159
208
log .Println (string (downloadBody [:100 ]))
160
209
log .Println (strings .Repeat ("-" , 88 ))
161
210
211
+ log .Println ("Now we'll create a new request to put the same object using a presigned post request" )
212
+ presignPostRequest , err := presigner .PresignPostObject (bucketName , uploadKey , 60 )
213
+ if err != nil {
214
+ panic (err )
215
+ }
216
+ log .Printf ("Got a presigned post request to url %v with values %v\n " , presignPostRequest .URL , presignPostRequest .Values )
217
+ log .Println ("Using net/http multipart to send the request..." )
218
+ uploadFile , err = os .Open (uploadFilename )
219
+ if err != nil {
220
+ panic (err )
221
+ }
222
+ defer uploadFile .Close ()
223
+ multiPartResponse , err := sendMultipartRequest (presignPostRequest .URL , presignPostRequest .Values , uploadFile , uploadKey , httpRequester )
224
+ if err != nil {
225
+ panic (err )
226
+ }
227
+ log .Printf ("Presign post object %v with presigned URL returned %v." , uploadKey , multiPartResponse .StatusCode )
228
+
162
229
log .Println ("Let's presign a request to delete the object." )
163
230
questioner .Ask ("Press Enter when you're ready." )
164
231
presignedDelRequest , err := presigner .DeleteObject (bucketName , uploadKey )
0 commit comments