簡體   English   中英

我的 function 讀取 blob 后如何刪除它

[英]How do I delete a blob after it has been read by my function

我創建了一個 Blob 觸發 Azure function 應用程序,該應用程序從 TSV 文件中獲取數據並將其拆分並將數據寫入 SQL 數據庫。

讀取文件后,我想將其從 blob 容器中刪除。 我目前正在學習,這是我的第一個 C# 代碼,所以我希望你能幫助我並具體說明。 我查看了文檔

CloudBlockBlob blob = CloudBlobContainer.GetBlockBlobReference(???????);
blob.DeleteIfExists();

但我似乎不知道該放什么

這是我完整的 function。 如果你能幫我找出在哪里插入刪除命令,我將不勝感激:)

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 的原因是因為 function 多次讀取文件。 當它發生時,我會超時,因為處理數據需要超過 5 分鍾。 可能這就是它不止一次啟動的原因。

有沒有辦法寫這個,所以過程更快? TSV 文件有時可以容納多達 100.000 行。

期待對此羅賓的一些建議

如果您查看用於GetBlockBlobReference方法的MSFT API ,它blobName作為參數。

因此,理想情況下,您已經從 function 的參數中獲得了名稱。 你可以做

CloudBlockBlob blob = CloudBlobContainer.GetBlockBlobReference(name);
blob.DeleteIfExists();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM