This leads to the downloading of that file into the local directory of our client-side local machine. There are cases when we have to download all the files in a directory with the given extension type. In the above code snippet we have moved into the specific directory using the conn. We then got the list of files available in that directory using the conn.
Following that, we iterated each file and checked if it was a png file that was downloaded using the conn. In this tutorial, we have learned how to download files from an STFP server using the pysftp package in Python. To begin with, we tried to establish the connection with the server. One of the core elements your code is missing is object-orientation. Take advantage of Ruby's powerful object model.
That alone will vastly improve what you have. What we're going to do is progressively breakdown everything into chunks of logic and encapsulate these chunks of logic in objects and methods. Before that, let's go through each line and give feedback if anything seems a little off..
It seems you've defined quite a few constants for things which, by nature, will change on a user-by-user basis. They are not constants, they are variables, and so it would be better to use variables. However, if you do replace them with variables, you will not see any real benefit because the code is largely a procedural script. You will see the benefit of using variables instead once the arguments and execution of the script are separated.
You almost never need to initialize variables like this in Ruby. Look into inject for an alternative. This, along with the empty collections you defined above, give the impression that something is wrong. We are storing files and file details in separate arrays. There appears to be a focus on primitives, where we should instead be thinking about how we can encapsulate these in objects. There is nothing wrong with using arrays, but we should think about another approach when it becomes obvious that simple primitives are not providing the functionality we need in a clean way.
We have littered about puts statements as a way to log the execution of the script. Perhaps we later want to log this to a file instead? That would be a good enough reason to extract it into its own object, and, if we want, later change the method of how we log.
Another common mistake, though easily fixed, is the magic number Along with magic numbers, we have magic indexes being used everywhere. You yourself can see this is difficult to follow and so have created a comment to remind you what you are dealing with.
Again, this should be hidden behind a descriptive variable or method. I haven't played around with this so unsure as to why, but lets assume this something we can also include in our logging object. On a side-note - we have lots of comments in this code. As you begin to hide details behind descriptive names, you will see that the value in comments begins to diminish.
If you have to litter comments everywhere then perhaps your code isn't readable enough? Now let's start refactoring. With all that in mind, where do we begin? I can show you the end result, but that isn't useful in learning how to approach this in the future. Use get command to download file from sftp server to local system drive. Use lcd to change location of local download folder.
Below command will download remotefile. To download files and folders recursively use -r switch with get command. Below command will download folder remotedir from remote system to local system recursively. Use put command to upload a file from local system to remote system. Thankfully, the community has made modules for everyone to use freely to make this job much more manageable. Why not write on a platform with an existing audience and share your knowledge with the world?
Adam the Automator. Twitter Facebook LinkedIn. Table of Contents.
0コメント