Last active
July 1, 2024 03:37
-
-
Save samuraitruong/315c23a83937bb0ef29e40cc359c196f to your computer and use it in GitHub Desktop.
dummy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Azure.Storage.Blobs; | |
using Microsoft.EntityFrameworkCore; | |
namespace BlobStorageExample | |
{ | |
// Sample EF Entity | |
public class YourEntity | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
// Other properties | |
} | |
// Sample EF DbContext | |
public class YourDbContext : DbContext | |
{ | |
public DbSet<YourEntity> YourEntities { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
optionsBuilder.UseSqlServer("<Your_SQL_Server_Connection_String>"); | |
} | |
} | |
class Program | |
{ | |
private const string blobConnectionString = "<Your_Azure_Blob_Storage_Connection_String>"; | |
private const string blobContainerName = "<Your_Container_Name>"; | |
static async Task Main(string[] args) | |
{ | |
string blobName = "snapshot.csv"; | |
await ExportTableToCsvAsync(blobName); | |
} | |
public static async Task ExportTableToCsvAsync(string blobName) | |
{ | |
BlobServiceClient blobServiceClient = new BlobServiceClient(blobConnectionString); | |
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(blobContainerName); | |
BlobClient blobClient = containerClient.GetBlobClient(blobName); | |
// Open the blob stream | |
await using (Stream blobStream = await blobClient.OpenWriteAsync(overwrite: true)) | |
await using (StreamWriter writer = new StreamWriter(blobStream, Encoding.UTF8)) | |
await using (YourDbContext context = new YourDbContext()) | |
{ | |
int batchSize = 10000; | |
int skip = 0; | |
bool hasMore = true; | |
// Writing headers | |
writer.WriteLine("Id,Name"); // Replace with your actual property names | |
while (hasMore) | |
{ | |
var batch = await context.YourEntities | |
.AsNoTracking() | |
.OrderBy(e => e.Id) // Ensure consistent ordering | |
.Skip(skip) | |
.Take(batchSize) | |
.ToListAsync(); | |
if (batch.Count == 0) | |
{ | |
hasMore = false; | |
continue; | |
} | |
// Writing data | |
foreach (var entity in batch) | |
{ | |
writer.WriteLine($"{entity.Id},{entity.Name}"); // Replace with your actual property names | |
} | |
await writer.FlushAsync(); // Ensure data is written to the stream | |
skip += batchSize; | |
} | |
} | |
} | |
} | |
} |
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
namespace BlobStorageExample
{
public class BlobService
{
private readonly BlobServiceClient _blobServiceClient;
public BlobService(string blobConnectionString)
{
_blobServiceClient = new BlobServiceClient(blobConnectionString);
}
public async Task<StreamWriter> CreateBlobStreamWriterAsync(string containerName, string blobName)
{
BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
await containerClient.CreateIfNotExistsAsync();
BlobClient blobClient = containerClient.GetBlobClient(blobName);
Stream blobStream = await blobClient.OpenWriteAsync(overwrite: true);
return new StreamWriter(blobStream, Encoding.UTF8);
}
}
}
private static string GetCsvHeaders()
{
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
return string.Join(",", properties.Select(p => p.Name));
}
private static string ConvertToCsvLine<T>(T obj)
{
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var values = properties.Select(p => p.GetValue(obj)?.ToString()?.Replace(",", ";") ?? string.Empty);
return string.Join(",", values);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have another blobservice to upload file, cna you update my blob service to have function create stream writer