尝试使用 Cognitect 的 aws-api 在 Lambda 函数中从 S3 获取文件时,我遇到了预期之外的缓慢行为。以下样本代码用于获取每个相关操作的总体耗时。获取客户端 (aws/client {:api :s3})
始终需要超过 4 秒。作为比较,同样代码在我的本地开发环境中访问同一个 S3 桶/键时,执行没有这种明显的延迟,就像它在 Lambda 中执行时一样。
应注意的是,这个函数所在的命名空间利用了 gen-class 功能。
(defn read-s3-file
[bucket obj-key]
(let [_ (println (str "-----> Before declaring s3 client: " (java.time.LocalDateTime/now)))
s3 (aws/client {:api :s3})
_ (println (str "-----> After declaring s3 client: " (java.time.LocalDateTime/now)))
_ (println (str "---> bucket: " bucket ", key " obj-key))
body (:Body (aws/invoke s3 {:op :GetObject :request {:Bucket bucket :Key obj-key}}))
_ (println (str "-----> After GetObject: " (java.time.LocalDateTime/now)))
]
(println (str "-----> Before calling slurp: " (java.time.LocalDateTime/now)))
(slurp body)
)
)
这是否是由 Lambda 所需的 Java8 运行时引起的?
本地输出
clj-lead-updater.core=> (clj-lead-updater.s3/read-s3-file "automattix-incoming-emails" "tdf10001AnsSvc/0dqsmtvh5mquq3tdhpq550lnqcp6162f0k29da01")
-----> Before declaring s3 client: 2019-11-18T18:52:27.611925
-----> After declaring s3 client: 2019-11-18T18:52:27.638097
---> bucket: automattix-incoming-emails, key tdf10001AnsSvc/0dqsmtvh5mquq3tdhpq550lnqcp6162f0k29da01
-----> After GetObject: 2019-11-18T18:52:28.096044
-----> Before calling slurp: 2019-11-18T18:52:28.096431
相关的两个度量是
-----> Before declaring s3 client: 2019-11-18T18:52:27.611925
-----> After declaring s3 client: 2019-11-18T18:52:27.638097
Lambda 输出
-----> Before turning input into a map: 2019-11-17T02:27:08.705
-----> Before declaring s3 client: 2019-11-17T02:27:08.781
2019-11-17 02:27:10.641:INFO::main: Logging initialized @8431ms to org.eclipse.jetty.util.log.StdErrLog
-----> After declaring s3 client: 2019-11-17T02:27:12.923
-----> After GetObject: 2019-11-17T02:27:14.446
-----> Before calling slurp: 2019-11-17T02:27:14.446
再次,相关的两个度量是
-----> Before declaring s3 client: 2019-11-17T02:27:08.781
-----> After declaring s3 client: 2019-11-17T02:27:12.923
项目中相关的依赖
[org.clojure/clojure "1.10.1"]
[com.cognitect.aws/api "0.8.391"]
[com.cognitect.aws/endpoints "1.1.11.670"]
[com.cognitect.aws/s3 "770.2.568.0"]