我正在嘗試將資料從 Blob 容器發送到檔案共享,這是我正在使用的代碼,但令人驚訝的是它不起作用:
public static void MoveFileBlobToFileShare(string shareName, string fileName, string sourceDirName, string destinationDirName, string container, string destinationFileName = "")
{
var blobServiceClient = Zeus.AzureStorage.Common.CreateblobServiceClient();
var containerClient = blobServiceClient.GetBlobContainerClient(container);
var blobClient = containerClient.GetBlobClient(sourceDirName "/" fileName);
var fileSas = blobClient.GenerateSasUri(Azure.Storage.Sas.BlobSasPermissions.Read, DateTime.UtcNow.AddHours(24));
var shareClient = Common.CreateSMBClientFromConnectionString(shareName);
ShareDirectoryClient directory = shareClient.GetDirectoryClient(destinationDirName);
ShareFileClient file = string.Equals(string.Empty, destinationFileName) ?
directory.GetFileClient(fileName) : directory.GetFileClient(destinationFileName);
file.StartCopy(fileSas);
blobClient.Delete();
}
無論如何都可以使用blobClient.URI和擺脫fileSAS
我收到的錯誤:
The specified blob does not exist.
RequestId:b19857e0-001a-0008-670a-5a8332000000
Time:2022-04-27T07:46:55.4219904Z
Status: 404 (The specified blob does not exist.)
ErrorCode: CannotVerifyCopySource
Content:
<?xml version="1.0" encoding="utf-8"?><Error><Code>CannotVerifyCopySource</Code><Message>The specified blob does not exist.
RequestId:b19857e0-001a-0008-670a-5a8332000000
Time:2022-04-27T07:46:55.4219904Z</Message></Error>
Headers:
x-ms-request-id: b19857e0-001a-0008-670a-5a8332000000
x-ms-client-request-id: 697e573f-1ef0-47e5-971e-97f67b4fa083
x-ms-version: 2021-04-10
x-ms-error-code: CannotVerifyCopySource
Content-Length: 225
Content-Type: application/xml
Date: Wed, 27 Apr 2022 07:46:55 GMT
Server: Windows-Azure-File/1.0 Microsoft-HTTPAPI/2.0
uj5u.com熱心網友回復:
無論如何只使用 blobClient.URI 并擺脫 fileSAS
使用 Microsoft.WindowsAzure.Storage 時,一種解決方法是生成 SAS,然后使用以下代碼獲取 Uri。
Uri blobSASUri = new Uri(sourceBlob.StorageUri.PrimaryUri.ToString() blobSAS);
如果您使用的是 Azure.Storage.Blobs 那么下面的作品
string sasuri = "<SAS>";
Uri blobUri = new Uri(sasuri);
下面是我們使用 WindowsAzure.Storage 將檔案從 Blob 存盤傳輸到檔案共享的代碼。
static async Task Main(string[] args)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("<Connection_String>");
// To get Blob References
var blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference("container1");
CloudBlockBlob sourceBlob = cloudBlobContainer.GetBlockBlobReference("TextSample.txt");
// To get FileShare References
CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient();
CloudFileShare cloudFileShare = cloudFileClient.GetShareReference("fileshare1");
CloudFile destinationFileShare = cloudFileShare.GetRootDirectoryReference().GetFileReference("TextSample.txt");
// Generating SAS
string blobSAS = sourceBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
});
// Copying Blob To FileShare
Uri blobSASUri = new Uri(sourceBlob.StorageUri.PrimaryUri.ToString() blobSAS);
await destinationFileShare.StartCopyAsync(blobSASUri);
// Deleting when Transfer is done
await sourceBlob.DeleteIfExistsAsync();
Console.WriteLine("Transfer Complete!");
}
下面是使用 Azure.Storage.blob 時的代碼。
static async Task Main(string[] args)
{
// To get Blob References
BlobServiceClient serviceClient = new BlobServiceClient("<Connection_String>");
BlobContainerClient containerClient = serviceClient.GetBlobContainerClient("container1");
BlobClient sourceBlob = containerClient.GetBlobClient("TextSample.txt");
// To get FileShare References
ShareFileClient destinationFileShare = new ShareFileClient("<Connection_String>", "fileshare1", "TextSample.txt");
// Generating SAS
Uri blobSAS = sourceBlob.GenerateSasUri(Azure.Storage.Sas.BlobSasPermissions.Read, DateTime.UtcNow.AddHours(24));
// Copying Blob To FileShare
await destinationFileShare.StartCopyAsync(blobSAS);
// Deleting when Transfer is done
await sourceBlob.DeleteIfExistsAsync();
Console.WriteLine("Completed Trasfer!");
}
結果:

更新的答案
BlobServiceClient blobServiceClient = new BlobServiceClient("<ConnectionString>");
var containerClient = blobServiceClient.GetBlobContainerClient(container);
var blobClient = containerClient.GetBlobClient(sourceDirName "/" fileName);
var fileSas = blobClient.GenerateSasUri(Azure.Storage.Sas.BlobSasPermissions.Read, DateTime.UtcNow.AddHours(24));
ShareClient shareClient = new ShareClient("<ConnectionString>", shareName);
ShareDirectoryClient directory = shareClient.GetDirectoryClient(destinationDirName);
ShareFileClient file = string.Equals(string.Empty, destinationFileName) ? directory.GetFileClient(fileName) : directory.GetFileClient(destinationFileName);
file.StartCopy(fileSas);
blobClient.Delete();
uj5u.com熱心網友回復:
問題是您開始復制操作但不等待它完成。相反,您洗掉作為復制操作的源操作的檔案
file.StartCopy(fileSas);
此呼叫啟動復制操作。它在后臺運行。它回傳一個ShareFileCopyInfo實體,您可以檢查以查看操作的狀態。該屬性CopyStatus是一個具有 Aborted、Failed、Pending 和 Success 值的列舉,請參閱檔案。
我懷疑在您的情況下,當您致電時,該操作仍在等待中blobClient.Delete();。
根據官方示例,您可以這樣做以等待復制操作完成:
// Start the copy operation
file.StartCopy(fileSas);
if (await file.ExistsAsync())
{
Console.WriteLine($"{sourceFile.Uri} copied to {destFile.Uri}");
}
另一種選擇是輪詢完成。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/465552.html
