} // SaveBlob /// <summary> /// Verifies the eTag when saving /// </summary> /// <param name="blobContainer"></param> /// <param name="fileName"></param> /// <param name="text"></param> /// <param name="eTag"></param> public void SaveBlob(string blobContainer, string fileName, string text, string eTag) { InitializeStorage(blobContainer); string containerName = blobContainer.ToString().ToLower(); // must be lower case! Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobStorage = blobStorageDictionary[containerName]; Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobStorage.GetContainerReference(containerName); Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blob = container.GetBlockBlobReference(fileName); // save if (fileName.Contains(".")) { blob.Properties.ContentType = GetMimeTypeFromExtension(fileName.Substring(fileName.LastIndexOf("."))); } else { blob.Properties.ContentType = "application/octet-stream"; } Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = new Microsoft.WindowsAzure.Storage.AccessCondition(); accessCondition.IfMatchETag = eTag; PutText(blob, text, accessCondition); } // SaveBlob
public void FetchAttributes(Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = null, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options = null, Microsoft.WindowsAzure.Storage.OperationContext operationContext = null) { // TODO: use protocol ServerState ss = slaEngine.FindServerToRead(Name); ICloudBlob blob = ClientRegistry.GetCloudBlob(ss.Name, configuration.Name, Name); watch.Start(); blob.FetchAttributes(accessCondition, options, operationContext); ss.AddRtt(watch.ElapsedMilliseconds); // slaEngine.SessionState.RecordObjectRead(blob.Name, Timestamp(blob), ss); slaEngine.Session.RecordObjectRead(blob.Name, Timestamp(blob), ss, ""); }
//public int StreamWriteSizeInBytes //{ // get // { // return strongBlob.StreamWriteSizeInBytes; // } // set // { // strongBlob.StreamWriteSizeInBytes = value; // eventualBlob.StreamWriteSizeInBytes = value; // } //} //public int StreamMinimumReadSizeInBytes //{ // get // { // return strongBlob.StreamMinimumReadSizeInBytes; // } // set // { // strongBlob.StreamMinimumReadSizeInBytes = value; // eventualBlob.StreamMinimumReadSizeInBytes = value; // } //} //public Microsoft.WindowsAzure.Storage.Blob.BlobProperties Properties //{ // get { return strongBlob.Properties; } //} //public IDictionary<string, string> Metadata //{ // get { return strongBlob.Metadata; } //} //public DateTimeOffset? SnapshotTime //{ // get { return strongBlob.SnapshotTime; } //} //public Microsoft.WindowsAzure.Storage.Blob.CopyState CopyState //{ // get { return strongBlob.CopyState; } //} //public Microsoft.WindowsAzure.Storage.Blob.BlobType BlobType //{ // get { return strongBlob.BlobType; } //} /// <summary> /// Upload to primary blobs from the provided stream. /// /// Our put is using an optimization where it does not take lease on blobs if there is only one primary container. /// </summary> /// <param name="source"></param> /// <param name="accessCondition"></param> /// <param name="options"></param> /// <param name="operationContext"></param> public void UploadFromStream(System.IO.Stream source, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = null, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options = null, Microsoft.WindowsAzure.Storage.OperationContext operationContext = null) { source.Position = 0; protocol.Write(blob => blob.UploadFromStream(source, /*lease.getAccessConditionWithLeaseId(accessCondition)*/ accessCondition, options, operationContext), accessCondition); /* * bool isDone = false; * * do * { * try * { * * if (configuration.IsInFastMode()) * { * DoUploadFromStream(source, accessCondition, options, operationContext); * isDone = true; * } * else * { * //We are not sure if reconfiguration is happening or not. We execute put in slow mode. * using (CloudBlobLease lease = new CloudBlobLease(configuration.Name, LeaseTakingPolicy.TryOnce)) * { * if (lease.HasLease) * { * configuration.SyncWithCloud(ClientRegistry.GetConfigurationAccount()); * DoUploadFromStream(source, accessCondition, options, operationContext); * isDone = true; * } * else * { * continue; * } * } * } * } * catch (StorageException ex) * { * throw ex; * } * catch (Exception ex) * { * Console.WriteLine(ex.StackTrace.ToString()); * throw ex; * } * } * while (!isDone); */ }
private void DoUploadFromStream(System.IO.Stream source, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = null, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options = null, Microsoft.WindowsAzure.Storage.OperationContext operationContext = null) { try { bool done = false; while (!done) { using (PrimaryCloudBlobLease lease = new PrimaryCloudBlobLease(Name, configuration, true)) { if (lease.HasLease) { foreach (string server in configuration.PrimaryServers) { watch.Start(); ICloudBlob blob = ClientRegistry.GetCloudBlob(server, configuration.Name, Name, false); source.Position = 0; blob.UploadFromStream(source, lease.getAccessConditionWithLeaseId(accessCondition), options, operationContext); watch.Stop(); ServerState ss = slaEngine.Monitor.GetServerState(server); ss.AddRtt(watch.ElapsedMilliseconds); slaEngine.Session.RecordObjectWritten(Name, Timestamp(blob), ss); } done = true; } } } } catch (StorageException se) { throw se; } catch (Exception ex) { throw ex; } }
/// <summary> /// Writes text to a blob reference with an access condition /// </summary> /// <param name="blob"></param> /// <param name="text"></param> /// <param name="accessCondition"></param> public void PutText(Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blob, string text, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition) { using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(text))) { try { blob.UploadFromStream(memoryStream, accessCondition); } catch (Microsoft.WindowsAzure.Storage.StorageException storageException) { if (storageException.Message.Contains("conditional header(s) is not met")) { throw new Sample.Azure.Common.CustomException.CustomConcurrencyException(System.Net.HttpStatusCode.PreconditionFailed, "The file has been updated by another user."); } else { throw; } } } }
private void DoDelete(Microsoft.WindowsAzure.Storage.Blob.DeleteSnapshotsOption deleteSnapshotsOption = DeleteSnapshotsOption.None, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = null, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options = null, Microsoft.WindowsAzure.Storage.OperationContext operationContext = null) { bool done = false; while (!done) { using (PrimaryCloudBlobLease lease = new PrimaryCloudBlobLease(this.Name, configuration, true)) { if (lease.HasLease) { Dictionary <ICloudBlob, IAsyncResult> results = new Dictionary <ICloudBlob, IAsyncResult>(); foreach (string serverName in configuration.PrimaryServers) { watch.Start(); ICloudBlob blob = ClientRegistry.GetCloudBlob(serverName, configuration.Name, Name); results[blob] = blob.BeginDelete(deleteSnapshotsOption, lease.getAccessConditionWithLeaseId(accessCondition), options, operationContext, null, null); ServerState ss = slaEngine.Monitor.GetServerState(serverName); ss.AddRtt(watch.ElapsedMilliseconds); slaEngine.Session.RecordObjectWritten(Name, Timestamp(blob), ss); } foreach (ICloudBlob blob in results.Keys) { blob.EndDelete(results[blob]); } done = true; } } } }
//public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginUploadFromStream(System.IO.Stream source, AsyncCallback callback, object state) //{ // return strongBlob.BeginUploadFromStream(source, callback, state); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginUploadFromStream(System.IO.Stream source, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options, Microsoft.WindowsAzure.Storage.OperationContext operationContext, AsyncCallback callback, object state) //{ // return strongBlob.BeginUploadFromStream(source, accessCondition, options, operationContext, callback, state); //} //public void EndUploadFromStream(IAsyncResult asyncResult) //{ // strongBlob.EndUploadFromStream(asyncResult); // slaEngine.SessionState.RecordObjectWritten(strongBlob.Name, Timestamp(strongBlob), primaryServer); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginDownloadToStream(System.IO.Stream target, AsyncCallback callback, object state) //{ // // TODO: Use SLA to decide from which server to download. // return strongBlob.BeginDownloadToStream(target, callback, state); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginDownloadToStream(System.IO.Stream target, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options, Microsoft.WindowsAzure.Storage.OperationContext operationContext, AsyncCallback callback, object state) //{ // return strongBlob.BeginDownloadToStream(target, accessCondition, options, operationContext, callback, state); //} //public void EndDownloadToStream(IAsyncResult asyncResult) //{ // strongBlob.EndDownloadToStream(asyncResult); // slaEngine.SessionState.RecordObjectRead(strongBlob.Name, Timestamp(strongBlob), primaryServer); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginDownloadRangeToStream(System.IO.Stream target, long? offset, long? length, AsyncCallback callback, object state) //{ // return strongBlob.BeginDownloadRangeToStream(target, offset, length, callback, state); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginDownloadRangeToStream(System.IO.Stream target, long? offset, long? length, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options, Microsoft.WindowsAzure.Storage.OperationContext operationContext, AsyncCallback callback, object state) //{ // return strongBlob.BeginDownloadRangeToStream(target, offset, length, accessCondition, options, operationContext, callback, state); //} //public void EndDownloadRangeToStream(IAsyncResult asyncResult) //{ // strongBlob.EndDownloadRangeToStream(asyncResult); // slaEngine.SessionState.RecordObjectRead(strongBlob.Name, Timestamp(strongBlob), primaryServer); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginExists(AsyncCallback callback, object state) //{ // return strongBlob.BeginExists(callback, state); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginExists(Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options, Microsoft.WindowsAzure.Storage.OperationContext operationContext, AsyncCallback callback, object state) //{ // return strongBlob.BeginExists(options, operationContext, callback, state); //} //public bool EndExists(IAsyncResult asyncResult) //{ // bool result = strongBlob.EndExists(asyncResult); // slaEngine.SessionState.RecordObjectRead(strongBlob.Name, Timestamp(strongBlob), primaryServer); // return result; //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginFetchAttributes(AsyncCallback callback, object state) //{ // return strongBlob.BeginFetchAttributes(callback, state); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginFetchAttributes(Microsoft.WindowsAzure.Storage.AccessCondition accessCondition, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options, Microsoft.WindowsAzure.Storage.OperationContext operationContext, AsyncCallback callback, object state) //{ // return strongBlob.BeginFetchAttributes(accessCondition, options, operationContext, callback, state); //} //public void EndFetchAttributes(IAsyncResult asyncResult) //{ // strongBlob.EndFetchAttributes(asyncResult); // slaEngine.SessionState.RecordObjectRead(strongBlob.Name, Timestamp(strongBlob), primaryServer); //} //public void SetMetadata(Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = null, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options = null, Microsoft.WindowsAzure.Storage.OperationContext operationContext = null) //{ // watch.Start(); // strongBlob.SetMetadata(accessCondition, options, operationContext); // primaryServer.AddRtt(watch.ElapsedMilliseconds); // slaEngine.SessionState.RecordObjectWritten(strongBlob.Name, Timestamp(strongBlob), primaryServer); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginSetMetadata(AsyncCallback callback, object state) //{ // return strongBlob.BeginSetMetadata(callback, state); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginSetMetadata(Microsoft.WindowsAzure.Storage.AccessCondition accessCondition, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options, Microsoft.WindowsAzure.Storage.OperationContext operationContext, AsyncCallback callback, object state) //{ // return strongBlob.BeginSetMetadata(accessCondition, options, operationContext, callback, state); //} //public void EndSetMetadata(IAsyncResult asyncResult) //{ // strongBlob.EndSetMetadata(asyncResult); // slaEngine.SessionState.RecordObjectWritten(strongBlob.Name, Timestamp(strongBlob), primaryServer); //} //public void SetProperties(Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = null, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options = null, Microsoft.WindowsAzure.Storage.OperationContext operationContext = null) //{ // watch.Start(); // strongBlob.SetProperties(accessCondition, options, operationContext); // primaryServer.AddRtt(watch.ElapsedMilliseconds); // slaEngine.SessionState.RecordObjectWritten(strongBlob.Name, Timestamp(strongBlob), primaryServer); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginSetProperties(AsyncCallback callback, object state) //{ // return strongBlob.BeginSetProperties(callback, state); //} //public Microsoft.WindowsAzure.Storage.ICancellableAsyncResult BeginSetProperties(Microsoft.WindowsAzure.Storage.AccessCondition accessCondition, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options, Microsoft.WindowsAzure.Storage.OperationContext operationContext, AsyncCallback callback, object state) //{ // return strongBlob.BeginSetProperties(accessCondition, options, operationContext, callback, state); //} //public void EndSetProperties(IAsyncResult asyncResult) //{ // strongBlob.EndSetProperties(asyncResult); // slaEngine.SessionState.RecordObjectWritten(strongBlob.Name, Timestamp(strongBlob), primaryServer); //} public void Delete(Microsoft.WindowsAzure.Storage.Blob.DeleteSnapshotsOption deleteSnapshotsOption = DeleteSnapshotsOption.None, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = null, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options = null, Microsoft.WindowsAzure.Storage.OperationContext operationContext = null) { bool isDone = false; do { try { if (configuration.IsInFastMode()) { DoDelete(deleteSnapshotsOption, accessCondition, options, operationContext); isDone = true; } else { //We are not sure if reconfiguration is happening or not. We execute put in slow mode. using (CloudBlobLease lease = new CloudBlobLease(configuration.Name, LeaseTakingPolicy.TryOnce)) { if (lease.HasLease) { configuration.SyncWithCloud(ClientRegistry.GetConfigurationAccount()); DoDelete(deleteSnapshotsOption, accessCondition, options, operationContext); isDone = true; } } } } catch (StorageException ex) { throw ex; } catch (Exception ex) { throw ex; } } while (!isDone); }