Selaa lähdekoodia

Merge pull request #1707 from kaelanspatel/kaelan-fix-azure-listdirectories

Fix ListDirectories for Azure Storage
Kaelan Patel 3 vuotta sitten
vanhempi
sitoutus
6c87510326
1 muutettua tiedostoa jossa 10 lisäystä ja 52 poistoa
  1. 10 52
      pkg/storage/azurestorage.go

+ 10 - 52
pkg/storage/azurestorage.go

@@ -270,28 +270,6 @@ func (b *AzureStorage) Stat(name string) (*StorageInfo, error) {
 	}, nil
 }
 
-func (b *AzureStorage) StatDirectories(name string) (*StorageInfo, error) {
-	name = trimLeading(name)
-	ctx := context.Background()
-
-	blobURL := getBlobURL(name, b.containerURL)
-	props, err := blobURL.GetProperties(ctx, blob.BlobAccessConditions{}, blob.ClientProvidedKeyOptions{})
-	if err != nil {
-		return nil, err
-	}
-
-	if trimName(name) == "" {
-		return &StorageInfo{
-			Name:    trimName(name),
-			Size:    props.ContentLength(),
-			ModTime: props.LastModified(),
-		}, nil
-	} else {
-		return nil, fmt.Errorf("non-directory in dir")
-	}
-
-}
-
 // Read uses the relative path of the storage combined with the provided path to
 // read the contents.
 func (b *AzureStorage) Read(name string) ([]byte, error) {
@@ -434,9 +412,10 @@ func (b *AzureStorage) List(path string) ([]*StorageInfo, error) {
 }
 
 func (b *AzureStorage) ListDirectories(path string) ([]*StorageInfo, error) {
+
 	path = trimLeading(path)
 
-	log.Debugf("AzureStorage::List(%s)", path)
+	log.Debugf("AzureStorage::ListDirectories(%s)", path)
 	ctx := context.Background()
 
 	// Ensure the object name actually ends with a dir suffix. Otherwise we'll just iterate the
@@ -448,9 +427,9 @@ func (b *AzureStorage) ListDirectories(path string) ([]*StorageInfo, error) {
 	marker := blob.Marker{}
 	listOptions := blob.ListBlobsSegmentOptions{Prefix: path}
 
-	var names []string
+	var stats []*StorageInfo
 	for i := 1; ; i++ {
-		var blobItems []blob.BlobItemInternal
+		var blobPrefixes []blob.BlobPrefix
 
 		list, err := b.containerURL.ListBlobsHierarchySegment(ctx, marker, DirDelim, listOptions)
 		if err != nil {
@@ -458,10 +437,12 @@ func (b *AzureStorage) ListDirectories(path string) ([]*StorageInfo, error) {
 		}
 
 		marker = list.NextMarker
-		blobItems = list.Segment.BlobItems
+		blobPrefixes = list.Segment.BlobPrefixes
 
-		for _, blob := range blobItems {
-			names = append(names, blob.Name)
+		for _, prefix := range blobPrefixes {
+			stats = append(stats, &StorageInfo{
+				Name: trimLeading(prefix.Name),
+			})
 		}
 
 		// Continue iterating if we are not done.
@@ -469,32 +450,9 @@ func (b *AzureStorage) ListDirectories(path string) ([]*StorageInfo, error) {
 			break
 		}
 
-		log.Debugf("Requesting next iteration of listing blobs. Entries: %d, iteration: %d", len(names), i)
+		log.Debugf("Requesting next iteration of listing blobs. Entries: %d, iteration: %d", len(stats), i)
 	}
 
-	// get the storage information for each blob (really unfortunate we have to do this)
-	var lock sync.Mutex
-	var stats []*StorageInfo
-	var wg sync.WaitGroup
-	wg.Add(len(names))
-
-	for i := 0; i < len(names); i++ {
-		go func(n string) {
-			defer wg.Done()
-
-			stat, err := b.StatDirectories(n)
-			if err != nil {
-				log.Errorf("Error statting blob %s: %s", n, err)
-			} else {
-				lock.Lock()
-				stats = append(stats, stat)
-				lock.Unlock()
-			}
-		}(names[i])
-	}
-
-	wg.Wait()
-
 	return stats, nil
 }