当我写代码的时候,我必须上传一个文件到BunnyCDN,我修改了@dan-online/bunnycdnjs库,并制作了如下的上传代码。
async Upload(
path: string,
filename: string,
content: string | ReadStream
): Promise<APIResponse<any>> {
let url = `${this.endpoint}/${this.storageZoneName}/${path}/${filename}`;
return new Promise((resolve) => {
let options: request.CoreOptions = {
method: "PUT",
headers: {
AccessKey: this.apiKey,
'Content-Type': 'application/octet-stream',
},
body: content,
};
let handle: request.RequestCallback = (err, res, body) => {
let HttpResponse = res;
let _Error = err;
let IsLocalError = false;
let Data;
try {
let parsed = JSON.parse(body);
Data = parsed;
} catch (err) {
_Error = err;
IsLocalError = true;
}
resolve({
HttpResponse,
Error: _Error,
IsLocalError,
Data,
});
};
request(url, options, handle);
});
}
然后,我作了如下的联系。
const BunnyClient = new Bunny({
apiKey: BUNNY_STORAGE_KEY,
storageZoneName: BUNNY_STORAGE_ZONE,
cdnLocation: 'Main'
});
最后,我将Express设置为
import fileUpload from 'express-fileupload';
app.use(express.json({limit: '500mb'}));
app.use(express.urlencoded({limit: '500mb', extended: true}));
app.use(fileUpload({
limits: { fileSize: 500 * 1024 * 1024 },
}));
// ...
app.post('/api/_createEpisode', async (req, res) => {
if (!req.files || ['video'].some(key => !req.files[key])) return res.json(APIResponses.ERROR(
ErrorCodes.BadRequest,
{
'files': 'Err'
}
));
const {name, data} = req.files.video;
await BunnyClient.Upload('test', name, data.toString('base64'));
})
但是当它上传文件时,内容是无效的。我该如何解决这个问题。x1c 0d1x我上传的文件123.png的内容保存时
我已经找了很长时间,但我没有找到任何适合我的结果。我希望我可以在这里找到一个解决方案。
资源:BunnyCDN Docs - Upload File
1条答案
按热度按时间4ioopgfo1#
经过一点努力,我尝试通过键入
data
而不是data.toString('base64')
来发送缓冲区,并且成功地上载了缓冲区,没有任何问题