This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-09-19
Channels
- # announcements (1)
- # babashka (40)
- # beginners (84)
- # biff (46)
- # calva (37)
- # cherry (2)
- # cider (18)
- # clj-otel (5)
- # clojure (53)
- # clojure-europe (39)
- # clojure-hungary (12)
- # clojure-norway (40)
- # clojure-sweden (2)
- # clojure-uk (2)
- # clojurescript (6)
- # community-development (21)
- # cursive (28)
- # data-science (12)
- # datomic (3)
- # figwheel-main (2)
- # fulcro (12)
- # graalvm (7)
- # gratitude (1)
- # hyperfiddle (23)
- # integrant (9)
- # jobs (2)
- # leiningen (4)
- # lsp (8)
- # malli (3)
- # missionary (1)
- # off-topic (39)
- # polylith (3)
- # portal (33)
- # practicalli (4)
- # re-frame (3)
- # releases (1)
- # sci (53)
- # solo-full-stack (8)
- # sql (5)
- # timbre (9)
kesobbi referencianak, itt egy pelda arra h h kell hasznalni a https://github.com/awslabs/aws-java-nio-spi-for-s3 file system-et.
deps.edn
-be ez megy:
software.amazon.nio.s3/aws-java-nio-spi-for-s3 {:mvn/version "1.2.3"}}
aztan meg egy s3_repl.clj
-be ez:
(ns s3-repl
(:import ( URI)
(java.nio.file Files OpenOption Path StandardOpenOption)
(java.time LocalDateTime)))
(def CREATE_OPTS
[StandardOpenOption/WRITE
StandardOpenOption/CREATE])
(def TRUNCATE_OPTS
(into CREATE_OPTS
[StandardOpenOption/TRUNCATE_EXISTING]))
(def APPEND_OPTS
(into CREATE_OPTS
[StandardOpenOption/APPEND]))
(def READ_OPTS
[StandardOpenOption/READ])
(defn ls [^Path path] (-> path Files/list .iterator iterator-seq))
(def bucket "<some-bucket-name>")
(def bucket-root (-> (str "s3://" bucket) URI. Path/of))
(def object-path (-> bucket-root (.resolve "xxx.txt")))
(comment
(-> bucket-root ls)
;; Write
(-> object-path
(Files/newOutputStream (into-array TRUNCATE_OPTS #_APPEND_OPTS))
(spit (-> "[%s] Created with `clojure.core/spit`\n"
(format (LocalDateTime/now)))))
;; Read
(-> object-path
(Files/newInputStream (into-array READ_OPTS))
(slurp))
)
a https://docs.oracle.com/javase/tutorial/essential/io/fileio.html -t olvastam vegig, meg beleneztem par Clojure nio wrapper lib-be amikbol az into-array
temat tanultam, h varargs-os metodus hivashoz az kell.
ezek utan mar ossze birtam rakni ezt a peldat.
bar azt nem tudom h mennyire hatekony ez igy, mert a slurp
/`spit` az a regi java.io.file
package-en alapszik.
meg az a gyanum, h spit/slurp-el csak szoveget tudok irni/olvasni. binaris adatot nem. pedig ugy remlik h csinaltam mar ilyet. valszeg kell valami reader v stream a byte[] kore...
(let [f "/tmp/asd.txt"]
(spit f (-> "qwe\n" (.getBytes "UTF-8")))
(slurp f))
=> "[B@3b412bd2"
it az ideje elolvasni ezt is vegre: https://docs.oracle.com/javase/tutorial/essential/io/streams.html
Bár nem tudom itt lehet-e, illetve nippy-vel talán tudsz binárist írni és olvasni.. de lehet nem erre gondolsz
itt egy pelda babashka.fs
hasznalatra is, ami szinten java.nio.file.Path
-ra epul:
(require '[babashka.fs :as fs])
(def test-path (fs/path (URI. "s3://<some-bucket>/nio-test.txt")))
(def test-content "test \uD83D\uDE0E\n")
(def utf8 "UTF-8")
(def default-write-opts {:create true :truncate-existing true :write true})
(-> test-path (fs/write-bytes (.getBytes test-content utf8) default-write-opts))
(-> test-path (fs/read-all-bytes) (String. utf8))
a peldaban az is lathato, h ha egy string-ben van a teljes path, akkor a Path/of
single arity-s valtozatat is tudjuk hasznalni, ami URI-t var (`fs/path` ~= Path/of
)
az ArrayList
az nem jo vararg-nak:
(Path/of "asd" (java.util.ArrayList.))
Execution error (ClassCastException) at repl.s3/eval1026 (s3.clj:80).
class java.util.ArrayList cannot be cast to class [Ljava.lang.String; (java.util.ArrayList and [Ljava.lang.String; are in module java.base of loader 'bootstrap')
make-array
viszont jo arra az esetre ha ures a varargs:
(Path/of "asd" (make-array String 0))
=> #object[sun.nio.fs.UnixPath 0x6e9127a7 "asd"]