本文整理汇总了C#中Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.GetPageBlobReference方法的典型用法代码示例。如果您正苦于以下问题:C# CloudBlobContainer.GetPageBlobReference方法的具体用法?C# CloudBlobContainer.GetPageBlobReference怎么用?C# CloudBlobContainer.GetPageBlobReference使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer
的用法示例。
在下文中一共展示了CloudBlobContainer.GetPageBlobReference方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestAccess
private static void TestAccess(string sasToken, SharedAccessBlobPermissions permissions, SharedAccessBlobHeaders headers, CloudBlobContainer container, CloudBlob blob)
{
CloudBlob SASblob;
StorageCredentials credentials = string.IsNullOrEmpty(sasToken) ?
new StorageCredentials() :
new StorageCredentials(sasToken);
if (container != null)
{
container = new CloudBlobContainer(credentials.TransformUri(container.Uri));
if (blob.BlobType == BlobType.BlockBlob)
{
SASblob = container.GetBlockBlobReference(blob.Name);
}
else if (blob.BlobType == BlobType.PageBlob)
{
SASblob = container.GetPageBlobReference(blob.Name);
}
else
{
SASblob = container.GetAppendBlobReference(blob.Name);
}
}
else
{
if (blob.BlobType == BlobType.BlockBlob)
{
SASblob = new CloudBlockBlob(credentials.TransformUri(blob.Uri));
}
else if (blob.BlobType == BlobType.PageBlob)
{
SASblob = new CloudPageBlob(credentials.TransformUri(blob.Uri));
}
else
{
SASblob = new CloudAppendBlob(credentials.TransformUri(blob.Uri));
}
}
HttpStatusCode failureCode = sasToken == null ? HttpStatusCode.NotFound : HttpStatusCode.Forbidden;
// We want to ensure that 'create', 'add', and 'write' permissions all allow for correct writing of blobs, as is reasonable.
if (((permissions & SharedAccessBlobPermissions.Create) == SharedAccessBlobPermissions.Create) || ((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write))
{
if (blob.BlobType == BlobType.PageBlob)
{
CloudPageBlob SASpageBlob = (CloudPageBlob)SASblob;
SASpageBlob.Create(512);
CloudPageBlob pageBlob = (CloudPageBlob)blob;
byte[] buffer = new byte[512];
buffer[0] = 2; // random data
if (((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write))
{
SASpageBlob.UploadFromByteArray(buffer, 0, 512);
}
else
{
TestHelper.ExpectedException(
() => SASpageBlob.UploadFromByteArray(buffer, 0, 512),
"pageBlob SAS token without Write perms should not allow for writing/adding",
failureCode);
pageBlob.UploadFromByteArray(buffer, 0, 512);
}
}
else if (blob.BlobType == BlobType.BlockBlob)
{
if ((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write)
{
UploadText(SASblob, "blob", Encoding.UTF8);
}
else
{
TestHelper.ExpectedException(
() => UploadText(SASblob, "blob", Encoding.UTF8),
"Block blob SAS token without Write or perms should not allow for writing",
failureCode);
UploadText(blob, "blob", Encoding.UTF8);
}
}
else // append blob
{
// If the sas token contains Feb 2012, append won't be accepted
if (sasToken.Contains(Constants.VersionConstants.February2012))
{
UploadText(blob, "blob", Encoding.UTF8);
}
else
{
CloudAppendBlob SASAppendBlob = SASblob as CloudAppendBlob;
SASAppendBlob.CreateOrReplace();
byte[] textAsBytes = Encoding.UTF8.GetBytes("blob");
using (MemoryStream stream = new MemoryStream())
{
stream.Write(textAsBytes, 0, textAsBytes.Length);
stream.Seek(0, SeekOrigin.Begin);
if (((permissions & SharedAccessBlobPermissions.Add) == SharedAccessBlobPermissions.Add) || ((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write))
{
//.........这里部分代码省略.........
示例2: CopyBlobDirectory
// copy a directory of the same container to another container
public static List<ICancellableAsyncResult> CopyBlobDirectory(CloudBlobDirectory srcDirectory, CloudBlobContainer destContainer, string sourceblobToken)
{
List<ICancellableAsyncResult> mylistresults = new List<ICancellableAsyncResult>();
var srcBlobList = srcDirectory.ListBlobs(
useFlatBlobListing: true,
blobListingDetails: BlobListingDetails.None).ToList();
foreach (var src in srcBlobList)
{
var srcBlob = src as ICloudBlob;
// Create appropriate destination blob type to match the source blob
ICloudBlob destBlob;
if (srcBlob.Properties.BlobType == BlobType.BlockBlob)
destBlob = destContainer.GetBlockBlobReference(srcBlob.Name);
else
destBlob = destContainer.GetPageBlobReference(srcBlob.Name);
// copy using src blob as SAS
mylistresults.Add(destBlob.BeginStartCopyFromBlob(new Uri(srcBlob.Uri.AbsoluteUri + sourceblobToken), null, null));
}
return mylistresults;
}
示例3: CloudBlobDirectorySetupWithDelimiterAsync
private async Task<bool> CloudBlobDirectorySetupWithDelimiterAsync(CloudBlobContainer container, string delimiter = "/")
{
try
{
for (int i = 1; i < 3; i++)
{
for (int j = 1; j < 3; j++)
{
for (int k = 1; k < 3; k++)
{
String directory = "TopDir" + i + delimiter + "MidDir" + j + delimiter + "EndDir" + k + delimiter + "EndBlob" + k;
CloudPageBlob blob1 = container.GetPageBlobReference(directory);
await blob1.CreateAsync(0);
}
}
CloudPageBlob blob2 = container.GetPageBlobReference("TopDir" + i + delimiter + "Blob" + i);
await blob2.CreateAsync(0);
}
return true;
}
catch (Exception e)
{
throw e;
}
}
示例4: GetReader
public static MessageReader GetReader(string sas)
{
var uri = new Uri(sas);
var container = new CloudBlobContainer(uri);
var posBlob = container.GetPageBlobReference(Constants.PositionFileName);
var dataBlob = container.GetPageBlobReference(Constants.StreamFileName);
var position = new CloudCheckpointReader(posBlob);
var messages = new CloudPageReader(dataBlob);
return new MessageReader(position, messages);
}
示例5: CreateAndInitWriter
public static MessageWriter CreateAndInitWriter(CloudBlobContainer container)
{
container.CreateIfNotExists();
var dataBlob = container.GetPageBlobReference(Constants.StreamFileName);
var posBlob = container.GetPageBlobReference(Constants.PositionFileName);
var pageWriter = new CloudPageWriter(dataBlob);
var posWriter = new CloudCheckpointWriter(posBlob);
var writer = new MessageWriter(pageWriter, posWriter);
writer.Init();
return writer;
}
示例6: CreateBlobs
public static List<string> CreateBlobs(CloudBlobContainer container, int count, BlobType type)
{
string name;
List<string> blobs = new List<string>();
for (int i = 0; i < count; i++)
{
switch (type)
{
case BlobType.BlockBlob:
name = "bb" + Guid.NewGuid().ToString();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
blockBlob.PutBlockList(new string[] { });
blobs.Add(name);
break;
case BlobType.PageBlob:
name = "pb" + Guid.NewGuid().ToString();
CloudPageBlob pageBlob = container.GetPageBlobReference(name);
pageBlob.Create(0);
blobs.Add(name);
break;
}
}
return blobs;
}
示例7: CreateBlobsAsync
public static async Task<List<string>> CreateBlobsAsync(CloudBlobContainer container, int count, BlobType type)
{
string name;
List<string> blobs = new List<string>();
for (int i = 0; i < count; i++)
{
switch (type)
{
case BlobType.BlockBlob:
name = "bb" + Guid.NewGuid().ToString();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
await blockBlob.PutBlockListAsync(new string[] { });
blobs.Add(name);
break;
case BlobType.PageBlob:
name = "pb" + Guid.NewGuid().ToString();
CloudPageBlob pageBlob = container.GetPageBlobReference(name);
await pageBlob.CreateAsync(0);
blobs.Add(name);
break;
case BlobType.AppendBlob:
name = "ab" + Guid.NewGuid().ToString();
CloudAppendBlob appendBlob = container.GetAppendBlobReference(name);
await appendBlob.CreateOrReplaceAsync();
blobs.Add(name);
break;
}
}
return blobs;
}
示例8: GetBlob
public static ICloudBlob GetBlob(CloudBlobContainer container, string blobName, StorageType blobType)
{
ICloudBlob blob = null;
if (blobType == StorageType.BlockBlob)
{
blob = container.GetBlockBlobReference(blobName);
}
else
{
blob = container.GetPageBlobReference(blobName);
}
return blob;
}
示例9: CloudPageBlobUploadFromStreamTask
private void CloudPageBlobUploadFromStreamTask(CloudBlobContainer container, int size, long? copyLength, AccessCondition accessCondition, int startOffset, bool testMd5)
{
try
{
byte[] buffer = GetRandomBuffer(size);
MD5 hasher = MD5.Create();
string md5 = string.Empty;
if (testMd5)
{
md5 = Convert.ToBase64String(hasher.ComputeHash(buffer, startOffset, copyLength.HasValue ? (int)copyLength : buffer.Length - startOffset));
}
CloudPageBlob blob = container.GetPageBlobReference("blob1");
blob.StreamWriteSizeInBytes = 512;
using (MemoryStream originalBlob = new MemoryStream())
{
originalBlob.Write(buffer, startOffset, buffer.Length - startOffset);
using (MemoryStream sourceStream = new MemoryStream(buffer))
{
sourceStream.Seek(startOffset, SeekOrigin.Begin);
BlobRequestOptions options = new BlobRequestOptions()
{
StoreBlobContentMD5 = true,
};
if (copyLength.HasValue)
{
blob.UploadFromStreamAsync(sourceStream, copyLength.Value, accessCondition, options, null).Wait();
}
else
{
blob.UploadFromStreamAsync(sourceStream, accessCondition, options, null).Wait();
}
}
blob.FetchAttributesAsync().Wait();
if (testMd5)
{
Assert.AreEqual(md5, blob.Properties.ContentMD5);
}
using (MemoryStream downloadedBlob = new MemoryStream())
{
blob.DownloadToStreamAsync(downloadedBlob).Wait();
TestHelper.AssertStreamsAreEqualAtIndex(
originalBlob,
downloadedBlob,
0,
0,
copyLength.HasValue ? (int)copyLength : (int)originalBlob.Length);
}
}
}
catch (AggregateException ex)
{
throw ex.InnerException;
}
}
示例10: CloudPageBlobUploadFromStream
private void CloudPageBlobUploadFromStream(CloudBlobContainer container, int size, long? copyLength, AccessCondition accessCondition, int startOffset, bool isAsync, bool testMd5)
{
byte[] buffer = GetRandomBuffer(size);
MD5 hasher = MD5.Create();
string md5 = string.Empty;
if (testMd5)
{
md5 = Convert.ToBase64String(hasher.ComputeHash(buffer, startOffset, copyLength.HasValue ? (int)copyLength : buffer.Length - startOffset));
}
CloudPageBlob blob = container.GetPageBlobReference("blob1");
blob.StreamWriteSizeInBytes = 512;
using (MemoryStream originalBlob = new MemoryStream())
{
originalBlob.Write(buffer, startOffset, buffer.Length - startOffset);
using (MemoryStream sourceStream = new MemoryStream(buffer))
{
sourceStream.Seek(startOffset, SeekOrigin.Begin);
BlobRequestOptions options = new BlobRequestOptions()
{
StoreBlobContentMD5 = true,
};
if (isAsync)
{
using (ManualResetEvent waitHandle = new ManualResetEvent(false))
{
if (copyLength.HasValue)
{
ICancellableAsyncResult result = blob.BeginUploadFromStream(
sourceStream, copyLength.Value, accessCondition, options, null, ar => waitHandle.Set(), null);
waitHandle.WaitOne();
blob.EndUploadFromStream(result);
}
else
{
ICancellableAsyncResult result = blob.BeginUploadFromStream(
sourceStream, accessCondition, options, null, ar => waitHandle.Set(), null);
waitHandle.WaitOne();
blob.EndUploadFromStream(result);
}
}
}
else
{
if (copyLength.HasValue)
{
blob.UploadFromStream(sourceStream, copyLength.Value, accessCondition, options);
}
else
{
blob.UploadFromStream(sourceStream, accessCondition, options);
}
}
}
blob.FetchAttributes();
if (testMd5)
{
Assert.AreEqual(md5, blob.Properties.ContentMD5);
}
using (MemoryStream downloadedBlob = new MemoryStream())
{
if (isAsync)
{
using (ManualResetEvent waitHandle = new ManualResetEvent(false))
{
ICancellableAsyncResult result = blob.BeginDownloadToStream(downloadedBlob,
ar => waitHandle.Set(),
null);
waitHandle.WaitOne();
blob.EndDownloadToStream(result);
}
}
else
{
blob.DownloadToStream(downloadedBlob);
}
TestHelper.AssertStreamsAreEqualAtIndex(
originalBlob,
downloadedBlob,
0,
0,
copyLength.HasValue ? (int)copyLength : (int)originalBlob.Length);
}
}
}
示例11: TestAccessAsync
private static async Task TestAccessAsync(string sasToken, SharedAccessBlobPermissions permissions, SharedAccessBlobHeaders headers, CloudBlobContainer container, CloudBlob blob)
{
OperationContext operationContext = new OperationContext();
StorageCredentials credentials = string.IsNullOrEmpty(sasToken) ?
new StorageCredentials() :
new StorageCredentials(sasToken);
if (container != null)
{
container = new CloudBlobContainer(container.Uri, credentials);
if (blob.BlobType == BlobType.BlockBlob)
{
blob = container.GetBlockBlobReference(blob.Name);
}
else
{
blob = container.GetPageBlobReference(blob.Name);
}
}
else
{
if (blob.BlobType == BlobType.BlockBlob)
{
blob = new CloudBlockBlob(blob.Uri, credentials);
}
else
{
blob = new CloudPageBlob(blob.Uri, credentials);
}
}
if (container != null)
{
if ((permissions & SharedAccessBlobPermissions.List) == SharedAccessBlobPermissions.List)
{
await container.ListBlobsSegmentedAsync(null);
}
else
{
await TestHelper.ExpectedExceptionAsync(
async () => await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.None, null, null, null, operationContext),
operationContext,
"List blobs while SAS does not allow for listing",
HttpStatusCode.Forbidden);
}
}
if ((permissions & SharedAccessBlobPermissions.Read) == SharedAccessBlobPermissions.Read)
{
await blob.FetchAttributesAsync();
// Test headers
if (headers != null)
{
if (headers.CacheControl != null)
{
Assert.AreEqual(headers.CacheControl, blob.Properties.CacheControl);
}
if (headers.ContentDisposition != null)
{
Assert.AreEqual(headers.ContentDisposition, blob.Properties.ContentDisposition);
}
if (headers.ContentEncoding != null)
{
Assert.AreEqual(headers.ContentEncoding, blob.Properties.ContentEncoding);
}
if (headers.ContentLanguage != null)
{
Assert.AreEqual(headers.ContentLanguage, blob.Properties.ContentLanguage);
}
if (headers.ContentType != null)
{
Assert.AreEqual(headers.ContentType, blob.Properties.ContentType);
}
}
}
else
{
await TestHelper.ExpectedExceptionAsync(
async () => await blob.FetchAttributesAsync(null, null, operationContext),
operationContext,
"Fetch blob attributes while SAS does not allow for reading",
HttpStatusCode.Forbidden);
}
if ((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write)
{
await blob.SetMetadataAsync();
}
else
{
await TestHelper.ExpectedExceptionAsync(
async () => await blob.SetMetadataAsync(null, null, operationContext),
operationContext,
"Set blob metadata while SAS does not allow for writing",
HttpStatusCode.Forbidden);
//.........这里部分代码省略.........
示例12: WritePageBlob
/// <summary>
/// Write page blob. Although concurrency params exist, does NOT do concurrent uploading yet.
/// </summary>
/// <param name="stream"></param>
/// <param name="blob"></param>
/// <param name="container"></param>
/// <param name="parallelFactor"></param>
/// <param name="chunkSizeInMB"></param>
private void WritePageBlob(Stream stream, string blobName, Blob blob, CloudBlobContainer container, int parallelFactor = 1, int chunkSizeInMB = 2)
{
var blobRef = container.GetPageBlobReference(blobName);
blobRef.UploadFromStream(stream);
}
示例13: CloudPageBlobUploadFromStreamAsync
private async Task CloudPageBlobUploadFromStreamAsync(CloudBlobContainer container, int size, long? copyLength, AccessCondition accessCondition, OperationContext operationContext, int startOffset)
{
byte[] buffer = GetRandomBuffer(size);
CloudPageBlob blob = container.GetPageBlobReference("blob1");
blob.StreamWriteSizeInBytes = 512;
using (MemoryStream originalBlobStream = new MemoryStream())
{
originalBlobStream.Write(buffer, startOffset, buffer.Length - startOffset);
using (MemoryStream sourceStream = new MemoryStream(buffer))
{
sourceStream.Seek(startOffset, SeekOrigin.Begin);
if (copyLength.HasValue)
{
await blob.UploadFromStreamAsync(sourceStream, copyLength.Value, accessCondition, null, operationContext);
}
else
{
await blob.UploadFromStreamAsync(sourceStream, accessCondition, null, operationContext);
}
}
using (MemoryStream downloadedBlobStream = new MemoryStream())
{
await blob.DownloadToStreamAsync(downloadedBlobStream);
Assert.AreEqual(copyLength ?? originalBlobStream.Length, downloadedBlobStream.Length);
TestHelper.AssertStreamsAreEqualAtIndex(
originalBlobStream,
downloadedBlobStream,
0,
0,
copyLength.HasValue ? (int)copyLength : (int)originalBlobStream.Length);
}
}
}
示例14: CloudPageBlobUploadFromStreamAsync
private async Task CloudPageBlobUploadFromStreamAsync(CloudBlobContainer container, int size, long? copyLength, AccessCondition accessCondition, OperationContext operationContext, int startOffset, bool testMd5)
{
byte[] buffer = GetRandomBuffer(size);
string md5 = string.Empty;
if (testMd5)
{
#if ASPNET_K
MD5 hasher = MD5.Create();
md5 = Convert.ToBase64String(hasher.ComputeHash(buffer, startOffset, copyLength.HasValue ? (int)copyLength : buffer.Length - startOffset));
#else
CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash();
hasher.Append(buffer.AsBuffer(startOffset, copyLength.HasValue ? (int)copyLength : buffer.Length - startOffset));
md5 = CryptographicBuffer.EncodeToBase64String(hasher.GetValueAndReset());
#endif
}
CloudPageBlob blob = container.GetPageBlobReference("blob1");
blob.StreamWriteSizeInBytes = 512;
using (MemoryStream originalBlobStream = new MemoryStream())
{
originalBlobStream.Write(buffer, startOffset, buffer.Length - startOffset);
using (MemoryStream sourceStream = new MemoryStream(buffer))
{
sourceStream.Seek(startOffset, SeekOrigin.Begin);
BlobRequestOptions options = new BlobRequestOptions()
{
StoreBlobContentMD5 = true,
};
if (copyLength.HasValue)
{
await blob.UploadFromStreamAsync(sourceStream, copyLength.Value, accessCondition, options, operationContext);
}
else
{
await blob.UploadFromStreamAsync(sourceStream, accessCondition, options, operationContext);
}
}
if (testMd5)
{
await blob.FetchAttributesAsync();
Assert.AreEqual(md5, blob.Properties.ContentMD5);
}
using (MemoryOutputStream downloadedBlobStream = new MemoryOutputStream())
{
await blob.DownloadToStreamAsync(downloadedBlobStream);
Assert.AreEqual(copyLength ?? originalBlobStream.Length, downloadedBlobStream.UnderlyingStream.Length);
TestHelper.AssertStreamsAreEqualAtIndex(
originalBlobStream,
downloadedBlobStream.UnderlyingStream,
0,
0,
copyLength.HasValue ? (int)copyLength : (int)originalBlobStream.Length);
}
}
}
示例15: PageRangesSample
/// <summary>
/// Get a list of valid page ranges for a page blob
/// </summary>
/// <param name="container"></param>
/// <returns>A Task object.</returns>
private static async Task PageRangesSample(CloudBlobContainer container)
{
BlobRequestOptions requestOptions = new BlobRequestOptions { RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(1), 3) };
await container.CreateIfNotExistsAsync(requestOptions, null);
Console.WriteLine("Create Page Blob");
CloudPageBlob pageBlob = container.GetPageBlobReference("blob1");
pageBlob.Create(4 * 1024);
Console.WriteLine("Write Pages to Blob");
byte[] buffer = GetRandomBuffer(1024);
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
pageBlob.WritePages(memoryStream, 512);
}
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
pageBlob.WritePages(memoryStream, 3 * 1024);
}
Console.WriteLine("Get Page Ranges");
IEnumerable<PageRange> pageRanges = pageBlob.GetPageRanges();
foreach (PageRange pageRange in pageRanges)
{
Console.WriteLine(pageRange.ToString());
}
// Clean up after the demo. This line is not strictly necessary as the container is deleted in the next call.
// It is included for the purposes of the example.
Console.WriteLine("Delete page blob");
await pageBlob.DeleteIfExistsAsync();
Console.WriteLine();
}