使用IBinder删除blob的webjobs

时间:2014-07-16 17:54:29

标签: azure azure-storage-blobs azure-webjobssdk

好的,我意识到webjobs sdk仍处于测试阶段,但它非常酷。 我正在努力与IBinder进行一些努力,但在获取我绑定的实际对象方面。这可能是显而易见的,请原谅我,如果是这样的话......

我正在处理要通过webjob发送的电子邮件。他们被放入队列并触发事件。 这段代码有效..但我不禁想到我可以访问生成的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内容,但后来我需要做整个连接的事情才能删除它..这可能不对..它可以吗?

2 个答案:

答案 0 :(得分:2)

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

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

作为旁注,您还可以绑定到CloudStorageAccount。如果您的Web作业具有CloudStorageAccount参数(不需要属性),则它将被神奇地绑定。

答案 1 :(得分:0)

有了这个答案,我也成功了。 以下是补充说明

在我的情况下,我必须通过使用和删除

来更改BlobAttribute
//use block blob
//Must be "FileAccess.Read"
var fileAttr = new BlobAttribute(anyblobname, FileAccess.Read);
using(var stream = binder.Bind<Stream>(fileAttr)){
  ...
}

//delete block blob
//Must be "FileAccess.ReadWrite"
var blobAttr = new BlobAttribute(anyblobname, FileAccess.ReadWrite);
var blockBlob = binder.Bind<CloudBlockBlob>(blobAttr);
blockBlob.DeleteIfExists();