将文件从Azure文件共享复制到Azure Blob的C#代码

时间:2016-02-17 14:28:49

标签: azure azure-blob-storage azure-files

我正在寻找将文件从Azure文件共享复制到Azure Blob的解决方案。

2 个答案:

答案 0 :(得分:2)

终于搞定了。

string rootFolder = "root";

string mainFolder = "MainFolder";

string fileshareName = "testfileshare";

string containerName = "container";

string connectionString = "Provide StorageConnectionString here";

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

        // Create a new file share, if it does not already exist.
        CloudFileShare share = fileClient.GetShareReference(fileshareName);
        share.CreateIfNotExists();

        // Create a new file in the root directory.

        CloudFileDirectory rootDir = share.GetRootDirectoryReference();

        CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(rootFolder);


        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(containerName.ToLower());
        container.CreateIfNotExists();

        foreach (var Files in sampleDir.ListFilesAndDirectories())
        {
            char strdelim = '/';
            string path = Files.Uri.ToString();
            var arr = Files.Uri.ToString().Split(strdelim);
            string strFileName = arr[arr.Length - 1];

            Console.WriteLine("\n" + strFileName);

            CloudFile sourceFile = sampleDir.GetFileReference(strFileName);

            string fileSas = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy()
            {
                // Only read permissions are required for the source file.
                Permissions = SharedAccessFilePermissions.Read,
                SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
            });

            Uri fileSasUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSas);

            string blob = mainFolder + "\\" + strFileName;

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(blob);

            blockBlob.StartCopy(fileSasUri);

        }

答案 1 :(得分:0)

感谢您的帮助,这正是我要找的。还可以直接访问 Microsoft Docs 获得更多帮助,以处理 blob 上的文件。

这里有更多帮助:

  • 下载一个 blob
  • 上传 Blob
  • 在存储帐户之间复制 Blob
<块引用>
CloudBlockBlob destBlob = destContainer.GetBlockBlobReference(sourceBlob.Name);
await destBlob.StartCopyAsync(new Uri(GetSharedAccessUri(sourceBlob.Name, sourceContainer)));

// Create a SAS token for the source blob, to enable it to be read by the StartCopyAsync method
private static string GetSharedAccessUri(string blobName, CloudBlobContainer container)
{
    DateTime toDateTime = DateTime.Now.AddMinutes(60);

    SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy
    {
        Permissions = SharedAccessBlobPermissions.Read,
        SharedAccessStartTime = null,
        SharedAccessExpiryTime = new DateTimeOffset(toDateTime)
    };

    CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
    string sas = blob.GetSharedAccessSignature(policy);

    return blob.Uri.AbsoluteUri + sas;
}