Переместите файлы из общих папок Azure в хранилище BLOB-объектов с помощью c # - PullRequest
1 голос
/ 04 ноября 2019

Я загрузил файл с FTP-сервера, используя функцию Azure, и сохранил его в целевой папке, полученной из этого кода:

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

Который будет где-то в «Файловых ресурсах», который мы можем увидетьв «Проводнике хранилища Microsoft Azure».

Теперь мой вопрос о том, как скопировать этот файл из общего файлового ресурса в контейнер Blob или напрямую сохранить его в хранилище BLOB-объектов, к которому у Azure SQL есть доступ?

Ответы [ 3 ]

0 голосов
/ 04 ноября 2019

Используйте следующие расширения для загрузки в 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 в своей базе данных ...

0 голосов
/ 04 ноября 2019

Мы можем использовать 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);
        }
    }
}

Надеюсь, это будет полезно для вашей проблемы ~

0 голосов
/ 04 ноября 2019
    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.

...