如何使用 Azure WebJob 将文件上传到 Azure Blob Storage? (使用 C#、.NET)
How to upload files to Azure Blob Storage using Azure WebJob? (Using C#, .NET)
我对Azure WebJob有一个非常基本的了解,就是它可以在后台执行任务。我想将文件上传到 Azure Blob 存储,特别是使用 Azure WebJob。我想知道如何从头开始。假设要上传的文件在系统的某个文件夹中本地可用(比如 C:/Users/Abc/Alpha/Beta/)。
- 如何以及在何处定义应该执行的后台任务?
- 如何确保每当同一文件夹中有新文件时 (
C:/Users/Abc/Alpha/Beta/) 函数自动触发,这个新文件也被传输到Azure Blob Storage?
- 我可以监控每个文件的传输进度吗?还是所有文件?
- 传输过程中连接失败如何处理?我还应该担心哪些其他错误?
How and where do I define the background task that is supposed to be performed?
根据您的描述,您可以在 VS.You 中创建一个 webjob 控制台应用程序,也可以 运行 在本地创建此控制台应用程序。
更多详细信息,您可以参考此article以了解如何在 VS 中创建 webjob。
注意:因为你需要看本地文件夹,这个 webjob 运行在你的本地没有上传到 azure web 应用程序。
How to make sure, that whenever a new file is available in the same folder ( C:/Users/Abc/Alpha/Beta/) the function is automatically triggered, and this new file is also transferred to Azure Blob Storage?
据我所知,webjob支持filetrigger,它会监控特定目录下的文件additions/changes,并在它们出现时触发作业功能。
更多细节,您可以参考下面的代码示例:
Program.cs:
static void Main()
{
var config = new JobHostConfiguration();
FilesConfiguration filesConfig = new FilesConfiguration();
//set the root path when the function to watch the folder
filesConfig.RootPath = @"D:\";
config.UseFiles(filesConfig);
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
function.cs:
public static void ImportFile(
[FileTrigger(@"fileupload\{name}", "*.*", WatcherChangeTypes.Created | WatcherChangeTypes.Changed)] Stream file,
FileSystemEventArgs fileTrigger,
[Blob("textblobs/{name}", FileAccess.Write)] Stream blobOutput,
TextWriter log)
{
log.WriteLine(string.Format("Processed input file '{0}'!", fileTrigger.Name));
file.CopyTo(blobOutput);
log.WriteLine("Upload File Complete");
}
Can I monitor progress of transfer for each file? or for all files?
据我所知,有一个 [BlobInput] 属性可让您指定要侦听的容器,它包括一个高效的 blob 侦听器,该侦听器将在检测到新的 blob 时分派给该方法。更详细的可以参考这个article.
How to handle connection failures during transfer? and what other errors should I worry about?
您可以使用 try catch 来捕获 error.If 错误发生时您可以将详细信息发送到队列或在 blob 中写入一个 txt 文件。然后就可以根据队列消息或者blob txt文件做一些操作了。
我对Azure WebJob有一个非常基本的了解,就是它可以在后台执行任务。我想将文件上传到 Azure Blob 存储,特别是使用 Azure WebJob。我想知道如何从头开始。假设要上传的文件在系统的某个文件夹中本地可用(比如 C:/Users/Abc/Alpha/Beta/)。
- 如何以及在何处定义应该执行的后台任务?
- 如何确保每当同一文件夹中有新文件时 ( C:/Users/Abc/Alpha/Beta/) 函数自动触发,这个新文件也被传输到Azure Blob Storage?
- 我可以监控每个文件的传输进度吗?还是所有文件?
- 传输过程中连接失败如何处理?我还应该担心哪些其他错误?
How and where do I define the background task that is supposed to be performed?
根据您的描述,您可以在 VS.You 中创建一个 webjob 控制台应用程序,也可以 运行 在本地创建此控制台应用程序。
更多详细信息,您可以参考此article以了解如何在 VS 中创建 webjob。
注意:因为你需要看本地文件夹,这个 webjob 运行在你的本地没有上传到 azure web 应用程序。
How to make sure, that whenever a new file is available in the same folder ( C:/Users/Abc/Alpha/Beta/) the function is automatically triggered, and this new file is also transferred to Azure Blob Storage?
据我所知,webjob支持filetrigger,它会监控特定目录下的文件additions/changes,并在它们出现时触发作业功能。
更多细节,您可以参考下面的代码示例:
Program.cs:
static void Main()
{
var config = new JobHostConfiguration();
FilesConfiguration filesConfig = new FilesConfiguration();
//set the root path when the function to watch the folder
filesConfig.RootPath = @"D:\";
config.UseFiles(filesConfig);
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
function.cs:
public static void ImportFile(
[FileTrigger(@"fileupload\{name}", "*.*", WatcherChangeTypes.Created | WatcherChangeTypes.Changed)] Stream file,
FileSystemEventArgs fileTrigger,
[Blob("textblobs/{name}", FileAccess.Write)] Stream blobOutput,
TextWriter log)
{
log.WriteLine(string.Format("Processed input file '{0}'!", fileTrigger.Name));
file.CopyTo(blobOutput);
log.WriteLine("Upload File Complete");
}
Can I monitor progress of transfer for each file? or for all files?
据我所知,有一个 [BlobInput] 属性可让您指定要侦听的容器,它包括一个高效的 blob 侦听器,该侦听器将在检测到新的 blob 时分派给该方法。更详细的可以参考这个article.
How to handle connection failures during transfer? and what other errors should I worry about?
您可以使用 try catch 来捕获 error.If 错误发生时您可以将详细信息发送到队列或在 blob 中写入一个 txt 文件。然后就可以根据队列消息或者blob txt文件做一些操作了。