当尝试使用 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"]