簡體   English   中英

使用功能app將文件從Sftp移到Azure容器

[英]Move the file from Sftp to the Azure container using function app

我正在編寫一個C#程序,將文件從Sftp移到Azure容器。 使用Visual Studio,我可以將文件從sftp移到容器。 我想通過Azure功能應用程序做到這一點。 下面是我在Visual Studio中編寫的代碼。 在azure函數應用程序中,我遇到許多錯誤,包括找不到參考。 有人可以建議我如何在azure函數應用中使此代碼生效嗎?

using System;
using System.Threading;
using System.IO;
using System.Configuration;
using Renci.SshNet;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Auth;
using Newtonsoft.Json;
using System.Linq;
using Microsoft.Win32;
using System.Xml;
using System.Collections.Generic;


public class CloudStorageAccount 
{
const string host = "";
const string username = "";
const string password = "";
const string workingdirectory = "/home/Inbound/";
const int port = 22;
//Connection to the sftp

const string StorageAccountName = "";
const string StorageAccountKey = "";

StorageCredentials storageCredentials = new 
StorageCredentials(StorageAccountName, StorageAccountKey);
CloudStorageAccount cloudStorageAccount = new 
CloudStorageAccount(storageCredentials, useHttps: true);
CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("sample");

var blobs = container.ListBlobs();


 using (var client = new SftpClient(host, port, username, password))
        {
            client.Connect();
            client.ChangeDirectory(workingdirectory);
            var listDirectory = client.ListDirectory(workingdirectory);

             foreach (var fi in listDirectory)
            {

                if (fi.Name.Contains(".txt"))
                {
                    string remoteFileName = fi.Name;
                    using (StreamReader file1 = new 
 StreamReader(client.OpenRead(source + remoteFileName)))

                    {
                        String oldContent = file1.ReadToEnd();

                    CloudBlockBlob blob = container.GetBlockBlobReference(remoteFileName);

                    blob.UploadText(oldContent);

                    }
                }
            }
        }
}

謝謝

您需要確保通過project.json包括所有非框架依賴項( Rensi.SshNetNewtonsoft.Json )。 請參閱github上的相關問題 您可能需要通過#r "System.Linq"樣式調用包括框架依賴關系(以System開頭的依賴關系)。

在此之前,您可以參考Azure函數簡介以了解有關Azure函數的更多信息。

我們還可以使用VS創建Azure功能應用程序 您可以按照本教程進行操作。

在azure函數應用程序中,我遇到許多錯誤,包括找不到參考。

如果可能的話,異常的屏幕快照會更有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM