gpt4 book ai didi

azure - webjobs 使用 IBinder 删除 blob

转载 作者:行者123 更新时间:2023-12-03 05:09:14 25 4
gpt4 key购买 nike

好吧,我意识到 webjobs sdk 仍处于测试阶段,但它非常酷。尽管在获取我要绑定(bind)的实际对象方面,我在使用 IBinder 方面遇到了一些困难。这可能是显而易见的,所以请原谅我……

我正在处理要通过网络作业发送的电子邮件。它们被放入队列并触发事件。这段代码有效..但我不禁想到我可以访问生成的 blob,如果成功则删除它,或者如果失败则移动它,更容易。

这是代码:

        public static void ProcessEmailBlob([QueueTrigger(Email.queueEmailsToSend) ] string blobname, IBinder binder)

{

TextReader inputfile = binder.Bind<TextReader>(new BlobAttribute(Email.blobEmailOutboxContainerAsPath+blobname));
string input = inputfile.ReadToEnd();

string connection = ConfigurationManager.ConnectionStrings["AzureJobsStorage"].ConnectionString;

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connection);

//Get create connect to the outbox blob
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(Email.blobEmailOutboxContainer);
CloudBlockBlob emailin = container.GetBlockBlobReference(blobname);

MailMessage smptmail = new MailMessage();
//ought to be able to JSONise this??
//smptmail = JsonConvert.DeserializeObject<MailMessage>(input);
smptmail = XmlMailMessage.MakeSMPTMessage(input);
bool success = Email.Send(smptmail);

if (success && emailin.Exists()) //If sending the email succeeded
{
emailin.Delete();
}
else
{ //The email failed or the blob doesn't exist which is also odd and bad
if (emailin.Exists())
{ //Then the file is ok.. store it in the Failed Email
CloudBlobContainer failedcontainer = blobClient.GetContainerReference(Email.blobEmailFailedContainer);
failedcontainer.CreateIfNotExists();
CloudBlockBlob failedmailblob = failedcontainer.GetBlockBlobReference(blobname); // use the same name just a different container
failedmailblob.StartCopyFromBlob(emailin);
}
else
{
//log something here
}

}

}

正如你所看到的,我可以使用binder.Bind片段获取blob内容,但是然后我需要执行整个连接操作才能删除它..这是不对的..可以吗?

最佳答案

BlobAttribute 也可用于 .NET SDK 类型。在您的情况下,您可以绑定(bind)到 CloudBlockBlob,然后就不需要连接字符串。

CloudBlockBlob blobReference = binder.Bind<CloudBlockBlob>(new BlobAttribute(Email.blobEmailOutboxContainerAsPath+blobname));
blobReference.DeleteIfExists();

顺便说一句,您还可以绑定(bind)到 CloudStorageAccount。如果您的网络作业有 CloudStorageAccount 参数(无需属性),它将被神奇地绑定(bind)。

关于azure - webjobs 使用 IBinder 删除 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24787535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com