AzCopy V10复制文件

时间:2019-08-16 10:34:40

标签: c# azcopy

尝试使用C#中的AzCopy V10将blob从一个容器复制到另一个容器时遇到问题。

当我通过命令行编写代码时,可以毫无问题地传输文件,但是当我尝试在C#中执行相同的操作时,会收到验证错误。

错误消息

failed to perform copy command due to error: cannot start job due to error: cannot list blobs, -> github.com/Azure/azure-storage-blob-go/azblob.newStorageError, /home/vsts/go/pkg/mod/github.com/!azure/azure-storage-blob-go@v0.7.0/azblob/zc_storage_error.go:42
===== RESPONSE ERROR (ServiceCode=AuthorizationPermissionMismatch) =====
Description=This request is not authorized to perform this operation using this permission.
Time:2019-08-16T10:21:18.0483745Z, Details:
   Code: AuthorizationPermissionMismatch
   User-Agent: [AzCopy/10.2.1 Azure-Storage/0.7 (go1.12; Windows_NT)]
   --------------------------------------------------------------------------------
   RESPONSE Status: 403 This request is not authorized to perform this operation using this permission.
   Content-Length: [279]
   Content-Type: [application/xml]
   Date: [Fri, 16 Aug 2019 10:21:17 GMT]
   Server: [Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0]
   X-Ms-Error-Code: [AuthorizationPermissionMismatch]     
   X-Ms-Version: [2018-11-09]

我的代码如下。

SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy
{
   SharedAccessStartTime = DateTimeOffset.UtcNow.AddDays(-1),
   SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddDays(1), // 1 day expired
   Permissions = SharedAccessBlobPermissions.Read  //Read & Write
};
//  var createFolder = items.GetBlockBlobReference(items.Name);

var AzCopyProcess = new Process();
AzCopyProcess.StartInfo.UseShellExecute = false;
AzCopyProcess.StartInfo.RedirectStandardOutput = true;
AzCopyProcess.StartInfo.FileName = strCommand;
//pass storage account name, container and the key        
//  Debug.WriteLine($"azcopy cp \"{items.Uri}{items.GetSharedAccessSignature(sasConstraints)}\" \"{dayBlob.Uri}{dayBlob.GetSharedAccessSignature(sasConstraints)}\" --recursive");
AzCopyProcess.StartInfo.Arguments = $"copy \"{items.Uri}{items.GetSharedAccessSignature(sasConstraints)}\" {" "} \"{dayBlob.Uri}{dayBlob.GetSharedAccessSignature(sasConstraints)}\" --recursive";
AzCopyProcess.Start();
StreamWriter stdOut = new StreamWriter(Console.OpenStandardOutput());
stdOut.AutoFlush = true;
Console.Write(stdOut);
var output = AzCopyProcess.StandardOutput.ReadToEnd();
Console.WriteLine($"{items.Name} {output}");

1 个答案:

答案 0 :(得分:1)

对于源,您需要将权限设置为“读取和列出”,以便它扫描文件。

Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List

对于目的地,您需要具有写权限。

Permissions = SharedAccessBlobPermissions.Write

由于仅使用单个SASPolicy,因此可以将它们组合在一起,尽管这确实意味着SAS令牌提供了一些该过程不需要的额外权限。

 Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List | SharedAccessBlobPermissions.Write