Приведенный ниже код может загружать файлы из указанного каталога localPath
в контейнер хранилища BLOB-объектов Azure.Проблема заключается в том, что файлы не помещаются в корень контейнера, а создаются папки, которые соответствуют пути к файлу, определенному переменной localPath
.
string localPath = @"C:\example\path\to\files";
В результатеКонтейнер BLOB-объектов выглядит следующим образом.
.
+-- example
| +-- path
| +-- to
| +-- files
| +-- file1.json
| +-- file2.json
| +-- file3.json
| +-- file4.json
| +-- file5.json
Какие изменения необходимы в приведенном ниже коде, чтобы файлы перемещались в корень контейнера, а не в структуру этой папки, которая соответствует localPath
?
Program.cs
namespace AzureBlobStorageFileTransfer
{
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using System;
using System.IO;
using System.Threading.Tasks;
public static class Program
{
public static void Main()
{
ProcessAsync().GetAwaiter().GetResult();
}
private static async Task ProcessAsync()
{
CloudStorageAccount storageAccount = null;
CloudBlobContainer cloudBlobContainer = null;
string storageConnectionString = Environment.GetEnvironmentVariable("storageconnectionstring");
if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
{
try
{
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
cloudBlobContainer = cloudBlobClient.GetContainerReference("example");
BlobContainerPermissions permissions = new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
};
await cloudBlobContainer.SetPermissionsAsync(permissions);
string localPath = @"C:\example\path\to\files";
var txtFiles = Directory.EnumerateFiles(localPath, "*.json");
foreach (string currentFile in txtFiles)
{
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(currentFile);
await cloudBlockBlob.UploadFromFileAsync(currentFile);
}
}
catch (StorageException ex)
{
Console.WriteLine("Error returned from the service: {0}", ex.Message);
}
}
else
{
Console.WriteLine(
"A connection string has not been defined in the system environment variables. " +
"Add a environment variable named 'storageconnectionstring' with your storage " +
"connection string as a value.");
}
}
}
}