public void uploadfromFilesystem(CloudBlockBlob blob, string localFilePath, string eventType) { if (eventType.Equals("create") || eventType.Equals("signUpStart")) { Program.ClientForm.addtoConsole("Upload started[create || signUpStart]:" + localFilePath); blob.UploadFromFile(localFilePath, FileMode.Open); Program.ClientForm.addtoConsole("Uploaded"); Program.ClientForm.ballon("Uploaded:"+ localFilePath); } else { try { Program.ClientForm.addtoConsole("Upload started[change,etc]:" + localFilePath); string leaseId = Guid.NewGuid().ToString(); blob.AcquireLease(TimeSpan.FromMilliseconds(16000), leaseId); blob.UploadFromFile(localFilePath, FileMode.Open, AccessCondition.GenerateLeaseCondition(leaseId)); blob.ReleaseLease(AccessCondition.GenerateLeaseCondition(leaseId)); Program.ClientForm.addtoConsole("Uploaded"); Program.ClientForm.ballon("Uploaded:" + localFilePath); } catch (Exception ex) { Program.ClientForm.addtoConsole("Upload: second attempt"); Thread.Sleep(5000); string leaseId = Guid.NewGuid().ToString(); blob.AcquireLease(TimeSpan.FromMilliseconds(16000), leaseId); blob.UploadFromFile(localFilePath, FileMode.Open, AccessCondition.GenerateLeaseCondition(leaseId)); blob.ReleaseLease(AccessCondition.GenerateLeaseCondition(leaseId)); Program.ClientForm.addtoConsole("Uploaded"); Program.ClientForm.ballon("Uploaded:" + localFilePath); } } }
public void CloudBlockBlobCopyWithSourceAccessCondition() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudBlockBlob source = container.GetBlockBlobReference("source"); string data = new string('a', 512); UploadText(source, data, Encoding.UTF8); string validLeaseId = Guid.NewGuid().ToString(); string leaseId = source.AcquireLease(TimeSpan.FromSeconds(60), validLeaseId); string invalidLeaseId = Guid.NewGuid().ToString(); source.FetchAttributes(); AccessCondition sourceAccessCondition1 = AccessCondition.GenerateIfNotModifiedSinceCondition(source.Properties.LastModified.Value); CloudBlockBlob copy1 = container.GetBlockBlobReference("copy1"); copy1.StartCopyFromBlob(TestHelper.Defiddler(source), sourceAccessCondition1); WaitForCopy(copy1); Assert.AreEqual(CopyStatus.Success, copy1.CopyState.Status); AccessCondition sourceAccessCondition2 = AccessCondition.GenerateLeaseCondition(invalidLeaseId); CloudBlockBlob copy2 = container.GetBlockBlobReference("copy2"); TestHelper.ExpectedException <ArgumentException>(() => copy2.StartCopyFromBlob(TestHelper.Defiddler(source), sourceAccessCondition2), "A lease condition cannot be specified on the source of a copy."); } finally { container.DeleteIfExists(); } }
public RenewingBlobLease(CloudBlockBlob blob) { _log = this.GetLogger(); _blob = blob; Id = _blob.AcquireLease(TimeSpan.FromSeconds(40), null); _log.DebugEvent("AcquiredLease", new Facet("blobUri", _blob.Uri), new Facet("leaseId", Id)); _timer = new Timer(TimeSpan.FromSeconds(30).TotalMilliseconds); _timer.Elapsed += RenewLease; _timer.Start(); }
public static void Leased(CloudBlockBlob blob, Action<CloudBlockBlob> a) { var lease = blob.AcquireLease(TimeSpan.FromSeconds(60), null); try { a(blob); } finally { blob.ReleaseLease(AccessCondition.GenerateLeaseCondition(lease)); } }
public bool TryAcquireLease(CloudBlockBlob blob, double leaseTimeInSeconds) { if(leaseTimeInSeconds < 15 || leaseTimeInSeconds > 60) throw new ArgumentException(@"value must be greater than 15 and smaller than 60", "leaseTimeInSeconds"); try { string proposedLeaseId = Guid.NewGuid().ToString(); var leaseTime = TimeSpan.FromSeconds(leaseTimeInSeconds); string leaseId = blob.AcquireLease(leaseTime, proposedLeaseId); UpdateAcquiredLease(blob, leaseId, leaseTimeInSeconds); return true; } catch (StorageException) { Trace.WriteLine(string.Format(FAILED_TO_ACQUIRE_LEASE, blob.Name)); return false; } }