使用C#将文件从Azure文件共享移动到Blob存储中

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

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

我们将在“ microsoft Azure存储资源管理器”中看到“文件共享”中的哪个位置。

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

rree2009 回答:使用C#将文件从Azure文件共享移动到Blob存储中

    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。

,

使用以下扩展名上传到天蓝色:

    /// <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保存到您的数据库中...

,

我们可以使用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);
        }
    }
}

希望对您的问题有帮助〜

本文链接:https://www.f2er.com/3166526.html

大家都在问