本文整理汇总了C#中Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamAsync方法的典型用法代码示例。如果您正苦于以下问题:C# CloudBlockBlob.UploadFromStreamAsync方法的具体用法?C# CloudBlockBlob.UploadFromStreamAsync怎么用?C# CloudBlockBlob.UploadFromStreamAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob
的用法示例。
在下文中一共展示了CloudBlockBlob.UploadFromStreamAsync方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UploadBlockBlobAsync
public async virtual void UploadBlockBlobAsync(CloudBlockBlob blockBlob, Stream streamToUpload, AccessCondition accessCondition = null)
{
await blockBlob.UploadFromStreamAsync(streamToUpload, accessCondition, null, null);
}
示例2: Upload
public async Task Upload(Stream blob, string containerName, string path)
{
CloudBlobContainer container = _blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
container.SetPermissions(new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
_blockBlob = container.GetBlockBlobReference(path);
using (var fileStream = blob)
{
await _blockBlob.UploadFromStreamAsync(fileStream);
}
}
示例3: TestBlobSASAsync
/// <summary>
/// Tests a blob SAS to determine which operations it allows.
/// </summary>
/// <param name="sasUri">A string containing a URI with a SAS appended.</param>
/// <param name="blobContent">A string content content to write to the blob.</param>
/// <returns>A Task object.</returns>
private static async Task TestBlobSASAsync(string sasUri, string blobContent)
{
// Try performing blob operations using the SAS provided.
// Return a reference to the blob using the SAS URI.
CloudBlockBlob blob = new CloudBlockBlob(new Uri(sasUri));
// Create operation: Upload a blob with the specified name to the container.
// If the blob does not exist, it will be created. If it does exist, it will be overwritten.
try
{
MemoryStream msWrite = new MemoryStream(Encoding.UTF8.GetBytes(blobContent));
msWrite.Position = 0;
using (msWrite)
{
await blob.UploadFromStreamAsync(msWrite);
}
Console.WriteLine("Create operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("Create operation failed for SAS {0}", sasUri);
Console.WriteLine("Additional error information: " + e.Message);
Console.WriteLine();
}
else
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
// Write operation: Add metadata to the blob
try
{
await blob.FetchAttributesAsync();
string rnd = new Random().Next().ToString();
string metadataName = "name";
string metadataValue = "value";
blob.Metadata.Add(metadataName, metadataValue);
await blob.SetMetadataAsync();
Console.WriteLine("Write operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("Write operation failed for SAS {0}", sasUri);
Console.WriteLine("Additional error information: " + e.Message);
Console.WriteLine();
}
else
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
// Read operation: Read the contents of the blob.
try
{
MemoryStream msRead = new MemoryStream();
using (msRead)
{
await blob.DownloadToStreamAsync(msRead);
msRead.Position = 0;
using (StreamReader reader = new StreamReader(msRead, true))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
Console.WriteLine();
}
Console.WriteLine("Read operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("Read operation failed for SAS {0}", sasUri);
//.........这里部分代码省略.........
示例4: UploadPhoto
/// <summary>
/// Uploads the photo.
/// </summary>
/// <param name="stream">The memory stream.</param>
/// <param name="localPath">The local path.</param>
/// <param name="caption">The caption.</param>
/// <param name="categoryId">The id of the assocaited category.</param>
/// <returns>The uploaded photo.</returns>
public async Task<Photo> UploadPhoto(Stream stream, string localPath, string caption, string categoryId)
{
try
{
var sasContracts = await GetSasUrls();
// Upload photos into azure
foreach (var sasContract in sasContracts)
{
var blob =
new CloudBlockBlob(
new Uri($"{sasContract.FullBlobUri}{sasContract.SasToken}"));
var sideLength = sasContract.SasPhotoType.ToSideLength();
var resizedStream = await BitmapTools.Resize(
stream.AsRandomAccessStream(), sideLength,
sideLength);
await blob.UploadFromStreamAsync(resizedStream);
}
var photoContract = new PhotoContract
{
CategoryId = categoryId,
Description = caption,
OSPlatform = AnalyticsInfo.VersionInfo.DeviceFamily,
User = AppEnvironment.Instance.CurrentUser.ToDataContract(),
ThumbnailUrl =
sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.Thumbnail)?
.FullBlobUri.ToString(),
StandardUrl =
sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.Standard)?
.FullBlobUri.ToString(),
HighResolutionUrl =
sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.HighRes)?
.FullBlobUri.ToString()
};
var responsePhotoContract =
await _mobileServiceClient.InvokeApiAsync<PhotoContract, PhotoContract>("Photo",
photoContract,
HttpMethod.Post,
null);
return responsePhotoContract.ToDataModel();
}
catch (Exception e)
{
throw new ServiceException("UploadPhoto error", e);
}
}
示例5: UploadToBlobAsync
internal async Task UploadToBlobAsync(String blobName, System.IO.Stream source)
{
var fileUploadRequest = new FileUploadRequest()
{
BlobName = blobName
};
var fileUploadResponse = await this.httpClientHelper.PostAsync<FileUploadRequest, FileUploadResponse>(
GetRequestUri(this.deviceId, CommonConstants.BlobUploadPathTemplate, null),
fileUploadRequest,
ExceptionHandlingHelper.GetDefaultErrorMapping(),
null,
CancellationToken.None);
string putString = String.Format("https://{0}/{1}/{2}{3}",
fileUploadResponse.HostName,
fileUploadResponse.ContainerName,
fileUploadResponse.BlobName,
fileUploadResponse.SasToken);
var notification = new FileUploadNotificationResponse();
try
{
// 2. Use SAS URI to send data to Azure Storage Blob (PUT)
CloudBlockBlob blob = new CloudBlockBlob(new Uri(putString));
var uploadTask = blob.UploadFromStreamAsync(source);
await uploadTask;
notification.CorrelationId = fileUploadResponse.CorrelationId;
notification.IsSuccess = uploadTask.IsCompleted;
notification.StatusCode = uploadTask.IsCompleted ? 0 : -1;
notification.StatusDescription = uploadTask.IsCompleted ? null : "Failed to upload to storage.";
// 3. POST to IoTHub with upload status
await this.httpClientHelper.PostAsync<FileUploadNotificationResponse>(
GetRequestUri(this.deviceId, CommonConstants.BlobUploadStatusPathTemplate + "notifications", null),
notification,
ExceptionHandlingHelper.GetDefaultErrorMapping(),
null,
CancellationToken.None);
}
catch (Exception ex)
{
// 3. POST to IoTHub with upload status
notification.IsSuccess = false;
notification.StatusCode = -1;
notification.StatusDescription = ex.Message;
await this.httpClientHelper.PostAsync<FileUploadNotificationResponse>(
GetRequestUri(this.deviceId, CommonConstants.BlobUploadStatusPathTemplate + "notifications/" + fileUploadResponse.CorrelationId, null),
notification,
ExceptionHandlingHelper.GetDefaultErrorMapping(),
null,
CancellationToken.None);
throw ex;
}
}
示例6: UploadContentToBlobStorage
public override async Task<Uri> UploadContentToBlobStorage(string containerName, string fileName, string contentType, Stream fileContents, string sasTokenUri)
{
var storageCredentials = new StorageCredentials(sasTokenUri);
var storageAccount = new CloudStorageAccount(storageCredentials, true);
var blob = new CloudBlockBlob(null, null);
blob.Properties.ContentType = contentType;
await blob.UploadFromStreamAsync(fileContents);
UriBuilder uriBuilder = new UriBuilder(sasTokenUri);
uriBuilder.Query = null;
uriBuilder.Fragment = null;
return uriBuilder.Uri;
}
示例7: ResizeImage
private static async Task<string> ResizeImage(Stream streamInput, CloudBlockBlob blobOutput, ImageSize size)
{
streamInput.Position = 0;
using (var memoryStream = new MemoryStream()) {
// use a memory stream, since using the blob stream directly causes InvalidOperationException due to the way image resizer works
var instructions = new Instructions(imageDimensionsTable[size]);
var job = new ImageJob(streamInput, memoryStream, instructions, disposeSource: false, addFileExtension: false);
// use the advanced version of resize so that we can get the content type
var result = ImageBuilder.Current.Build(job);
memoryStream.Position = 0;
await blobOutput.UploadFromStreamAsync(memoryStream);
var contentType = result.ResultMimeType;
blobOutput.Properties.ContentType = contentType;
blobOutput.SetProperties();
return contentType;
}
}