当前,我使用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
}