SFTP文件锁定机制 [英] SFTP file lock mechanism

查看:242
本文介绍了SFTP文件锁定机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确保在传输过程中通过SFTP(在Linux基本系统中)上传的文件保持锁定状态,从而使自动化系统无法读取该文件?

How can I make sure that a file uploaded through SFTP (in a Linux base system) stays locked during the transfer so an automated system will not read it?

客户端上有选项吗?还是服务器端?

Is there an option on the client side? Or server side?

推荐答案

SFTP协议自版本5开始支持锁定.请参见

SFTP protocol supports locking since version 5. See the specification.

您未指定正在使用什么SFTP服务器.因此,我假设使用最广泛的OpenSSH. OpenSSH仅支持SFTP版本3,因此不支持锁定.

You didn't specify, what SFTP server are you using. So I'm assuming the most widespread one, the OpenSSH. The OpenSSH supports SFTP version 3 only, so it does not support locking.

无论如何,即使您的服务器支持文件锁定,大多数SFTP客户端/库也不支持SFTP版本5.或者即使它们支持,它们也不支持锁定功能.请注意,该锁是显式的,客户端必须请求它.

Anyway, even if your server supported file locking, most SFTP clients/libraries won't support SFTP version 5. Or even if they do, they won't support the locking feature. Note that the lock is explicit, the client has to request it.

有一些常见的解决方法:

There are some common workarounds for the problem:

  • As suggested by @user1717259, you can have the client upload a "done" file, once an upload finishes. Make your automated system wait for the "done" file to appear.
  • You can have a dedicated "upload" folder and have the client (atomically) move the uploaded file to a "done" folder. Make your automated system look to the "done" folder only.
  • Have a file naming convention for files being uploaded (".filepart") and have the client (atomically) rename the file after an upload to its final name. Make your automated system ignore the ".filepart" files.
    See (my) article Locking files while uploading / Upload to temporary file name for example of implementing this approach.
  • A gross hack is to periodically check for file attributes (size and time) and consider the upload finished, if the attributes have not changed for some time interval.
  • You can also make use of the fact that some file formats have clear end-of-the-file marker (like XML or ZIP). So you know, when you download an incomplete file.

这篇关于SFTP文件锁定机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