如何将图像下载为流然后上传? [英] How to download an image as stream then upload it?

查看:61
本文介绍了如何将图像下载为流然后上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有很多图片,我想将这些图片迁移到新的主机上.然后我制作了一个脚本来使用 xray 刮它.获得 url 后,我想下载文件而不将其写入硬盘,然后直接将其上传到我的新主机.我正在使用 Strapi 和strapi-provider-upload-wasabi"上传我的图片.使用我的代码,我总是收到 400 Bad request

I have a lot of images on my website and want to migrate the images to a new hosting. then I made a script to scrape it using xray. once I got the url I want to download the file without writing it to hardisk and then directly upload it to my new hosting. I am using Strapi and "strapi-provider-upload-wasabi" to upload my image. with my code I always getting 400 Bad request

request.get(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');

        var form = {
            "refId": body.id,
            "ref": "comic",
            "field": "cover",
            "files": data,
            "path": "/"
        }

        request({
            uri: 'http://localhost:1337/upload',
            method: 'POST',
            headers: {
                'Content-Type': 'multipart/form-data'
            },
            formData: form
        }, function optionalCallback(err, httpResponse, body) {

            if (err) {
                return console.error('upload failed:', err);
            }
            // console.log(httpResponse);
            console.log('Upload successful!  Server responded with:', httpResponse.statusMessage);
        });
    }
});

推荐答案

第一步是检查错误信息.

First step would be to check error message.

确保模型 comic 有上传字段.

Make sure that model comic has upload field.

response.headers["content-type"]

response.headers["content-type"]

应该是:

response.headers["Content-Type"]

response.headers["Content-Type"]

Form Data 应该是 FormData 对象:

Form Data should be FormData object:

https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData

你有哪个版本的 Strapi?

Which version of Strapi do you have?

您是否查看过最新的文档?

Have you checked latest documentation?

https://strapi.io/documentation/3.0.0-beta.x/plugins/upload.html

另外,最好尝试使用一些最新的提供程序,例如

Also, it would be good to try with some up-to-date provider, e.g.

https://www.npmjs.com/package/strapi-provider-上传本地

这篇关于如何将图像下载为流然后上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