@@ -5,10 +5,12 @@ import (
55 "fmt"
66 "io/fs"
77 "io/ioutil"
8+ "math/rand"
89 "net/url"
910 "os"
1011 "path/filepath"
1112 "regexp"
13+ "time"
1214
1315 logger "github.com/sirupsen/logrus"
1416 "github.com/tencentyun/cos-go-sdk-v5"
@@ -173,7 +175,7 @@ func GetObjectsList(c *cos.Client, prefix string, limit int, include string, exc
173175 isTruncated = false
174176 } else {
175177 isTruncated = res .IsTruncated
176- marker = res .NextMarker
178+ marker , _ = url . QueryUnescape ( res .NextMarker )
177179 }
178180 }
179181
@@ -191,7 +193,7 @@ func GetObjectsList(c *cos.Client, prefix string, limit int, include string, exc
191193
192194func GetObjectsListForLs (c * cos.Client , prefix string , limit int , include string , exclude string ,
193195 marker string ) (dirs []string ,
194- objects []cos.Object , isTruncated bool , nextMaker string ) {
196+ objects []cos.Object , isTruncated bool , nextMarker string ) {
195197 opt := & cos.BucketGetOptions {
196198 Prefix : prefix ,
197199 Delimiter : "/" ,
@@ -220,7 +222,7 @@ func GetObjectsListForLs(c *cos.Client, prefix string, limit int, include string
220222 isTruncated = false
221223 } else {
222224 isTruncated = res .IsTruncated
223- nextMaker = res .NextMarker
225+ nextMarker , _ = url . QueryUnescape ( res .NextMarker )
224226 }
225227
226228 if len (include ) > 0 {
@@ -232,13 +234,19 @@ func GetObjectsListForLs(c *cos.Client, prefix string, limit int, include string
232234 dirs = MatchPattern (dirs , exclude , false )
233235 }
234236
235- return dirs , objects , isTruncated , nextMaker
237+ return dirs , objects , isTruncated , nextMarker
236238}
237239
238- func CheckCosPathType (c * cos.Client , prefix string , limit int ) (isDir bool ) {
240+ func CheckCosPathType (c * cos.Client , prefix string , limit int , retryCount ... int ) (isDir bool ) {
239241 if prefix == "" {
240242 return true
241243 }
244+
245+ retries := 0
246+ if len (retryCount ) > 0 {
247+ retries = retryCount [0 ]
248+ }
249+
242250 opt := & cos.BucketGetOptions {
243251 Prefix : prefix ,
244252 Delimiter : "" ,
@@ -247,7 +255,7 @@ func CheckCosPathType(c *cos.Client, prefix string, limit int) (isDir bool) {
247255 MaxKeys : limit ,
248256 }
249257
250- res , _ , err := c . Bucket . Get ( context . Background () , opt )
258+ res , err := tryGetBucket ( c , opt , retries )
251259 if err != nil {
252260 logger .Fatalln (err )
253261 os .Exit (1 )
@@ -260,8 +268,14 @@ func CheckCosPathType(c *cos.Client, prefix string, limit int) (isDir bool) {
260268 return isDir
261269}
262270
263- func GetObjectsListRecursive (c * cos.Client , prefix string , limit int , include string , exclude string ) (objects []cos.Object ,
271+ func GetObjectsListRecursive (c * cos.Client , prefix string , limit int , include string , exclude string , retryCount ... int ) (objects []cos.Object ,
264272 commonPrefixes []string ) {
273+
274+ retries := 0
275+ if len (retryCount ) > 0 {
276+ retries = retryCount [0 ]
277+ }
278+
265279 opt := & cos.BucketGetOptions {
266280 Prefix : prefix ,
267281 Delimiter : "" ,
@@ -275,7 +289,7 @@ func GetObjectsListRecursive(c *cos.Client, prefix string, limit int, include st
275289 for isTruncated {
276290 opt .Marker = marker
277291
278- res , _ , err := c . Bucket . Get ( context . Background () , opt )
292+ res , err := tryGetBucket ( c , opt , retries )
279293 if err != nil {
280294 logger .Fatalln (err )
281295 os .Exit (1 )
@@ -288,7 +302,7 @@ func GetObjectsListRecursive(c *cos.Client, prefix string, limit int, include st
288302 isTruncated = false
289303 } else {
290304 isTruncated = res .IsTruncated
291- marker = res .NextMarker
305+ marker , _ = url . QueryUnescape ( res .NextMarker )
292306 }
293307 }
294308
@@ -305,6 +319,29 @@ func GetObjectsListRecursive(c *cos.Client, prefix string, limit int, include st
305319 return objects , commonPrefixes
306320}
307321
322+ func tryGetBucket (c * cos.Client , opt * cos.BucketGetOptions , retryCount int ) (* cos.BucketGetResult , error ) {
323+ for i := 0 ; i <= retryCount ; i ++ {
324+ res , resp , err := c .Bucket .Get (context .Background (), opt )
325+ if err != nil {
326+ if resp .StatusCode == 503 {
327+ if i == retryCount {
328+ return res , err
329+ } else {
330+ fmt .Println ("Error 503: Service Unavailable. Retrying..." )
331+ waitTime := time .Duration (rand .Intn (10 )+ 1 ) * time .Second
332+ time .Sleep (waitTime )
333+ continue
334+ }
335+ } else {
336+ return res , err
337+ }
338+ } else {
339+ return res , err
340+ }
341+ }
342+ return nil , fmt .Errorf ("Retry limit exceeded" )
343+ }
344+
308345func GetObjectsListRecursiveForLs (c * cos.Client , prefix string , limit int , include string , exclude string ,
309346 marker string ) (objects []cos.Object , isTruncated bool , nextMarker string , commonPrefixes []string ) {
310347 opt := & cos.BucketGetOptions {
@@ -331,7 +368,7 @@ func GetObjectsListRecursiveForLs(c *cos.Client, prefix string, limit int, inclu
331368 isTruncated = false
332369 } else {
333370 isTruncated = res .IsTruncated
334- nextMarker = res .NextMarker
371+ nextMarker , _ = url . QueryUnescape ( res .NextMarker )
335372 }
336373
337374 if len (include ) > 0 {
0 commit comments