Move the file from Sftp to the Azure container using function app

北城余情 提交于 2020-01-06 05:08:11

问题


I was writing a C# program to move the file from Sftp to the Azure container. Using visual studio I'm able to move the file from sftp to container. I wanted to make it through Azure function app. Below is the code i have written in visual studio. In azure function app i'm getting many error including reference not found. Could any one advise me how to make work this code in azure function app?

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);

                    }
                }
            }
        }
}

Thanks


回答1:


You need to make sure all your non-framework dependencies (Rensi.SshNet, Newtonsoft.Json) are included via project.json. See this related issue on github. And you may need to include framework dependencies (the ones starting with System) via #r "System.Linq" style calls.




回答2:


Before do that you could refer to an introduction to Azure Functions to learn more about Azure function.

We also could create the Azure function Application with VS. You could follow this tutorial to do that.

In azure function app i'm getting many error including reference not found.

If possible, a screenshot of exception will be more helpful.



来源:https://stackoverflow.com/questions/49599311/move-the-file-from-sftp-to-the-azure-container-using-function-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!