如何在没有任何用户交互的情况下对服务器进行文件传输?

时间:2021-10-09 05:54:56

In my scenario, users are able to upload zip files to a.example.com

在我的场景中,用户可以将zip文件上传到a.example.com

I would love to create a "daemon" which in specified time intervals will move-transfer any zip files uploaded by the users from a.example.com to b.example.com

我很乐意创建一个“守护进程”,它会在指定的时间间隔内移动 - 将用户上传的任何zip文件从a.example.com传输到b.example.com

From the info i gathered so far,

从我目前收集的信息,

  1. The daemon will be an .ashx generic handler.
  2. 守护进程将是.ashx通用处理程序。
  3. The daemon will be triggered at the specified time intervals via a plesk cron job
  4. 将通过plesk cron作业以指定的时间间隔触发守护程序
  5. The daemon (thanks to SLaks) will consist of two FtpWebRequest's (One for reading and one for writing).
  6. 守护进程(感谢SLaks)将包含两个FtpWebRequest(一个用于读取,一个用于写入)。

So the question is how could i implement step 3?

所以问题是如何实施第3步?

  • Do i have to read into to a memory() array the whole file and try to write that in b.example.com ?
  • 我是否必须将整个文件读入memory()数组并尝试在b.example.com中编写?
  • How could i write the info i read to b.example.com?
  • 我怎么能把我读到的信息写到b.example.com?
  • Could i perform reading and writing of the file at the same time?
  • 我可以同时执行文件的读写吗?

No i am not asking for the full code, i just can figure out, how could i perform reading and writing on the fly, without user interaction.

不,我不是要求完整的代码,我只能弄明白,我怎么能在没有用户交互的情况下即时执行读写。

I mean i could download the file locally from a.example.com and upload it at b.example.com but that is not the point.

我的意思是我可以从a.example.com本地下载文件并将其上传到b.example.com,但这不是重点。

8 个解决方案

#1


5  

To answer your questions - yes you can read and write the files at the same time.

回答你的问题 - 是的,你可以同时读写文件。

You can open an FTPWebRequest to ServerA and a FTPWebRequest to ServerB. On the FTPWebRequest to serverA you would request the file, and get the ResponseStream. Once you have the ResponseStream, you would read a chunk of bytes at a time, and write that chunck of bytes to the serverB RequestStream.

您可以打开到ServerA的FTPWebRequest和ServerB的FTPWebRequest。在FTPARequest到serverA上,您将请求该文件,并获取ResponseStream。获得ResponseStream之后,您将一次读取一块字节,并将该字节块写入serverB RequestStream。

The only memory you would be using would be the byte[] buffer in your read/write loop. Just keep in mind though that the underlying implementation of FTPWebRequest will download the complete FTP file before returning the response stream.

您将使用的唯一内存是读/写循环中的byte []缓冲区。请记住,FTPWebRequest的底层实现将在返回响应流之前下载完整的FTP文件。

Similarly, you cannot send your FTPWebRequest to upload the new file until all bytes have been written. In effect, the operations will happen synchronously. You will call GetResponse which won't return until the full file is available, and only then can you 'upload' the new file.

同样,在写入所有字节之前,您无法发送FTPWebRequest来上载新文件。实际上,操作将同步进行。您将调用GetResponse,它将在完整文件可用之前不会返回,然后才能“上传”新文件。

References:

参考文献:

FTPWebRequest

的FtpWebRequest

#2


8  

Here is another solution:

这是另一个解决方案:

  1. Let ASP.Net in server A receive the file as a regular file upload and store it in directory XXX
  2. 让服务器A中的ASP.Net作为常规文件上载接收文件并将其存储在目录XXX中
  3. Have a windows service in server A that checks directory XXX for new files.
  4. 在服务器A中有一个Windows服务,它检查目录XXX是否有新文件。
  5. Let the window service upload the file to server B using HttpWebRequest
  6. 让窗口服务使用HttpWebRequest将文件上传到服务器B.
  7. Let server B receive the file using a regular ASP.Net file upload page.
  8. 让服务器B使用常规的ASP.Net文件上传页面接收文件。

Links:

链接:

Problems you gotto solve:

你需要解决的问题:

  1. How to determine which files to upload to server B. I would use Directory.GetFiles in a Timer to find new files instead of using a FileSystemWatcher. You need to be able to check if a file have been uploaded previously (delete it, rename it, check DB or whatever suits your needs).

    如何确定要上传到服务器B的文件。我会在Timer中使用Directory.GetFiles来查找新文件而不是使用FileSystemWatcher。您需要能够检查先前是否已上载文件(删除,重命名,检查数据库或任何适合您需要的文件)。

  2. Authentication on server B, so that only you can upload files to it.

    在服务器B上进行身份验证,以便只有您可以上传文件。

#3


3  

Something you have to take into consideration is that a long running web requests (your .ashx generic handler) may be killed when the AppDomain refreshes. Therefore you have to implement some sort of atomic transaction logic in your code, and you should handle sudden disconnects and incomplete FTP transfers if you go that way.

