本文整理汇总了C#中Microsoft.WindowsAzure.Storage.File.CloudFile.UploadFromFile方法的典型用法代码示例。如果您正苦于以下问题:C# CloudFile.UploadFromFile方法的具体用法?C# CloudFile.UploadFromFile怎么用?C# CloudFile.UploadFromFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.WindowsAzure.Storage.File.CloudFile
的用法示例。
在下文中一共展示了CloudFile.UploadFromFile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UploadFiles
// TODO: extract the common pattern(Upload and Download)
private static void UploadFiles(Dictionary<string, DataSyncFileInfo> localOnly, CloudFileClient fileClient)
{
List<Task> waitTasks = new List<Task>();
foreach (var file in localOnly)
{
var path = file.Key.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (path.Length < 2)
{
throw new Exception("share is needed, so you should create a flod in local.");
}
string shareString = path[0];
fileClient.GetShareReference(shareString).CreateIfNotExists();
string dirString = path[0] + "/";
for (int i = 1; i < path.Length - 1; i++)
{
dirString += path[i] + "/";
CloudFileDirectory fileDir = new CloudFileDirectory(
new Uri(fileClient.BaseUri.AbsoluteUri + dirString), fileClient.Credentials);
fileDir.CreateIfNotExists();
}
var uploadTask = Task.Run(() =>
{
Console.WriteLine($"upload {file.Key} ...");
CloudFile cloudFile = new CloudFile(new Uri(fileClient.BaseUri.AbsoluteUri + file.Key.Substring(1)), fileClient.Credentials);
cloudFile.UploadFromFile(file.Value.LocalFile.FullName);
Console.WriteLine($"{file.Key} is uploaded!");
});
waitTasks.Add(uploadTask);
// TODO: config the number of running task
while (waitTasks.Count(task =>
task.Status == TaskStatus.Running || task.Status == TaskStatus.WaitingToRun) >= 8)
{
Task.WaitAny(waitTasks.ToArray());
}
}
Task.WaitAll(waitTasks.ToArray());
}
示例2: DoUploadDownloadFile
private void DoUploadDownloadFile(CloudFile file, int fileSize, bool isAsync)
{
string inputFileName = Path.GetTempFileName();
string outputFileName = Path.GetTempFileName();
try
{
byte[] buffer = GetRandomBuffer(fileSize);
using (FileStream fileStream = new FileStream(inputFileName, FileMode.Create, FileAccess.Write))
{
fileStream.Write(buffer, 0, buffer.Length);
}
if (isAsync)
{
IAsyncResult result;
using (AutoResetEvent waitHandle = new AutoResetEvent(false))
{
result = file.BeginUploadFromFile(inputFileName, FileMode.Open,
ar => waitHandle.Set(),
null);
waitHandle.WaitOne();
file.EndUploadFromFile(result);
OperationContext context = new OperationContext();
result = file.BeginUploadFromFile(inputFileName, FileMode.Open, null, null, context,
ar => waitHandle.Set(),
null);
waitHandle.WaitOne();
file.EndUploadFromFile(result);
Assert.IsNotNull(context.LastResult.ServiceRequestID);
TestHelper.ExpectedException<IOException>(
() => file.BeginDownloadToFile(outputFileName, FileMode.CreateNew, null, null),
"CreateNew on an existing file should fail");
context = new OperationContext();
result = file.BeginDownloadToFile(outputFileName, FileMode.Create, null, null, context,
ar => waitHandle.Set(),
null);
waitHandle.WaitOne();
file.EndDownloadToFile(result);
Assert.IsNotNull(context.LastResult.ServiceRequestID);
using (FileStream inputFile = new FileStream(inputFileName, FileMode.Open, FileAccess.Read),
outputFile = new FileStream(outputFileName, FileMode.Open, FileAccess.Read))
{
TestHelper.AssertStreamsAreEqual(inputFile, outputFile);
}
result = file.BeginDownloadToFile(outputFileName, FileMode.Append,
ar => waitHandle.Set(),
null);
waitHandle.WaitOne();
file.EndDownloadToFile(result);
using (FileStream inputFile = new FileStream(inputFileName, FileMode.Open, FileAccess.Read),
outputFile = new FileStream(outputFileName, FileMode.Open, FileAccess.Read))
{
Assert.AreEqual(2 * fileSize, outputFile.Length);
for (int i = 0; i < fileSize; i++)
{
Assert.AreEqual(inputFile.ReadByte(), outputFile.ReadByte());
}
inputFile.Seek(0, SeekOrigin.Begin);
for (int i = 0; i < fileSize; i++)
{
Assert.AreEqual(inputFile.ReadByte(), outputFile.ReadByte());
}
}
}
}
else
{
file.UploadFromFile(inputFileName, FileMode.Open);
OperationContext context = new OperationContext();
file.UploadFromFile(inputFileName, FileMode.Open, null, null, context);
Assert.IsNotNull(context.LastResult.ServiceRequestID);
TestHelper.ExpectedException<IOException>(
() => file.DownloadToFile(outputFileName, FileMode.CreateNew),
"CreateNew on an existing file should fail");
context = new OperationContext();
file.DownloadToFile(outputFileName, FileMode.Create, null, null, context);
Assert.IsNotNull(context.LastResult.ServiceRequestID);
using (FileStream inputFileStream = new FileStream(inputFileName, FileMode.Open, FileAccess.Read),
outputFileStream = new FileStream(outputFileName, FileMode.Open, FileAccess.Read))
{
TestHelper.AssertStreamsAreEqual(inputFileStream, outputFileStream);
}
file.DownloadToFile(outputFileName, FileMode.Append);
using (FileStream inputFileStream = new FileStream(inputFileName, FileMode.Open, FileAccess.Read),
outputFileStream = new FileStream(outputFileName, FileMode.Open, FileAccess.Read))
//.........这里部分代码省略.........