MCPcopy
hub / github.com/cortexlabs/cortex / ListS3TopLevelDirs

Method ListS3TopLevelDirs

pkg/lib/aws/s3.go:164–200  ·  view source on GitHub ↗

Efficient way to list all top-level directory prefixes on a bucket. Warning: when using this function, make sure the "~" character isn't used in any of the S3 objects.

(bucket string)

Source from the content-addressed store, hash-verified

162//
163// Warning: when using this function, make sure the "~" character isn't used in any of the S3 objects.
164func (c *Client) ListS3TopLevelDirs(bucket string) ([]string, error) {
165 // find first top-level directory
166 s3Objects, err := c.ListS3Prefix(bucket, "", false, pointer.Int64(1), nil)
167 if err != nil {
168 return nil, err
169 }
170 dirs := []string{}
171 for _, prefix := range ConvertS3ObjectsToKeys(s3Objects...) {
172 dirs = append(dirs, files.GetTopLevelDirectory(prefix))
173 }
174
175 // detect all remaining top-level dirs
176 for {
177 if len(dirs) == 0 {
178 break
179 }
180 previousDir := dirs[len(dirs)-1]
181 s3Objects, err := c.ListS3Prefix(
182 bucket,
183 "",
184 true,
185 pointer.Int64(1),
186 pointer.String(filepath.Join(previousDir, "~~~")),
187 )
188 if err != nil {
189 return nil, err
190 }
191 if len(ConvertS3ObjectsToKeys(s3Objects...)) == 0 {
192 break
193 }
194 for _, prefix := range ConvertS3ObjectsToKeys(s3Objects...) {
195 dirs = append(dirs, files.GetTopLevelDirectory(prefix))
196 }
197 }
198
199 return dirs, nil
200}
201
202func ConvertS3ObjectsToKeys(s3Objects ...*s3.Object) []string {
203 paths := make([]string, 0, len(s3Objects))

Callers 1

Calls 5

ListS3PrefixMethod · 0.95
Int64Function · 0.92
GetTopLevelDirectoryFunction · 0.92
StringFunction · 0.92
ConvertS3ObjectsToKeysFunction · 0.85

Tested by

no test coverage detected