Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...