c# - 如何在我的函数读取 blob 后删除它
问题描述
我创建了一个 blob 触发的 Azure 函数应用程序,它从 TSV 文件中获取数据并将其拆分并将数据写入 SQL 数据库。
读取文件后,我想将其从 blob 容器中删除。我目前正在学习,这是我的第一个 C# 代码,所以我希望你能帮助我并具体说明。我查看了文档
CloudBlockBlob blob = CloudBlobContainer.GetBlockBlobReference(???????);
blob.DeleteIfExists();
但我似乎不知道该放什么
这是我的完整功能。如果你能帮我找出在哪里插入删除命令,我将不胜感激:)
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System.Threading.Tasks;
using System.Diagnostics;
using System;
using System.Data.SqlClient;
using System.Linq;
using Azure.Storage.Blobs;
using Microsoft.WindowsAzure.Storage.Blob;
namespace FileProcessor
{
public static class FileProcessorFn
{
[FunctionName("FileProcessorFn")]
public static async Task Run([BlobTrigger("import/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, TraceWriter log)
{
log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
if (myBlob.Length > 0)
{
using (var reader = new StreamReader(myBlob))
{
var lineNumber = 1;
var line = await reader.ReadLineAsync();
var raceID = 0;
while (line != null)
{
if (lineNumber == 1 )
{
var fileName = name.Substring(0, name.Length - 4);
var races = fileName.Split('-');
var item2 = new Race
{
Race_Name = races[0],
Race_Track = races[1],
Race_Sequence = races[2],
Race_Date = races[3]
};
using (var context = new GokartDbContext())
{
context.Races.Add(item2);
log.Info("new race added with the name: " + item2.Race_Name + " and the date: " + item2.Race_Date + " with Success!");
await context.SaveChangesAsync();
//context.GetValidationErrors();
//context.SaveChanges();
raceID = context.Races.Select(p => p.RaceId).Max();
}
}
if (raceID > 0 )
{
await ProcessLine(name, line, lineNumber, log, raceID);
line = await reader.ReadLineAsync();
}
lineNumber++;
}
}
}
CloudBlockBlob blob = CloudBlobContainer.GetBlockBlobReference(image/{ name});
blob.DeleteIfExists();
}
private static async Task ProcessLine(string name, string line, int lineNumber, TraceWriter log, int raceID)
{
if (string.IsNullOrWhiteSpace(line))
{
log.Warning($"{name}: {lineNumber} is empty.");
return;
}
if (lineNumber == 1)
{
log.Warning($"File header detected! Skipping....");
return;
}
//var fileName = name.Substring(0, name.Length -4);
//var races = fileName.Split('-');
var x_GPS_Longitudinal_Acceleration = "";
var x_Gyroscope_Y_Axis = "";
var x_Accelerometer_X_Axis = "";
var x_GPS_Speed = "";
var x_Temperatur_1 = "";
var x_Retning = "";
var x_Vertikalt_DOP = "";
var x_GPS_Lateral_Acceleration = "";
var x_Temperatur_fra_Barometer = "";
var x_RPM = "";
var x_Humidity = "";
var x_Gyroscope_Z_Axis = "";
var x_Intern_Temperatur = "";
var x_Lufttryk = "";
var x_Laengdegrad = "";
var x_Acceleeerometer_Z_Akse = "";
var x_Rat_Vinkel = "";
var x_GPS_Afstand = "";
var x_Batteri_Voltage = "";
var x_Vertical_Acceleration = "";
var x_Positions_DOP = "";
var x_Height = "";
var x_Breddegrad = "";
var x_Horisontal_DOP = "";
var x_Gyroscope_X_Axis = "";
var x_Accelerometer_Y_Akse = "";
var parts = line.Split('\t');
if (parts.Length > 6)
{
x_GPS_Longitudinal_Acceleration = parts[5];
x_Gyroscope_Y_Axis = parts[6];
x_Accelerometer_X_Axis = parts[7];
x_GPS_Speed = parts[8];
x_Temperatur_1 = parts[9];
x_Retning = parts[10];
x_Vertikalt_DOP = parts[11];
x_GPS_Lateral_Acceleration = parts[12];
x_Temperatur_fra_Barometer = parts[13];
x_RPM = parts[14];
x_Humidity = parts[15];
x_Gyroscope_Z_Axis = parts[16];
x_Intern_Temperatur = parts[17];
x_Lufttryk = parts[18];
x_Laengdegrad = parts[19];
x_Acceleeerometer_Z_Akse = parts[20];
x_Rat_Vinkel = parts[21];
x_GPS_Afstand = parts[22];
x_Batteri_Voltage = parts[23];
x_Vertical_Acceleration = parts[24];
x_Positions_DOP = parts[25];
x_Height = parts[26];
x_Breddegrad = parts[27];
x_Horisontal_DOP = parts[28];
x_Gyroscope_X_Axis = parts[29];
x_Accelerometer_Y_Akse = parts[30];
}
//var item2 = new Race
//{
// Race_Name = races[0],
// Race_Track = races[1],
// Race_Sequence = races[2],
// Race_Date = races[3]
//};
var item = new RaceData
{
RaceForeignKey = raceID,
Start_Date = parts[0],
Start_Time = parts[1],
Lap_Number = parts[2],
Session_Time = parts[3],
Lap_Time = parts[4],
GPS_Longitudinal_Acceleration = x_GPS_Longitudinal_Acceleration,
Gyroscope_Y_Axis = x_Gyroscope_Y_Axis,
Accelerometer_X_Axis = x_Accelerometer_X_Axis,
GPS_Speed = x_GPS_Speed,
Temperatur_1 = x_Temperatur_1,
Retning = x_Retning,
Vertikalt_DOP = x_Vertikalt_DOP,
GPS_Lateral_Acceleration = x_GPS_Lateral_Acceleration,
Temperatur_fra_Barometer = x_Temperatur_fra_Barometer,
RPM = x_RPM,
Humidity = x_Humidity,
Gyroscope_Z_Axis = x_Gyroscope_Z_Axis,
Intern_Temperatur = x_Intern_Temperatur,
Lufttryk = x_Lufttryk,
Laengdegrad = x_Laengdegrad,
Acceleeerometer_Z_Akse = x_Acceleeerometer_Z_Akse,
Rat_Vinkel = x_Rat_Vinkel,
GPS_Afstand = x_GPS_Afstand,
Batteri_Voltage = x_Batteri_Voltage,
Vertical_Acceleration = x_Vertical_Acceleration,
Positions_DOP = x_Positions_DOP,
Height = x_Height,
Breddegrad = x_Breddegrad,
Horisontal_DOP = x_Horisontal_DOP,
Gyroscope_X_Axis = x_Gyroscope_X_Axis,
Accelerometer_Y_Akse = x_Accelerometer_Y_Akse
};
using (var context = new GokartDbContext())
{
//context.Races.Add(item2);
//log.Info("new race added with the name: " + item2.Race_Name + " and the date: " + item2.Race_Date + " with Success!");
context.RaceDatas.Add(item);
log.Info($"{name}: {lineNumber} inserted task: \"{item.Start_Date}\" with id: {item.Id}.");
await context.SaveChangesAsync();
}
}
}
}
我想删除 blob 的原因是该函数多次读取文件。当它发生时,我会超时,因为处理数据需要超过 5 分钟。可能这就是它不止一次启动的原因。
有没有办法写这个,所以过程更快?TSV 文件有时可以容纳多达 100.000 行。
期待对此罗宾的一些建议
解决方案
如果您查看MSFT API的GetBlockBlobReference
方法,它会将 ablobName
作为参数。
因此,理想情况下,您已经从函数的参数中获得了名称。你可以做
CloudBlockBlob blob = CloudBlobContainer.GetBlockBlobReference(name);
blob.DeleteIfExists();
推荐阅读
- java - Mempoi 生成具有相同数据的双工作表
- javascript - Excel VBA 单击网页中的按钮以下载文件并将其通过电子邮件发送到电子邮件列表
- amazon-web-services - AWS Route53 私有托管区域到内部负载均衡器
- java - Java中的BigInteger因子
- java - Apache CXF 和 Log4j2
- ios - 每 10 毫秒记录 pcmaudiodata 不播放
- reactjs - 我们如何在 useState 中使用 redux state 来设置初始值
- c++ - 访问由 void 指针引用的结构的成员
- php - 怎么显示名字
- objective-c - 为什么这不是数组?