我有一个Java,它正在将一个文件从S3复制到/tmp。这取决于Lambda实例的大小,需要6-10秒。该文件在使用相同Lambda实例的新调用中被重新使用。然而,流量是非常尖利的,我可以看到50+同时发出请求,而来自S3的冷启动副本是不可接受的……
是否有比从S3复制更好的“预加载”数据到Lambda实例的方法?如何通过HTTP请求从Cloudfront获取缓存的副本。当有初始启动数据需求时,还有其他的“技巧”来加速冷启动吗?
回答:是的,,见下文
发布于 2019-03-24 12:22:01
回答:是的,
从S3托管桶中提取CloudFront和使用GZip更快。在1.5MB文件中,我看到性能从12秒提高到了4秒。下面是我为此目的编写的代码:
void getFile(String remoteUrl) throws Exception {
URL url = new URL(remoteUrl);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setRequestProperty("Accept-Encoding", "gzip");
BufferedInputStream bis;
if ("gzip".equals(con.getContentEncoding())) {
bis = new BufferedInputStream(new GZIPInputStream(con.getInputStream()));
}
else {
bis = new BufferedInputStream(con.getInputStream());
}
String getPath = url.getPath();
String fileName = "/tmp/"+getPath.substring(getPath.lastIndexOf('/') + 1);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
byte[] buff = new byte[16 * 1024];
int len;
while ((len = bis.read(buff)) > 0)
out.write(buff, 0, len);
bis.close();
out.close();
}我希望这能帮到别人。
悬崖
https://stackoverflow.com/questions/55319834
复制相似问题