本文整理汇总了C#中ICloudBlob.UploadFromStream方法的典型用法代码示例。如果您正苦于以下问题:C# ICloudBlob.UploadFromStream方法的具体用法?C# ICloudBlob.UploadFromStream怎么用?C# ICloudBlob.UploadFromStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICloudBlob
的用法示例。
在下文中一共展示了ICloudBlob.UploadFromStream方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UploadText
static void UploadText(ICloudBlob blob, string text, AccessCondition accessCondition)
{
blob.Properties.ContentEncoding = "UTF-8";
blob.Properties.ContentType = "text/plain";
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
{
blob.UploadFromStream(stream, accessCondition);
}
}
示例2: UploadText
public static void UploadText(ICloudBlob blob, string text, Encoding encoding, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
{
byte[] textAsBytes = encoding.GetBytes(text);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(textAsBytes, 0, textAsBytes.Length);
if (blob.BlobType == BlobType.PageBlob)
{
int lastPageSize = (int)(stream.Length % 512);
if (lastPageSize != 0)
{
byte[] padding = new byte[512 - lastPageSize];
stream.Write(padding, 0, padding.Length);
}
}
stream.Seek(0, SeekOrigin.Begin);
blob.UploadFromStream(stream, accessCondition, options, operationContext);
}
}
示例3: _handleWebException
private bool _handleWebException(ICloudBlob blob, StorageException err)
{
if (err.RequestInformation.HttpStatusCode == 404)
{
_azureDirectory.CreateContainer();
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
{
writer.Write(_lockFile);
blob.UploadFromStream(stream);
}
return true;
}
return false;
}
示例4: DoDownloadRangeToByteArrayNegativeTests
private void DoDownloadRangeToByteArrayNegativeTests(ICloudBlob blob)
{
int blobLength = 1024;
int resultBufSize = 1024;
byte[] buffer = GetRandomBuffer(blobLength);
byte[] resultBuffer = new byte[resultBufSize];
using (MemoryStream stream = new MemoryStream(buffer))
{
blob.UploadFromStream(stream);
TestHelper.ExpectedException(() => blob.DownloadRangeToByteArray(resultBuffer, 0, 1024, 1), "Try invalid length", HttpStatusCode.RequestedRangeNotSatisfiable);
StorageException ex = TestHelper.ExpectedException<StorageException>(() => blob.DownloadToByteArray(resultBuffer, 1024), "Provide invalid offset");
Assert.IsInstanceOfType(ex.InnerException, typeof(NotSupportedException));
ex = TestHelper.ExpectedException<StorageException>(() => blob.DownloadRangeToByteArray(resultBuffer, 1023, 0, 2), "Should fail when offset + length required is greater than size of the buffer");
Assert.IsInstanceOfType(ex.InnerException, typeof(NotSupportedException));
ex = TestHelper.ExpectedException<StorageException>(() => blob.DownloadRangeToByteArray(resultBuffer, 0, 0, -10), "Fail when a negative length is specified");
Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentOutOfRangeException));
TestHelper.ExpectedException<ArgumentOutOfRangeException>(() => blob.DownloadRangeToByteArray(resultBuffer, -10, 0, 20), "Fail if a negative offset is provided");
ex = TestHelper.ExpectedException<StorageException>(() => blob.DownloadRangeToByteArray(resultBuffer, 0, -10, 20), "Fail if a negative blob offset is provided");
Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentOutOfRangeException));
}
}
示例5: DoDownloadRangeToByteArray
/// <summary>
/// Single put blob and get blob
/// </summary>
/// <param name="blobSize">The blob size.</param>
/// <param name="bufferSize">The output buffer size.</param>
/// <param name="bufferOffset">The output buffer offset.</param>
/// <param name="blobOffset">The blob offset.</param>
/// <param name="length">Length of the data range to download.</param>
/// <param name="option">0 - Sync, 1 - APM and 2 - APM overload.</param>
private void DoDownloadRangeToByteArray(ICloudBlob blob, int blobSize, int bufferSize, int bufferOffset, long? blobOffset, long? length, int option)
{
int downloadLength;
byte[] buffer = GetRandomBuffer(blobSize);
byte[] resultBuffer = new byte[bufferSize];
byte[] resultBuffer2 = new byte[bufferSize];
using (MemoryStream originalBlob = new MemoryStream(buffer))
{
blob.UploadFromStream(originalBlob);
}
if (option == 0)
{
downloadLength = blob.DownloadRangeToByteArray(resultBuffer, bufferOffset, blobOffset, length);
}
else if (option == 1)
{
using (AutoResetEvent waitHandle = new AutoResetEvent(false))
{
ICancellableAsyncResult result = blob.BeginDownloadRangeToByteArray(resultBuffer,
bufferOffset,
blobOffset,
length,
ar => waitHandle.Set(),
null);
waitHandle.WaitOne();
downloadLength = blob.EndDownloadRangeToByteArray(result);
}
}
else
{
using (AutoResetEvent waitHandle = new AutoResetEvent(false))
{
OperationContext context = new OperationContext();
ICancellableAsyncResult result = blob.BeginDownloadRangeToByteArray(resultBuffer,
bufferOffset,
blobOffset,
length,
null,
null,
context,
ar => waitHandle.Set(),
null);
waitHandle.WaitOne();
downloadLength = blob.EndDownloadRangeToByteArray(result);
}
}
int downloadSize = Math.Min(blobSize - (int)(blobOffset.HasValue ? blobOffset.Value : 0), bufferSize - bufferOffset);
if (length.HasValue && (length.Value < downloadSize))
{
downloadSize = (int)length.Value;
}
Assert.AreEqual(downloadSize, downloadLength);
for (int i = 0; i < bufferOffset; i++)
{
Assert.AreEqual(0, resultBuffer[i]);
}
for (int j = 0; j < downloadLength; j++)
{
Assert.AreEqual(buffer[(blobOffset.HasValue ? blobOffset.Value : 0) + j], resultBuffer[bufferOffset + j]);
}
for (int k = bufferOffset + downloadLength; k < bufferSize; k++)
{
Assert.AreEqual(0, resultBuffer[k]);
}
}
示例6: DoDownloadToByteArrayTest
/// <summary>
/// Single put blob and get blob
/// </summary>
/// <param name="blobSize">The blob size.</param>
/// <param name="bufferOffset">The blob offset.</param>
/// <param name="option"> 0 - Sunc, 1 - APM and 2 - APM overload.</param>
private void DoDownloadToByteArrayTest(ICloudBlob blob, int blobSize, int bufferSize, int bufferOffset, int option)
{
int downloadLength;
byte[] buffer = GetRandomBuffer(blobSize);
byte[] resultBuffer = new byte[bufferSize];
byte[] resultBuffer2 = new byte[bufferSize];
using (MemoryStream originalBlob = new MemoryStream(buffer))
{
blob.UploadFromStream(originalBlob);
}
if (option == 0)
{
downloadLength = blob.DownloadToByteArray(resultBuffer, bufferOffset);
}
else if (option == 1)
{
using (AutoResetEvent waitHandle = new AutoResetEvent(false))
{
ICancellableAsyncResult result = blob.BeginDownloadToByteArray(resultBuffer,
bufferOffset,
ar => waitHandle.Set(),
null);
waitHandle.WaitOne();
downloadLength = blob.EndDownloadToByteArray(result);
}
}
else
{
using (AutoResetEvent waitHandle = new AutoResetEvent(false))
{
OperationContext context = new OperationContext();
ICancellableAsyncResult result = blob.BeginDownloadToByteArray(resultBuffer,
bufferOffset, /* offset */
null, /* accessCondition */
null, /* options */
context, /* operationContext */
ar => waitHandle.Set(),
null);
waitHandle.WaitOne();
downloadLength = blob.EndDownloadToByteArray(result);
}
}
int downloadSize = Math.Min(blobSize, bufferSize - bufferOffset);
Assert.AreEqual(downloadSize, downloadLength);
for (int i = 0; i < blob.Properties.Length; i++)
{
Assert.AreEqual(buffer[i], resultBuffer[bufferOffset + i]);
}
for (int j = 0; j < bufferOffset; j++)
{
Assert.AreEqual(0, resultBuffer2[j]);
}
if (bufferOffset + blobSize < bufferSize)
{
for (int k = bufferOffset + blobSize; k < bufferSize; k++)
{
Assert.AreEqual(0, resultBuffer2[k]);
}
}
}
示例7: DoDownloadRangeToByteArrayTask
/// <summary>
/// Single put blob and get blob
/// </summary>
/// <param name="blobSize">The blob size.</param>
/// <param name="bufferSize">The output buffer size.</param>
/// <param name="bufferOffset">The output buffer offset.</param>
/// <param name="blobOffset">The blob offset.</param>
/// <param name="length">Length of the data range to download.</param>
/// <param name="overload">Run with overloaded parameters.</param>
private void DoDownloadRangeToByteArrayTask(ICloudBlob blob, int blobSize, int bufferSize, int bufferOffset, long? blobOffset, long? length, bool overload)
{
int downloadLength;
byte[] buffer = GetRandomBuffer(blobSize);
byte[] resultBuffer = new byte[bufferSize];
byte[] resultBuffer2 = new byte[bufferSize];
using (MemoryStream originalBlob = new MemoryStream(buffer))
{
if (overload)
{
blob.UploadFromStream(originalBlob);
downloadLength = blob.DownloadRangeToByteArrayAsync(
resultBuffer, bufferOffset, blobOffset, length, null, null, new OperationContext()).Result;
}
else
{
blob.UploadFromStream(originalBlob);
downloadLength = blob.DownloadRangeToByteArrayAsync(resultBuffer, bufferOffset, blobOffset, length).Result;
}
int downloadSize = Math.Min(blobSize - (int)(blobOffset.HasValue ? blobOffset.Value : 0), bufferSize - bufferOffset);
if (length.HasValue && (length.Value < downloadSize))
{
downloadSize = (int)length.Value;
}
Assert.AreEqual(downloadSize, downloadLength);
for (int i = 0; i < bufferOffset; i++)
{
Assert.AreEqual(0, resultBuffer[i]);
}
for (int j = 0; j < downloadLength; j++)
{
Assert.AreEqual(buffer[(blobOffset.HasValue ? blobOffset.Value : 0) + j], resultBuffer[bufferOffset + j]);
}
for (int k = bufferOffset + downloadLength; k < bufferSize; k++)
{
Assert.AreEqual(0, resultBuffer[k]);
}
}
}
示例8: BlobReadStreamReadSizeTest
private void BlobReadStreamReadSizeTest(ICloudBlob blob)
{
byte[] buffer = GetRandomBuffer(5 * 1024 * 1024);
using (MemoryStream wholeBlob = new MemoryStream(buffer))
{
blob.UploadFromStream(wholeBlob);
}
TestHelper.ExpectedException<ArgumentOutOfRangeException>(
() => blob.StreamMinimumReadSizeInBytes = 16 * 1024 - 1,
"StreamMinimumReadSizeInBytes should not accept values smaller than 16KB");
blob.StreamMinimumReadSizeInBytes = 4 * 1024 * 1024 + 1;
BlobRequestOptions options = new BlobRequestOptions() { UseTransactionalMD5 = true };
TestHelper.ExpectedException<ArgumentOutOfRangeException>(
() => blob.OpenRead(null, options, null),
"StreamMinimumReadSizeInBytes should be smaller than 4MB if UseTransactionalMD5 is true");
string range = null;
OperationContext context = new OperationContext();
context.SendingRequest += (sender, e) => range = range ?? e.Request.Headers["x-ms-range"];
blob.StreamMinimumReadSizeInBytes = 4 * 1024 * 1024;
using (Stream blobStream = blob.OpenRead(null, options, context))
{
blobStream.ReadByte();
Assert.AreEqual("bytes=0-" + (blob.StreamMinimumReadSizeInBytes - 1).ToString(), range);
range = null;
}
blob.StreamMinimumReadSizeInBytes = 6 * 1024 * 1024;
options.UseTransactionalMD5 = false;
using (Stream blobStream = blob.OpenRead(null, options, context))
{
blobStream.ReadByte();
Assert.AreEqual("bytes=0-" + (buffer.Length - 1).ToString(), range);
range = null;
}
blob.StreamMinimumReadSizeInBytes = 16 * 1024;
using (Stream blobStream = blob.OpenRead(null, options, context))
{
blobStream.ReadByte();
Assert.AreEqual("bytes=0-" + (blob.StreamMinimumReadSizeInBytes - 1).ToString(), range);
range = null;
}
}
示例9: _handleWebException
private bool _handleWebException( ICloudBlob blob, StorageException err, string _lockFile )
{
if ( err.RequestInformation.HttpStatusCode == 404 ) {
this.blobContainer.CreateIfNotExists();
using ( MemoryStream stream = new MemoryStream() ) {
using ( StreamWriter writer = new StreamWriter( stream ) ) {
writer.Write( _lockFile );
blob.UploadFromStream( stream );
}
}
return true;
}
return false;
}