azure - 我可以复制azure blob文件夹而不遍历每个blob文件吗?

标签 azure go azure-storage azure-storage-blobs

当前,我使用golang实施从一个存储帐户到另一个存储帐户的复制文件。

例如:

https://storage1.blob.core.windows.net/container/A/B/file1.mp4
https://storage1.blob.core.windows.net/container/A/B/file2.mp4
https://storage1.blob.core.windows.net/container/A/B/file3.mp4


Copy from

https://storage1.blob.core.windows.net/container/A/B/
to 
https://storage2.blob.core.windows.net/container/A/B/

我可以复制整个文件夹而不遍历文件夹A / B中的所有文件吗?

这是我的示例代码



func (css StorageOperator) NestedListBlobs(ctx context.Context, srcFolderPath string) ([]azblob.BlobURL, error) {

    containerName, folderPath := css.getContainerNameAndPath(srcFolderPath)
    containerRawUrl := "https://" + css.StorageName + "." + lib.AzureBlobUrl + "/" + containerName
    containerUrl, _ := url.Parse(containerRawUrl)
    po := azblob.NewPipeline(css.sharedKeyCredential, css.createPipelineOptions())
    container := azblob.NewContainerURL(*containerUrl, po)

    var blobUrls []azblob.BlobURL

    // List the blob(s) in our container; since a container may hold millions of blobs, this is done 1 segment at a time.
    for marker := (azblob.Marker{}); marker.NotDone(); {
        // The parens around Marker{} are required to avoid compiler error.
        // Get a result segment starting with the blob indicated by the current Marker.
        listBlob, err := container.ListBlobsFlatSegment(ctx, marker, azblob.ListBlobsSegmentOptions{
            Prefix: folderPath,
        })
        if err != nil {
            return nil, err
        }
        // IMPORTANT: ListBlobs returns the start of the next segment; you MUST use this to get
        // the next segment (after processing the current result segment).
        marker = listBlob.NextMarker

        // Process the blobs returned in this result segment (if the segment is empty, the loop body won't execute)
        for _, blobInfo := range listBlob.Segment.BlobItems {
            css.logger.Debugf("Blob name: " + containerRawUrl + "/" + blobInfo.Name)

            blobUrl, _ := url.Parse(containerRawUrl + "/" + blobInfo.Name)
            blobUrls = append(blobUrls, azblob.NewBlobURL(*blobUrl, po))
        }
    }

    return blobUrls, nil
}

func (css StorageOperator) parallelCopyFileToDst(ctx context.Context, po pipeline.Pipeline, dstRawUrl string, srcBlobs []azblob.BlobURL) error {


for _, srcBlob := range srcBlobs {


dstUrl, _ := url.Parse(dstRawUrl + css.extractBlobPath(srcBlob))

dstBlobURL := azblob.NewBlobURL(*dstUrl, po)
srcSASUrl, err := css.createSASURL(css.lukeSharedKeyCredential, srcBlob.String())
if err != nil {
    css.logger.Errorf("createSASURL fail:%v", err)
    return
}
startCopy, err := dstBlobURL.StartCopyFromURL(ctx,
                *srcSASUrl,
                azblob.Metadata{},
                azblob.ModifiedAccessConditions{},
                azblob.BlobAccessConditions{})

if err != nil {
    css.logger.Errorf("startCopy fail:%v", err)
    return
}

if err = css.checkCopyStatus(ctx, dstBlobURL, startCopy); err != nil {
                css.logger.Errorf("checkCopyStatus fail:%v", err)
                return
}


}//for
    return nil
}

最佳答案

天蓝色blob内的文件夹结构不存在,而是使用路径名,因此,如果要在“虚拟文件夹”中复制blob,您所需要做的就是使用路径,以下是python列出blob的示例:

  ['Virtual-Folder/Boards.png', 'storage2.gif']

因此,为了传输Boards.png Blob,您必须在文件夹名称之前附加文件夹名称,而不要在文件夹中查找。我用python写了一些与此示例/问题有关的教程:https://github.com/adamsmith0016/Azure-storage

关于azure - 我可以复制azure blob文件夹而不遍历每个blob文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58691342/

相关文章:

c# - 如何更改 Windows Azure 上 IIS 7 上应用程序池的属性 Enable32BitAppOnWin64?

GOPATH 和 Go Web 服务器 : 'go run myserver.go'

go - 如何在 Go 中使用用户输入的 URL?

c# - Azure BlobWriteStream 客户端无法在指定超时内完成操作

c# - 天蓝色存储模拟器(表)返回 400 错误请求或 403 禁止

azure - 使用 Azure Powershell 为存储帐户设置自定义域

Azure - 使用 ARM 模板交换生产和暂存槽

google-app-engine - 如何切换google app engine账号来部署应用?

node.js - Nodejs 使用 .createBlockBlobFromLocalFile() 将 base64 图像上传到 azure blob 存储

python-3.x - 如何将 Azure-cli 命令的输出保存在变量中