@@ -7,14 +7,17 @@ package contentservice
7
7
import (
8
8
"context"
9
9
"fmt"
10
+ "io"
11
+ "net/http"
10
12
13
+ "github.com/gitpod-io/gitpod/common-go/log"
11
14
"github.com/gitpod-io/gitpod/content-service/api"
12
15
"google.golang.org/grpc"
13
16
"google.golang.org/grpc/credentials/insecure"
14
17
)
15
18
16
19
type Interface interface {
17
- GetSignedUploadUrl (ctx context.Context ) ( string , error )
20
+ UploadFile (ctx context.Context , filename string , body io. Reader ) error
18
21
}
19
22
20
23
type Client struct {
@@ -25,7 +28,33 @@ func New(url string) *Client {
25
28
return & Client {url : url }
26
29
}
27
30
28
- func (c * Client ) GetSignedUploadUrl (ctx context.Context ) (string , error ) {
31
+ func (c * Client ) UploadFile (ctx context.Context , filename string , body io.Reader ) error {
32
+ url , err := c .getSignedUploadUrl (ctx , filename )
33
+ if err != nil {
34
+ return fmt .Errorf ("failed to obtain signed upload URL: %w" , err )
35
+ }
36
+
37
+ req , err := http .NewRequest (http .MethodPut , url , body )
38
+ if err != nil {
39
+ return fmt .Errorf ("failed to construct http request: %w" , err )
40
+ }
41
+
42
+ req .Header .Set ("Content-Encoding" , "gzip" )
43
+
44
+ log .Infof ("Uploading %q to object storage..." , filename )
45
+ resp , err := http .DefaultClient .Do (req )
46
+ if err != nil {
47
+ return fmt .Errorf ("failed to make http request: %w" , err )
48
+ }
49
+ if resp .StatusCode != http .StatusOK {
50
+ return fmt .Errorf ("unexpected http response code: %s" , resp .Status )
51
+ }
52
+ log .Info ("Upload complete" )
53
+
54
+ return nil
55
+ }
56
+
57
+ func (c * Client ) getSignedUploadUrl (ctx context.Context , key string ) (string , error ) {
29
58
conn , err := grpc .Dial (c .url , grpc .WithTransportCredentials (insecure .NewCredentials ()))
30
59
if err != nil {
31
60
return "" , fmt .Errorf ("failed to dial content-service gRPC server: %w" , err )
@@ -34,9 +63,9 @@ func (c *Client) GetSignedUploadUrl(ctx context.Context) (string, error) {
34
63
35
64
uc := api .NewUsageReportServiceClient (conn )
36
65
37
- resp , err := uc .UploadURL (ctx , & api.UsageReportUploadURLRequest {Name : "some-name" })
66
+ resp , err := uc .UploadURL (ctx , & api.UsageReportUploadURLRequest {Name : key })
38
67
if err != nil {
39
- return "" , fmt .Errorf ("failed to obtain signed upload URL : %w" , err )
68
+ return "" , fmt .Errorf ("failed RPC to content service : %w" , err )
40
69
}
41
70
42
71
return resp .Url , nil
0 commit comments