-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathFileSamples.cs
66 lines (59 loc) · 2.53 KB
/
FileSamples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Azure.WebJobs;
namespace ExtensionsSample
{
public static class FileSamples
{
// When new files arrive in the "import" directory, they are uploaded to a blob
// container then deleted.
public static async Task ImportFile(
[FileTrigger(@"import/{name}", "*.dat", autoDelete: true)] Stream file,
[Blob(@"processed/{name}")] CloudBlockBlob output,
string name,
TextWriter log)
{
await output.UploadFromStreamAsync(file);
file.Close();
log.WriteLine(string.Format("Processed input file '{0}'!", name));
}
// When files are created or modified in the "cache" directory, this job will be triggered.
public static void ChangeWatcher(
[FileTrigger(@"cache\{name}", "*.txt", WatcherChangeTypes.Created | WatcherChangeTypes.Changed)] string file,
FileSystemEventArgs fileTrigger,
TextWriter log)
{
log.WriteLine(string.Format("Processed input file '{0}'!", fileTrigger.Name));
}
// Drop a file in the "convert" directory, and this function will reverse it
// the contents and write the file to the "converted" directory.
public static void Converter(
[FileTrigger(@"convert\{name}", "*.txt", autoDelete: true)] string file,
[File(@"converted\{name}", FileAccess.Write)] out string converted)
{
char[] arr = file.ToCharArray();
Array.Reverse(arr);
converted = new string(arr);
}
// Every time the timer fires, this file will update a file with the current time.
public static void Heartbeat(
[TimerTrigger("*/5 * * * * *")] TimerInfo timerInfo,
[File(@"heartbeat.txt", FileAccess.Write, FileMode.Append)] Stream file)
{
using (StreamWriter sw = new StreamWriter(file))
{
sw.WriteLine("Heartbeat timer triggered at " + DateTime.Now);
}
}
public static void ReadWrite(
[File(@"input.txt", FileAccess.Read, FileMode.OpenOrCreate)] Stream input,
[File(@"output.txt", FileAccess.Write, FileMode.Append)] Stream output)
{
input.CopyTo(output);
}
}
}