azure - webjobs 使用 IBinder 删除 blob

标签 azure azure-blob-storage azure-webjobssdk

好吧,我意识到 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/

相关文章:

javascript - 如何在 Node.js 中使用 Azure Function App 访问 Azure AD

c# - 文件上传到 Web API 和 Azure 抛出 HttpException

python - SAS 访问 blob 容器 (azure/python)

Azure CDN 与从 blob 直接访问

azure - 禁止在 Azure Webjobs ("Executing: ' xxx' 中进行日志记录,因为在 'yyy' 上检测到新队列消息。”)

azure - Azure WebJobs SDK 是否支持将 TextWriter 日志推送到 App Insights 中?

Azure 连续 webjob (blob) 仅触发一次

amazon-web-services - 创建k8s资源并等待完成继续下一步

azure - 跨不同应用程序洞察实例保存日志查询

Azure B2C + Facebook OAuth(站点 URL 和有效的 OAuth 重定向 URI)