0

I'm trying to FTP a file from Azure up to some FTP destination.

When I do it locally, it's working great. Ok ...

I publish this webjob up to azure and it's works great until it tries to do the FTP bit.

The following error is shown:

[12/21/2015 03:18:03 > 944464: INFO] 2015-12-21 03:18:03.6127| ERROR| Unable to connect to the remote server. Stack Trace: at System.Net.WebClient.UploadFile(Uri address, String method, String fileName)

[12/21/2015 03:18:03 > 944464: INFO] at System.Net.WebClient.UploadFile(String address, String method, String fileName)

And this is my simple code...

public void UploadFile(FileInfo fileInfo)
{
    try
    {
        using (var client = new WebClient())
        {
            var destination = new Uri($"ftp://{_server}/{fileInfo.Name}");
            client.Credentials = new NetworkCredential(_username, _password);
            client.UploadFile(destination, "STOR", fileInfo.FullName);
        }
    }
    catch (Exception exception)
    {
        // snipped.
        throw;
    }
}

So it's pretty damn simple.

Could this be some weird PASSIVE/ACTIVE issue? Like, how our office internet is simple while azure's setup is complex and ... as such ... it just cannot create the FTP connection?

Now - this is where things also get frustrating. Originally I was using a 3rd party FTP library and that DID work .. but some files were only getting partially uploaded .. which is why i'm trying to use WebClient instead. So I know that my azure webjob "website" thingy can FTP up. (no blocked ports, etc).

Any clues to what I can do, here?

Community
  • 1
  • 1
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • Could it be that the FTP server is blocking some IP range? – Matias Quaranta Dec 21 '15 at 03:42
  • Nope - because in my last part of the opening post (above), I have used a 3rd party FTP package and that was connecting ok .. it just wasn't always uploading the file 100%. Which is why I'm trying another way... – Pure.Krome Dec 21 '15 at 03:44
  • Yeah, I read that part :) But sometimes Web Apps get started / stopped or redeployed and change the outbound IP address. – Matias Quaranta Dec 21 '15 at 03:45
  • It never happened with the 3rd party FTP package i was using and it was uploading a fair number of files running for a while too. And I did a number of 'publishes' (which could have changed IP's). - No connection errors from what I could see/remember. – Pure.Krome Dec 21 '15 at 03:48
  • NOTE: I was hoping it might be as simple as an IP block. I don't think it is :( – Pure.Krome Dec 21 '15 at 03:49

1 Answers1

0

Following Kristensen's simple example, I could do it. But I'm not sure about the format of your FTP url destination.

Did you try:

var destination = new Uri(string.Format("ftp://{0}/{1}", _server,fileInfo.Name));

Assuming _server is defined somewhere, I don't see it in your code and that your credentials have permissions to write on that location.

Matias Quaranta
  • 13,907
  • 1
  • 22
  • 47
  • FTP url is c#6 sytanx. `_server` is private but global to the class. My cred's have permission, because when I run that same code locally, it works. – Pure.Krome Dec 21 '15 at 04:40
  • It's weird. Every single content I found regarding the issue always points to some Firewall or port blocking, specially if the FTP server is "inside". There is one post I found that mentions settings the FTP Upload to Active: http://stackoverflow.com/questions/14708205/powershell-need-to-create-an-active-ftp-transfer-connection-from-a-ps-script. – Matias Quaranta Dec 22 '15 at 12:10
  • that's the path i'm going down. I've been thinking: you've hit our FTP server too many times in a 'short' period, so .. we're going to block your IP for a short while. Why do i think this? well, a day later, I successfully uploaded some files _without changing any code_ .. come back 20 mins later (after a few more successful uploads every 2 mins..) and I've found out that I suddenly cannot access the server. Code is good -> looks like a server issue :/ – Pure.Krome Dec 22 '15 at 21:37
  • You could implement some kind of Queue to avoid the upload burst. Maybe use an Azure Queue / Azure Blob Storage solution to enqueue messages and a WebJob consuming the Queue periodically to make the uploads as spaced out as the server requires. – Matias Quaranta Dec 22 '15 at 23:11