Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FTP with Clojure

Tags:

clojure

ftp

Are there any libraries to perform FTP transfer with clojure, idiomatic to clojure, or is it necessary to use a java library such as apache commons?

Thanks

like image 692
kostas Avatar asked Apr 07 '12 07:04

kostas


3 Answers

It is not necessary to use java library and you can roll complete FTP implementation in Clojure but that would be like re-inventing the wheel and not a feasible thing to do. What you can do is probably write a more functional wrapper over the Java library and then use that wrapper in your clojure code so that everything seems seamless and that't how many of the existing Java libraries are being used in Clojure.

like image 107
Ankur Avatar answered Nov 02 '22 01:11

Ankur


You can use https://github.com/miner/clj-ftp either by invoking few convenience functions or by opening a client and invoking multiple commands with it.

The full API is documented in GitHub at https://github.com/miner/clj-ftp/blob/master/src/miner/ftp.clj .

Contents of project.clj

(defproject my-sweet-project "0.5.0"
  :dependencies [[com.velisco/clj-ftp "0.3.0"]
                 ; Other deps
                 ]
  ; ...
)

Invoking a single FTP command

This will open new FTP connection for each command so it should be used for invoking a single command only. See the full API for complete list of these convenience functions.

(ns my-sweet-name.space
  (:require [miner.ftp :as ftp]))

(defn list-files-from-ftp-server []
  "Here we list contents of a directory with a convenience function"
  (let [ftp-url "ftp://username:[email protected]:port/path/to/stuff"]
    (ftp/list-files ftp-url)))

Invoking multiple commands with same connection

This will open FTP connection and invoke arbitrary amount of commands with it. This should be used when multiple commands should be invoked. The FTP connection will be automatically closed. Again check the full API for complete list of functions.

(ns my-sweet-name.space
  (:require [miner.ftp :as ftp]))

(defn list-and-download-files []
  "Here we list and download contents of a directory"
  (let [ftp-url "ftp://username:[email protected]:port/path/to/stuff"]
    (ftp/with-ftp [ftp-client ftp-url]
      ; client-file-names is used to list contents of the ftp-url
      ; client-get is used to download a file
      (doseq [file-name (ftp/client-file-names ftp-client)]
        (let [local-file-name (str "/download-path/" file-name)]
          (ftp/client-get ftp-client file-name local-file-name))))))
like image 2
naga Avatar answered Nov 02 '22 02:11

naga


https://github.com/miner/clj-ftp is a wrapper over Apache Commons Net.

like image 1
miner49r Avatar answered Nov 02 '22 03:11

miner49r