9
9
"net/http"
10
10
"time"
11
11
12
- "github.com/ncw/swift"
12
+ swift "github.com/ncw/swift/v2 "
13
13
"github.com/pkg/errors"
14
14
"github.com/prometheus/client_golang/prometheus"
15
15
@@ -61,7 +61,7 @@ func NewSwiftObjectClient(cfg SwiftConfig, hedgingCfg hedging.Config) (*SwiftObj
61
61
return nil , err
62
62
}
63
63
// Ensure the container is created, no error is returned if it already exists.
64
- if err := c .ContainerCreate (cfg .ContainerName , nil ); err != nil {
64
+ if err := c .ContainerCreate (context . Background (), cfg . Config .ContainerName , nil ); err != nil {
65
65
return nil , err
66
66
}
67
67
hedging , err := createConnection (cfg , hedgingCfg , true )
@@ -78,30 +78,30 @@ func NewSwiftObjectClient(cfg SwiftConfig, hedgingCfg hedging.Config) (*SwiftObj
78
78
func createConnection (cfg SwiftConfig , hedgingCfg hedging.Config , hedging bool ) (* swift.Connection , error ) {
79
79
// Create a connection
80
80
c := & swift.Connection {
81
- AuthVersion : cfg .AuthVersion ,
82
- AuthUrl : cfg .AuthURL ,
83
- Internal : cfg .Internal ,
84
- ApiKey : cfg .Password ,
85
- UserName : cfg .Username ,
86
- UserId : cfg .UserID ,
87
- Retries : cfg .MaxRetries ,
88
- ConnectTimeout : cfg .ConnectTimeout ,
89
- Timeout : cfg .RequestTimeout ,
90
- TenantId : cfg .ProjectID ,
91
- Tenant : cfg .ProjectName ,
92
- TenantDomain : cfg .ProjectDomainName ,
93
- TenantDomainId : cfg .ProjectDomainID ,
94
- Domain : cfg .DomainName ,
95
- DomainId : cfg .DomainID ,
96
- Region : cfg .RegionName ,
81
+ AuthVersion : cfg .Config . AuthVersion ,
82
+ AuthUrl : cfg .Config . AuthURL ,
83
+ Internal : cfg .Config . Internal ,
84
+ ApiKey : cfg .Config . Password ,
85
+ UserName : cfg .Config . Username ,
86
+ UserId : cfg .Config . UserID ,
87
+ Retries : cfg .Config . MaxRetries ,
88
+ ConnectTimeout : cfg .Config . ConnectTimeout ,
89
+ Timeout : cfg .Config . RequestTimeout ,
90
+ TenantId : cfg .Config . ProjectID ,
91
+ Tenant : cfg .Config . ProjectName ,
92
+ TenantDomain : cfg .Config . ProjectDomainName ,
93
+ TenantDomainId : cfg .Config . ProjectDomainID ,
94
+ Domain : cfg .Config . DomainName ,
95
+ DomainId : cfg .Config . DomainID ,
96
+ Region : cfg .Config . RegionName ,
97
97
Transport : defaultTransport ,
98
98
}
99
99
100
100
switch {
101
- case cfg .UserDomainName != "" :
102
- c .Domain = cfg .UserDomainName
103
- case cfg .UserDomainID != "" :
104
- c .DomainId = cfg .UserDomainID
101
+ case cfg .Config . UserDomainName != "" :
102
+ c .Domain = cfg .Config . UserDomainName
103
+ case cfg .Config . UserDomainID != "" :
104
+ c .DomainId = cfg .Config . UserDomainID
105
105
}
106
106
if hedging {
107
107
var err error
@@ -111,7 +111,7 @@ func createConnection(cfg SwiftConfig, hedgingCfg hedging.Config, hedging bool)
111
111
}
112
112
}
113
113
114
- err := c .Authenticate ()
114
+ err := c .Authenticate (context . TODO () )
115
115
if err != nil {
116
116
return nil , err
117
117
}
@@ -124,8 +124,8 @@ func (s *SwiftObjectClient) Stop() {
124
124
s .hedgingConn .UnAuthenticate ()
125
125
}
126
126
127
- func (s * SwiftObjectClient ) ObjectExists (_ context.Context , objectKey string ) (bool , error ) {
128
- _ , _ , err := s .hedgingConn .Object (s .cfg .ContainerName , objectKey )
127
+ func (s * SwiftObjectClient ) ObjectExists (ctx context.Context , objectKey string ) (bool , error ) {
128
+ _ , _ , err := s .hedgingConn .Object (ctx , s .cfg . Config .ContainerName , objectKey )
129
129
if err != nil {
130
130
return false , err
131
131
}
@@ -134,9 +134,9 @@ func (s *SwiftObjectClient) ObjectExists(_ context.Context, objectKey string) (b
134
134
}
135
135
136
136
// GetObject returns a reader and the size for the specified object key from the configured swift container.
137
- func (s * SwiftObjectClient ) GetObject (_ context.Context , objectKey string ) (io.ReadCloser , int64 , error ) {
137
+ func (s * SwiftObjectClient ) GetObject (ctx context.Context , objectKey string ) (io.ReadCloser , int64 , error ) {
138
138
var buf bytes.Buffer
139
- _ , err := s .hedgingConn .ObjectGet (s .cfg .ContainerName , objectKey , & buf , false , nil )
139
+ _ , err := s .hedgingConn .ObjectGet (ctx , s .cfg . Config .ContainerName , objectKey , & buf , false , nil )
140
140
if err != nil {
141
141
return nil , 0 , err
142
142
}
@@ -145,12 +145,12 @@ func (s *SwiftObjectClient) GetObject(_ context.Context, objectKey string) (io.R
145
145
}
146
146
147
147
// GetObject returns a reader and the size for the specified object key from the configured swift container.
148
- func (s * SwiftObjectClient ) GetObjectRange (_ context.Context , objectKey string , offset , length int64 ) (io.ReadCloser , error ) {
148
+ func (s * SwiftObjectClient ) GetObjectRange (ctx context.Context , objectKey string , offset , length int64 ) (io.ReadCloser , error ) {
149
149
var buf bytes.Buffer
150
150
h := swift.Headers {
151
151
"Range" : fmt .Sprintf ("bytes=%d-%d" , offset , offset + length - 1 ),
152
152
}
153
- _ , err := s .hedgingConn .ObjectGet (s .cfg .ContainerName , objectKey , & buf , false , h )
153
+ _ , err := s .hedgingConn .ObjectGet (ctx , s .cfg . Config .ContainerName , objectKey , & buf , false , h )
154
154
if err != nil {
155
155
return nil , err
156
156
}
@@ -159,13 +159,13 @@ func (s *SwiftObjectClient) GetObjectRange(_ context.Context, objectKey string,
159
159
}
160
160
161
161
// PutObject puts the specified bytes into the configured Swift container at the provided key
162
- func (s * SwiftObjectClient ) PutObject (_ context.Context , objectKey string , object io.Reader ) error {
163
- _ , err := s .conn .ObjectPut (s .cfg .ContainerName , objectKey , object , false , "" , "" , nil )
162
+ func (s * SwiftObjectClient ) PutObject (ctx context.Context , objectKey string , object io.Reader ) error {
163
+ _ , err := s .conn .ObjectPut (ctx , s .cfg . Config .ContainerName , objectKey , object , false , "" , "" , nil )
164
164
return err
165
165
}
166
166
167
167
// List only objects from the store non-recursively
168
- func (s * SwiftObjectClient ) List (_ context.Context , prefix , delimiter string ) ([]client.StorageObject , []client.StorageCommonPrefix , error ) {
168
+ func (s * SwiftObjectClient ) List (ctx context.Context , prefix , delimiter string ) ([]client.StorageObject , []client.StorageCommonPrefix , error ) {
169
169
if len (delimiter ) > 1 {
170
170
return nil , nil , fmt .Errorf ("delimiter must be a single character but was %s" , delimiter )
171
171
}
@@ -177,7 +177,7 @@ func (s *SwiftObjectClient) List(_ context.Context, prefix, delimiter string) ([
177
177
opts .Delimiter = []rune (delimiter )[0 ]
178
178
}
179
179
180
- objs , err := s .conn .ObjectsAll (s .cfg .ContainerName , opts )
180
+ objs , err := s .conn .ObjectsAll (ctx , s .cfg . Config .ContainerName , opts )
181
181
if err != nil {
182
182
return nil , nil , err
183
183
}
@@ -203,8 +203,8 @@ func (s *SwiftObjectClient) List(_ context.Context, prefix, delimiter string) ([
203
203
}
204
204
205
205
// DeleteObject deletes the specified object key from the configured Swift container.
206
- func (s * SwiftObjectClient ) DeleteObject (_ context.Context , objectKey string ) error {
207
- return s .conn .ObjectDelete (s .cfg .ContainerName , objectKey )
206
+ func (s * SwiftObjectClient ) DeleteObject (ctx context.Context , objectKey string ) error {
207
+ return s .conn .ObjectDelete (ctx , s .cfg . Config .ContainerName , objectKey )
208
208
}
209
209
210
210
// IsObjectNotFoundErr returns true if error means that object is not found. Relevant to GetObject and DeleteObject operations.
0 commit comments