[英]How to load a PNG from MongoDB Atlas into a Unity Image via AWS Lambda?
I have stored my PNG images in MongoDB Atlas as Buffer.我已将我的 PNG 图像作为缓冲区存储在 MongoDB Atlas 中。
Before I migrated my server to AWS Lambda, I used to retrieve them at the back end correctly:在我将我的服务器迁移到 AWS Lambda 之前,我曾经在后端正确检索它们:
exports.tree = function(req, res, next) {
Tree.findOne({ level: parseInt(req.params.level) }, "data", function(err, f) {
if (err || !f) { res.send(""); return; }
res.contentType("image/png");
res.send(f.data);
});
};
Now I wish to move my back end to AWS Lambda to cut down my operational costs.现在我希望将我的后端移动到 AWS Lambda 以降低我的运营成本。
Here is my current Lambda function:这是我当前的 Lambda function:
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const {Tree, config} = require('/opt/nodejs/models');
const mongoose = require('mongoose');
mongoose.set('strictQuery', true);
await mongoose.connect(config.DB);
function res(body, type="text/plain"){
const res = {
statusCode: 200,
isBase64Encoded: false,
headers: {
"Access-Control-Allow-Origin":"*",
"Content-Type": type
},
multiValueHeader: {},
body
};
return res;
}
export const handler = async(event) => {
let f = await Tree.findOne({ level: parseInt(event.queryStringParameters.level,10) }, "data");
if (!f) { return res(""); }
return res(f.data,"image/png");
};
However, I couldn't load them into my Unity project:但是,我无法将它们加载到我的 Unity 项目中:
...
UnityWebRequest www;
if (l > 30) ll = 30; else ll = l;
if (!T[ll - 1]){
do {
www = UnityWebRequestTexture.GetTexture(Apis.tree+"?level="+ll);
yield return www.SendWebRequest();
print(www.downloadedBytes + " Current: " + www.error);
} while (www.error != null || www.downloadedBytes == 0);
T[ll - 1] = DownloadHandlerTexture.GetContent(www);
}
Tr.GetComponent<Image>().sprite = Sprite.Create(T[ll - 1], new Rect(0f, 0f, T[ll - 1].width, T[ll - 1].height), new Vector2(0.5f, 0.5f), 100f);
It kept saying: "Internal Server Error".它一直说:“内部服务器错误”。
What should I do?我应该怎么办?
A few changes were needed:需要进行一些更改:
I had to use the REST API and not just the HTTP API, and allow the image/png binary format.我必须使用 REST API 而不仅仅是 HTTP API,并允许图像/png 二进制格式。
I had to remove multiValueHeader: {} from the response.我必须从响应中删除 multiValueHeader: {}。
I had to convert the buffer to a base64 string before returning it in the response body.在响应正文中返回之前,我必须将缓冲区转换为 base64 字符串。
For the Unity part, here is my code:对于 Unity 部分,这是我的代码:
do{ www = UnityWebRequest.Get(Apis.tree+"?level="+ll); yield return www.SendWebRequest(); print(www.downloadedBytes + " Current: " + www.error); } while (www.error.= null || www;downloadedBytes == 0). byte[] imageBytes = Convert.FromBase64String(www.downloadHandler;text), T[ll - 1] = new Texture2D(2048, 2048. TextureFormat,ARGB32; false). T[ll - 1];LoadImage(imageBytes). Tr.GetComponent<Image>().sprite = Sprite,Create(T[ll - 1]. new Rect(0,0f. 0,0f. T[ll - 1],width. T[ll - 1],height). new Vector2(0,5f. 0,5f). 100;0f);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.