您需要考虑的是,当AppDomain刷新时,可能会终止长时间运行的Web请求(您的.ashx通用处理程序)。因此,您必须在代码中实现某种原子事务逻辑,如果您采用这种方式,则应该处理突然断开连接和不完整的FTP传输。

Did you have a look at Windows Azure before? This cloud platform supports distributed file system, and has built-in atomic transactions. Plus it scales nicely, should your service grow fast.

您之前看过Windows Azure吗?该云平台支持分布式文件系统,并具有内置的原子事务。如果您的服务快速增长,它可以很好地扩展。

#4


2  

I would make it pretty simple. The client program uploads the file to server A. This can be done very easily in C# with an FtpWebRequest.

我会让它变得非常简单。客户端程序将文件上传到服务器A.这可以通过FtpWebRequest在C#中轻松完成。

http://msdn.microsoft.com/en-us/library/ms229715.aspx

http://msdn.microsoft.com/en-us/library/ms229715.aspx

I would then have a service on server A that monitors the directory where files are uploaded. When a file is uploaded to that directory or on certain intervals it simply copies files over to server B. Again this can be done via Ftp or other means if they're on the same network.

然后我会在服务器A上有一个服务,它监视文件上传的目录。当文件上传到该目录或在某个时间间隔时,它只是将文件复制到服务器B.如果它们在同一网络上,这可以通过Ftp或其他方式完成。

#5


1  

you need some listener on the target domain, ftp server running there, and on the client side you will use System.Net.WebClient and UploadFile or UploadFileAsync to send the file. is that what you are asking?

你需要在目标域上运行一些监听器,在那里运行ftp服务器,在客户端你将使用System.Net.WebClient和UploadFile或UploadFileAsync来发送文件。你问的是什么?

#6


1  

It sounds like you don't really need a webservice or handler. All you need is a program that will, at regular intervals, open up an FTP connection to the other server and move the files. This can be done by any .NET program with the System.WebClient library, doesn't have to be a "web app". This other program could be a service, which could handle its own timing, or a simple app run by your cron job. If you need this to go two ways, for instance if the two servers are mirrors, you simply have the same app on the second box doing the same thing to upload files over to the first.

听起来你真的不需要webservice或处理程序。您所需要的只是一个程序,它将定期打开与其他服务器的FTP连接并移动文件。这可以通过任何带有System.WebClient库的.NET程序来完成,而不必是“Web应用程序”。这个其他程序可以是一个服务,可以处理自己的时间,或由您的cron作业运行的简单应用程序。如果您需要两种方式,例如,如果两台服务器都是镜像,那么您只需在第二个框上使用相同的应用程序即可将文件上传到第一个框。

#7


1  

If both machines are in the same domain, couldn't you just do file replication at the OS level? DFS

如果两台计算机都在同一个域中,那么您是否只能在操作系统级别进行文件复制? DFS

#8


1  

set up keys if you are using linux based systems:

如果您使用的是基于Linux的系统,请设置密钥:

http://compdottech.blogspot.com/2007/10/unix-login-without-password-setting.html

http://compdottech.blogspot.com/2007/10/unix-login-without-password-setting.html

Once you have the keys working, you can copy the file from system A to system B by writing regular shell scripts that would not need any user interactions.

一旦密钥正常工作,您就可以通过编写不需要任何用户交互的常规shell脚本将文件从系统A复制到系统B.

#1


5  

To answer your questions - yes you can read and write the files at the same time.

回答你的问题 - 是的,你可以同时读写文件。

You can open an FTPWebRequest to ServerA and a FTPWebRequest to ServerB. On the FTPWebRequest to serverA you would request the file, and get the ResponseStream. Once you have the ResponseStream, you would read a chunk of bytes at a time, and write that chunck of bytes to the serverB RequestStream.

您可以打开到ServerA的FTPWebRequest和ServerB的FTPWebRequest。在FTPARequest到serverA上,您将请求该文件,并获取ResponseStream。获得ResponseStream之后,您将一次读取一块字节,并将该字节块写入serverB RequestStream。

The only memory you would be using would be the byte[] buffer in your read/write loop. Just keep in mind though that the underlying implementation of FTPWebRequest will download the complete FTP file before returning the response stream.

您将使用的唯一内存是读/写循环中的byte []缓冲区。请记住,FTPWebRequest的底层实现将在返回响应流之前下载完整的FTP文件。

Similarly, you cannot send your FTPWebRequest to upload the new file until all bytes have been written. In effect, the operations will happen synchronously. You will call GetResponse which won't return until the full file is available, and only then can you 'upload' the new file.

同样,在写入所有字节之前,您无法发送FTPWebRequest来上载新文件。实际上,操作将同步进行。您将调用GetResponse,它将在完整文件可用之前不会返回,然后才能“上传”新文件。

References:

参考文献:

FTPWebRequest

的FtpWebRequest

#2


8  

Here is another solution:

