gpt4 book ai didi

c# - 将文件从一个 Azure 存储帐户复制到另一个 Azure 存储帐户

转载 作者:行者123 更新时间:2023-12-03 01:41:05 24 4
gpt4 key购买 nike

我正在尝试使用 StartCopy 将文件从一个存储帐户复制到另一个帐户方法来复制文件。检查下面的代码。

CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(@"source storage account connection string");
CloudStorageAccount destStorageAccount = CloudStorageAccount.Parse(@"destination storage account connection string");

CloudBlobClient sourceBlobClient = sourceStorageAccount.CreateCloudBlobClient();
CloudBlobClient destBlobClient = destStorageAccount.CreateCloudBlobClient();
var sourceContainer = sourceBlobClient.GetContainerReference("sourceContainer");
var destContainer = destBlobClient.GetContainerReference("destContainer");

CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference("copy.txt");
CloudBlockBlob targetBlob = destContainer.GetBlockBlobReference("copy.txt");

targetBlob.StartCopy(sourceBlob);

但它总是返回以下错误。

Microsoft.WindowsAzure.Storage.StorageException: 'The remote server returned an error: (404) Not Found.'

我在这里缺少什么?

请注意,如果我尝试将文件从一个容器复制到同一存储帐户中的另一个容器,则相同的代码可以完美运行。

最佳答案

看一下以下示例,了解如何执行复制(取自 Introducing Asynchronous Cross-Account Copy Blob ):

public static void CopyBlobs(
CloudBlobContainer srcContainer,
string policyId,
CloudBlobContainer destContainer)
{
// get the SAS token to use for all blobs
string blobToken = srcContainer.GetSharedAccessSignature(
new SharedAccessBlobPolicy(), policyId);


var srcBlobList = srcContainer.ListBlobs(true, BlobListingDetails.None);
foreach (var src in srcBlobList)
{
var srcBlob = src as CloudBlob;

// Create appropriate destination blob type to match the source blob
CloudBlob destBlob;
if (srcBlob.Properties.BlobType == BlobType.BlockBlob)
{
destBlob = destContainer.GetBlockBlobReference(srcBlob.Name);
}
else
{
destBlob = destContainer.GetPageBlobReference(srcBlob.Name);
}

// copy using src blob as SAS
destBlob.StartCopyFromBlob(new Uri(srcBlob.Uri.AbsoluteUri + blobToken));
}
}

希望对你有帮助!

关于c# - 将文件从一个 Azure 存储帐户复制到另一个 Azure 存储帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53313831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com