1
votes

I downloaded a file from an FTP server using an azure function and save it in the target that I get from this code:

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

Which will be somewhere in "File Shares" that we can see in "Microsoft Azure storage Explorer".

Now my question is about how to copy this file from File Share to Blob container or Directly save it to Blob Storage that azure SQL has access to?

3

3 Answers

0
votes
    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);
        }
    }

In my example I have assumed that content already achieved from file. And also one important thing you must create StorageAccount.

0
votes

Use the below extention to upload to 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;
    }

Do something like this:

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");

Then save azureUriForUploadedCSV into your database...

0
votes

We can use CloudBlockBlob.StartCopy(CloudFile). You may refer to the code below:

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);
        }
    }
}

Hope it would be helpful to your problem~