将 axios 请求通过管道传输到 pkgcloud 存储上传会导致 "write after end" 错误
Piping an axios request to a pkgcloud storage upload causes a "write after end" error
我正在尝试使用 axios 下载外部文件,然后使用 pkgcloud 将其通过管道传输到 openstack 对象存储容器中。上传流似乎在文件完全下载之前结束。
async function upload(url, name, extension, container) {
const uploadStream = storage.createClient({
...
}).upload({
container: container,
remote: name + "." + extension,
});
await new Promise(async (resolve) => {
const response = await axios({
method: "GET",
url: url,
responseType: "stream"
})
response.data.pipe(uploadStream)
response.data.on("end", () => {
console.log("Download finished");
})
response.data.on("error", (error: Error) => {
console.log("Download error")
console.log(error);
})
uploadStream.on("finish", () => {
console.log("Upload finished");
resolve();
})
uploadStream.on("error", (error: Error) => {
console.log("Upload error");
console.log(error);
})
})
}
const url = "https://picsum.photos/id/566/600/600";
(async () => {
await upload(url, "mountain", "jpg", "dummy_container")
console.log("Promise resolved");
})()
根据控制台输出的事件顺序如下:
Download finished
Upload finished
Promise resolved
Upload error
Error: write after end
...
可以通过使用 concat-stream
包将下载流式传输到缓冲区,然后将缓冲区流式传输到上传来解决此问题:
function upload(url, name, extension, container) {
const uploadStream = storage.createClient({
...
}).upload({
container,
remote: name + "." + extension,
});
axios({
method: "GET",
url,
responseType: "stream",
}).then((response) => {
const concatStream = concat((buffer) => {
const bufferStream = new Stream.PassThrough();
bufferStream.end(buffer);
bufferStream.pipe(uploadStream);
})
response.data.pipe(concatStream);
})
}
const url = "https://picsum.photos/id/566/600/600";
upload(url, "mountain", "jpg", "dummy_container")
问题来自 pkgcloud 在如何为 OpenStack 存储流式传输文件时的错误。在
中有解释
我正在尝试使用 axios 下载外部文件,然后使用 pkgcloud 将其通过管道传输到 openstack 对象存储容器中。上传流似乎在文件完全下载之前结束。
async function upload(url, name, extension, container) {
const uploadStream = storage.createClient({
...
}).upload({
container: container,
remote: name + "." + extension,
});
await new Promise(async (resolve) => {
const response = await axios({
method: "GET",
url: url,
responseType: "stream"
})
response.data.pipe(uploadStream)
response.data.on("end", () => {
console.log("Download finished");
})
response.data.on("error", (error: Error) => {
console.log("Download error")
console.log(error);
})
uploadStream.on("finish", () => {
console.log("Upload finished");
resolve();
})
uploadStream.on("error", (error: Error) => {
console.log("Upload error");
console.log(error);
})
})
}
const url = "https://picsum.photos/id/566/600/600";
(async () => {
await upload(url, "mountain", "jpg", "dummy_container")
console.log("Promise resolved");
})()
根据控制台输出的事件顺序如下:
Download finished
Upload finished
Promise resolved
Upload error
Error: write after end
...
可以通过使用 concat-stream
包将下载流式传输到缓冲区,然后将缓冲区流式传输到上传来解决此问题:
function upload(url, name, extension, container) {
const uploadStream = storage.createClient({
...
}).upload({
container,
remote: name + "." + extension,
});
axios({
method: "GET",
url,
responseType: "stream",
}).then((response) => {
const concatStream = concat((buffer) => {
const bufferStream = new Stream.PassThrough();
bufferStream.end(buffer);
bufferStream.pipe(uploadStream);
})
response.data.pipe(concatStream);
})
}
const url = "https://picsum.photos/id/566/600/600";
upload(url, "mountain", "jpg", "dummy_container")
问题来自 pkgcloud 在如何为 OpenStack 存储流式传输文件时的错误。在