SFTP文件锁定机制 [英] SFTP file lock mechanism
问题描述
如何确保在传输过程中通过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 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:
- 按照@ user1717259的建议,一旦上传完成,您就可以让客户端上传完成"文件.让您的自动化系统等待完成"文件出现.
- 您可以拥有一个专用的上传"文件夹,并使客户端(原子上)将上传的文件移至完成"文件夹.使您的自动化系统仅查看完成"文件夹.
- 对要上传的文件(".filepart")具有文件命名约定,并让客户端(原子上)在上传后将文件重命名为其最终名称.使您的自动化系统忽略".filepart"文件.
请参阅(我的)文章上传时锁定文件/上传到临时文件名,以了解实现此示例方法. - 总的黑客是定期检查文件属性(大小和时间)并考虑上传是否完成,如果该属性在一定时间间隔内未发生变化.
- 您还可以利用某些文件格式具有清晰的文件结尾标记(例如XML或ZIP)的事实.所以您知道,当您下载不完整的文件时.
- 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屋!