简体   繁体   中英

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM