【问题标题】:Move files from azure file shares into blob storage by using c#使用 c# 将文件从 azure 文件共享移动到 blob 存储
【发布时间】:2020-02-29 19:26:14
【问题描述】:

我使用 azure 函数从 FTP 服务器下载了一个文件,并将其保存在我从这段代码中获得的目标中:

var target = Path.Combine(context.FunctionAppDirectory, "File.CSV");

我们可以在“Microsoft Azure 存储资源管理器”中看到的“文件共享”中的某个位置。

现在我的问题是如何将此文件从文件共享复制到 Blob 容器或直接将其保存到 Azure SQL 可以访问的 Blob 存储?

【问题讨论】:

    标签: c# azure azure-blob-storage fileshare


    【解决方案1】:

    我们可以使用CloudBlockBlob.StartCopy(CloudFile)。您可以参考以下代码:

    using System;
    using Microsoft.Azure.Storage;
    using Microsoft.Azure.Storage.Blob;
    using Microsoft.Azure.Storage.File;
    
    namespace ConsoleApp3
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Parse the connection string for the storage account.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=*************");
    
                // Create a CloudFileClient object for credentialed access to File storage.
                CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    
                // Get a reference to the file share you created previously.
                CloudFileShare share = fileClient.GetShareReference("hurytest");
    
                // Get a reference to the file("test.csv") which I have uploaded to the file share("hurytest")
                CloudFile sourceFile = share.GetRootDirectoryReference().GetFileReference("test.csv");
    
                // Get a reference to the blob to which the file will be copied.(I have created a container with name of "targetcontainer")
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference("targetcontainer");
                //container.CreateIfNotExists();
                CloudBlockBlob destBlob = container.GetBlockBlobReference("test.csv");
    
                // Create a SAS for the file that's valid for 24 hours.
                // Note that when you are copying a file to a blob, or a blob to a file, you must use a SAS
                // to authenticate access to the source object, even if you are copying within the same
                // storage account.
                string fileSas = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy()
                {
                    // Only read permissions are required for the source file.
                    Permissions = SharedAccessFilePermissions.Read,
                    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
                });
    
                // Construct the URI to the source file, including the SAS token.
                Uri fileSasUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSas);
    
                // Copy the file to the blob.
                destBlob.StartCopy(fileSasUri);
            }
        }
    }
    

    希望对你的问题有所帮助~

    【讨论】:

    • 我在这个 Uri fileSasUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSas)中得到一个错误;
    • 嗨@sishanov,你能分享一下错误信息吗?
    • 指定的资源名称包含无效字符。 @hury
    • 嗨@sishanov,抱歉耽搁了。我刚才测试了一下,代码复制操作成功,我没有遇到你提到的同样的错误。我更新了答案中的代码,您能否检查一下您的代码和我的代码之间是否有任何差异?如果还有问题,请告诉我。
    • 嗨@sishanov,有更新吗?请问您解决了这个问题吗?
    【解决方案2】:

    使用以下扩展上传到 azure:

        /// <summary>
        /// </summary>
        /// <param name="file"></param>
        /// <param name="fileName"></param>
        /// <param name="connectionString"></param>
        /// <param name="containerName"></param>
        /// <param name="blobContentType"></param>
        /// <returns></returns>
        public static async Task<string> AzureUpload(this Stream file, string fileName, string connectionString, string containerName, string blobContentType = null)
        {
            CloudBlobClient blobClient = CloudStorageAccount.Parse(connectionString).CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            if (await container.CreateIfNotExistsAsync())
            {
               // Comment this code below if you don't want your files
               // to be publicly available. By default, a container is private.
               // You can see more on how
               // to set different container permissions at: 
               // https://docs.microsoft.com/en-us/azure/storage/blobs/storage-manage-access-to-resources
                await container.SetPermissionsAsync(new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob });
            }
    
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
    
            await blockBlob.UploadFromStreamAsync(file);
    
            blobContentType = blobContentType.HasValue() ? blobContentType : getBlobContentType(fileName);
            if (blobContentType.HasValue())
            {
                blockBlob.Properties.ContentType = blobContentType;
                await blockBlob.SetPropertiesAsync();
            }
    
            return blockBlob.Uri.AbsoluteUri;
        }
    

    做这样的事情:

    var target = Path.Combine(context.FunctionAppDirectory, "File.CSV");
    FileStream fileStream = new FileStream(target, FileMode.Open, FileAccess.Read);;
    string azureUriForUploadedCSV = await fileStream.AzureUpload(
                    "File.CSV",
                    "StorageConnectionString",
                    "csv-folder",
                    "application/csv");
    

    然后将azureUriForUploadedCSV 保存到您的数据库中...

    【讨论】:

    • 我不希望 BlobContainerPublicAccessType 访问 blob,在这种情况下公共为我们做了什么
    • 只评论这一行:await container.SetPermissionsAsync(new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob });
    【解决方案3】:
        private static void AzureStorageAccountBlob()
        {
            string filename = "mytestfile.txt";
            string fileContents = "some content";
    
            StorageCredentials creds = new StorageCredentials("mystorageaccount2020", "XXXXX");
            CloudStorageAccount acct = new CloudStorageAccount(creds, true);
            CloudBlobClient client = acct.CreateCloudBlobClient();
            CloudBlobContainer container = client.GetContainerReference("myfirstcontainer");
    
            container.CreateIfNotExists();
            ICloudBlob blob = container.GetBlockBlobReference(filename);
            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContents)))
            {
                blob.UploadFromStream(stream);
            }
        }
    

    在我的示例中,我假设内容已经从文件中获得。还有一件重要的事情,您必须创建 StorageAccount。

    【讨论】:

    • blob.UploadFromStream(stream);这仅适用于异步。我看不到 UploadFromStream!
    • @sishanov 您必须从 nuget 安装 WindowsAzure.Storage 库。 void UploadFromStream(Stream source, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null);
    猜你喜欢
    • 2017-08-18
    • 2022-01-07
    • 2021-02-22
    • 2017-01-19
    • 2020-09-19
    • 2021-11-28
    • 2018-10-16
    • 2016-05-29
    • 2017-08-12
    相关资源
    最近更新 更多