@@ -6,7 +6,7 @@ package service
6
6
7
7
import (
8
8
"context"
9
-
9
+ "errors"
10
10
"github.com/opentracing/opentracing-go"
11
11
"google.golang.org/grpc/codes"
12
12
"google.golang.org/grpc/status"
@@ -42,20 +42,64 @@ func (us *UsageReportService) UploadURL(ctx context.Context, req *api.UsageRepor
42
42
span .SetTag ("name" , req .Name )
43
43
defer tracing .FinishSpan (span , & err )
44
44
45
- err = us .s .EnsureExists (ctx , us .bucketName )
45
+ if req .GetName () == "" {
46
+ return nil , status .Error (codes .InvalidArgument , "Name is required but got empty." )
47
+ }
48
+
49
+ logger := log .WithField ("name" , req .Name ).
50
+ WithField ("bucket" , us .bucketName )
51
+
52
+ err = us .ensureBucketExists (ctx )
46
53
if err != nil {
47
- return nil , status . Error ( codes . NotFound , err . Error ())
54
+ return nil , err
48
55
}
49
56
50
57
info , err := us .s .SignUpload (ctx , us .bucketName , req .Name , & storage.SignedURLOptions {
51
58
ContentType : "application/json" ,
52
59
})
53
60
if err != nil {
54
- log .WithField ("name" , req .Name ).
55
- WithField ("bucket" , us .bucketName ).
56
- WithError (err ).
57
- Error ("Error getting UsageReport SignUpload URL" )
58
- return nil , status .Error (codes .Unknown , err .Error ())
61
+ logger .WithError (err ).Error ("Error getting UsageReport SignUpload URL" )
62
+ return nil , status .Error (codes .Internal , err .Error ())
59
63
}
60
64
return & api.UsageReportUploadURLResponse {Url : info .URL }, nil
61
65
}
66
+
67
+ func (us * UsageReportService ) DownloadURL (ctx context.Context , req * api.UsageReportDownloadURLRequest ) (resp * api.UsageReportDownloadURLResponse , err error ) {
68
+ span , ctx := opentracing .StartSpanFromContext (ctx , "UsageReport.DownloadURL" )
69
+ span .SetTag ("name" , req .Name )
70
+ defer tracing .FinishSpan (span , & err )
71
+
72
+ if req .GetName () == "" {
73
+ return nil , status .Error (codes .InvalidArgument , "Name is required but got empty." )
74
+ }
75
+
76
+ err = us .ensureBucketExists (ctx )
77
+ if err != nil {
78
+ return nil , err
79
+ }
80
+
81
+ download , err := us .s .SignDownload (ctx , us .bucketName , req .GetName (), & storage.SignedURLOptions {
82
+ ContentType : "application/json" ,
83
+ })
84
+ if err != nil {
85
+ if errors .Is (err , storage .ErrNotFound ) {
86
+ return nil , status .Errorf (codes .NotFound , "Object %s does not exist." , req .GetName ())
87
+ }
88
+
89
+ return nil , status .Errorf (codes .Internal , "Failed to generate download URL for usage report: %s" , err .Error ())
90
+ }
91
+
92
+ return & api.UsageReportDownloadURLResponse {
93
+ Url : download .URL ,
94
+ }, nil
95
+ }
96
+
97
+ func (us * UsageReportService ) ensureBucketExists (ctx context.Context ) error {
98
+ err := us .s .EnsureExists (ctx , us .bucketName )
99
+ if err != nil {
100
+ log .WithError (err ).Errorf ("Bucket %s does not exist" , us .bucketName )
101
+ return status .Error (codes .Internal , err .Error ())
102
+ }
103
+
104
+ return nil
105
+ }
0 commit comments