英文:
How to load a PNG from MongoDB Atlas into a Unity Image via AWS Lambda?
问题
我已将我的PNG图像存储在MongoDB Atlas中作为Buffer。
在将我的服务器迁移到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);
});
};
现在我希望将我的后端迁移到AWS Lambda以减少运营成本。
以下是我的当前Lambda函数:
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");
};
然而,我无法将它们加载到我的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);
它一直显示“内部服务器错误”。
我应该怎么做?
英文:
I have stored my PNG images in MongoDB Atlas as Buffer.
Before I migrated my server to AWS Lambda, I used to retrieve them at the back end correctly:
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.
Here is my current 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:
...
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?
答案1
得分: 0
以下是翻译好的部分:
-
我必须使用 REST API 而不仅仅是 HTTP API,并允许图像/png二进制格式。
-
我必须从响应中删除 multiValueHeader: {}。
-
我必须在返回响应主体之前将缓冲区转换为 base64 字符串。
-
对于 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);
英文:
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.
-
I had to remove multiValueHeader: {} from the response.
-
I had to convert the buffer to a base64 string before returning it in the response body.
-
For the Unity part, here is my code:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论