这是另一个解决方案:

  1. Let ASP.Net in server A receive the file as a regular file upload and store it in directory XXX
  2. 让服务器A中的ASP.Net作为常规文件上载接收文件并将其存储在目录XXX中
  3. Have a windows service in server A that checks directory XXX for new files.
  4. 在服务器A中有一个Windows服务,它检查目录XXX是否有新文件。
  5. Let the window service upload the file to server B using HttpWebRequest
  6. 让窗口服务使用HttpWebRequest将文件上传到服务器B.
  7. Let server B receive the file using a regular ASP.Net file upload page.
  8. 让服务器B使用常规的ASP.Net文件上传页面接收文件。

Links:

链接:

Problems you gotto solve:

你需要解决的问题:

  1. How to determine which files to upload to server B. I would use Directory.GetFiles in a Timer to find new files instead of using a FileSystemWatcher. You need to be able to check if a file have been uploaded previously (delete it, rename it, check DB or whatever suits your needs).

    如何确定要上传到服务器B的文件。我会在Timer中使用Directory.GetFiles来查找新文件而不是使用FileSystemWatcher。您需要能够检查先前是否已上载文件(删除,重命名,检查数据库或任何适合您需要的文件)。

  2. Authentication on server B, so that only you can upload files to it.

    在服务器B上进行身份验证,以便只有您可以上传文件。

#3


3  

Something you have to take into consideration is that a long running web requests (your .ashx generic handler) may be killed when the AppDomain refreshes. Therefore you have to implement some sort of atomic transaction logic in your code, and you should handle sudden disconnects and incomplete FTP transfers if you go that way.

您需要考虑的是,当AppDomain刷新时,可能会终止长时间运行的Web请求(您的.ashx通用处理程序)。因此,您必须在代码中实现某种原子事务逻辑,如果您采用这种方式,则应该处理突然断开连接和不完整的FTP传输。

Did you have a look at Windows Azure before? This cloud platform supports distributed file system, and has built-in atomic transactions. Plus it scales nicely, should your service grow fast.

您之前看过Windows Azure吗?该云平台支持分布式文件系统,并具有内置的原子事务。如果您的服务快速增长,它可以很好地扩展。

#4


2  

I would make it pretty simple. The client program uploads the file to server A. This can be done very easily in C# with an FtpWebRequest.

我会让它变得非常简单。客户端程序将文件上传到服务器A.这可以通过FtpWebRequest在C#中轻松完成。

http://msdn.microsoft.com/en-us/library/ms229715.aspx

http://msdn.microsoft.com/en-us/library/ms229715.aspx

I would then have a service on server A that monitors the directory where files are uploaded. When a file is uploaded to that directory or on certain intervals it simply copies files over to server B. Again this can be done via Ftp or other means if they're on the same network.

然后我会在服务器A上有一个服务,它监视文件上传的目录。当文件上传到该目录或在某个时间间隔时,它只是将文件复制到服务器B.如果它们在同一网络上,这可以通过Ftp或其他方式完成。

#5


1  

you need some listener on the target domain, ftp server running there, and on the client side you will use System.Net.WebClient and UploadFile or UploadFileAsync to send the file. is that what you are asking?

你需要在目标域上运行一些监听器,在那里运行ftp服务器,在客户端你将使用System.Net.WebClient和UploadFile或UploadFileAsync来发送文件。你问的是什么?

#6


1  

It sounds like you don't really need a webservice or handler. All you need is a program that will, at regular intervals, open up an FTP connection to the other server and move the files. This can be done by any .NET program with the System.WebClient library, doesn't have to be a "web app". This other program could be a service, which could handle its own timing, or a simple app run by your cron job. If you need this to go two ways, for instance if the two servers are mirrors, you simply have the same app on the second box doing the same thing to upload files over to the first.

听起来你真的不需要webservice或处理程序。您所需要的只是一个程序,它将定期打开与其他服务器的FTP连接并移动文件。这可以通过任何带有System.WebClient库的.NET程序来完成,而不必是“Web应用程序”。这个其他程序可以是一个服务,可以处理自己的时间,或由您的cron作业运行的简单应用程序。如果您需要两种方式,例如,如果两台服务器都是镜像,那么您只需在第二个框上使用相同的应用程序即可将文件上传到第一个框。

#7


1  

If both machines are in the same domain, couldn't you just do file replication at the OS level? DFS

如果两台计算机都在同一个域中,那么您是否只能在操作系统级别进行文件复制? DFS

#8


1  

set up keys if you are using linux based systems:

如果您使用的是基于Linux的系统,请设置密钥:

http://compdottech.blogspot.com/2007/10/unix-login-without-password-setting.html

http://compdottech.blogspot.com/2007/10/unix-login-without-password-setting.html

Once you have the keys working, you can copy the file from system A to system B by writing regular shell scripts that would not need any user interactions.

一旦密钥正常工作,您就可以通过编写不需要任何用户交互的常规shell脚本将文件从系统A复制到系统B.