当我尝试在Lambda函数中使用Cognitect的aws-api从S3检索文件时,遇到了意料之外的缓慢操作。下面的示例代码用于获取每个相关操作的耗时。获取客户端(aws/client {:api :s3})
总是需要超过4秒钟。作为比较,相同的代码在本地开发环境中执行,访问同一S3存储桶/键,没有出现同样的明显延迟。
需要注意的是,这个函数所在的命名空间使用了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
Λàmá输出
-----> 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"]