使用Go语言在Azure中按层次结构列出blob的问题

huangapple go评论80阅读模式
英文:

Problem listing blobs in azure hierarchily using Go

问题

在Python中,这段代码是有效的:

container_client.walk_blobs(name_starts_with=path + "/", delimiter="/")

它允许我迭代遍历存储在 Blob 存储中的顶级 "目录"。

我尝试在 Go 中使用以下方式实现相同的功能:

func getModelsHierarchy(client *azblob.Client, containerName string, directory string) {
	maxResults := int32(200)
	delimiter := "/"

	containerClient := client.ServiceClient().NewContainerClient(containerName)
	pager := containerClient.NewListBlobsHierarchyPager(delimiter, &container.ListBlobsHierarchyOptions{
		Include:    container.ListBlobsInclude{},
		MaxResults: &maxResults,
		Prefix:     &directory,
	})

	for pager.More() {
		resp, err := pager.NextPage(context.TODO())
		if err != nil {
			log.Fatal(err)
		}
		for _, blob := range resp.ListBlobsHierarchySegmentResponse.Segment.BlobItems {
			fmt.Println("Adding model " + *blob.Name)
		}
	}
}

根据文档https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob@v1.0.0/container#Client.NewListBlobsHierarchyPager,我认为这段代码应该是有效的。我已经在启用层次结构的 Blob 存储和数据湖上进行了测试,并且通过 Web 存储资源管理器验证了存在文件和目录。

结果:

使用 "/" 作为分隔符时,我没有得到任何结果。使用 "_" 作为分隔符时,我得到了所有文件的结果。

英文:

In python this works:

container_client.walk_blobs(name_starts_with=path + "/", delimiter="/")

It will allow me to iterate over top level "directories" in the blob storage.

I'm attempting to do the same in Go like this:

func getModelsHierarchy(client *azblob.Client, containerName string, directory string) {
	maxResults := int32(200)
	delimiter := "/"

	containerClient := client.ServiceClient().NewContainerClient(containerName)
	pager := containerClient.NewListBlobsHierarchyPager(delimiter, &container.ListBlobsHierarchyOptions{
		Include:    container.ListBlobsInclude{},
		MaxResults: &maxResults,
		Prefix:     &directory,
	})

	for pager.More() {
		resp, err := pager.NextPage(context.TODO())
		if err != nil {
			log.Fatal(err)
		}
		for _, blob := range resp.ListBlobsHierarchySegmentResponse.Segment.BlobItems {
			fmt.Println("Adding model " + *blob.Name)
		}
	}
}

According to docs at https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob@v1.0.0/container#Client.NewListBlobsHierarchyPager I think this should work. I've tested with a blob storage and a data lake gen 2 with hierarchy turned on. I have checked that there are files and directories using the web storage explorer.

Results:

With "/" as delimiter I get no results back. With "_" as delimiter I get all files back.

答案1

得分: 1

如果我理解正确,您希望仅迭代目录(而不是 blob)。正如在这个讨论中提到的那样,Go SDK当前不支持这个功能。

REST API通过showonly={deleted,files,directories} URI参数支持此功能,但似乎SDK没有实现这个转换。我查看了代码,没有找到相关内容,也没有找到一种将其添加到REST GET字符串变量中的方法。

也许一个解决方法是直接使用其中一种身份验证方法来实现REST API。

英文:

If I understood, you want to iterate only over the directories (not the blobs). As mentioned in this discussion, this is currently not natively supported by the Go SDK.

The REST API supports it via the showonly={deleted,files,directories} URI parameter, however it looks like the SDK did not implement this translation. I looked over the code and couldn't find it, nor a way to hack it into the REST GET string var.

Perhaps a workaround is to do a REST API implementation directly using one of these authentication methods.

huangapple
  • 本文由 发表于 2023年5月4日 22:10:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76174152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定