下载然后上传图像而不存储它

Posted

技术标签:

【中文标题】下载然后上传图像而不存储它【英文标题】:Download then upload image without storing it 【发布时间】:2021-10-20 17:28:52 【问题描述】:

我目前正在尝试在 Zapier 中构建图像上传组件,但在使其正常工作时遇到问题。我必须能够下载图像,然后将其发布到新端点而不将其存储在本地。目前,我能做的最接近的方法是将 IncomingMessage 发送到 POST,但我知道这是不对的。

有人有什么建议吗?

let FormData = require('form-data');
let http = require('https');

const makeDownloadStream = (url) => 
  new Promise((resolve, reject) => 
    http.request(url, resolve).on('error', reject).end();
  );


const makeUploadStream = (z, bundle, options) => 
    var imageRequest = options;
    const promise = z.request(imageRequest);
    return promise.then((response) => 
        return response.data;
    );


const addAttachment = async (z, bundle) => 
    /*var request = 
        'url': bundle.inputData.attachment
    ;
    const promiseAt = z.request(request);
    return promiseAt.then((stream) => */
        const form = new FormData();
        var data = `"type": "records", "attributes": "form_id": $bundle.inputData.form_id`
        const stream = await makeDownloadStream(bundle.inputData.attachment);
        form.append(`field_$bundle.inputData.field_id`, stream);
        form.append('data', data);
        var request = 
            'url': bundle.inputData.url,
            'method': 'PUT',
            'headers': 
                'Content-Type': `multipart/form-data; boundary=$form.getBoundary()`
            ,
            'body': form
        ;
        const response = await makeUploadStream(z, bundle, request);
        return response;
    //);

【问题讨论】:

【参考方案1】:

我自己想通了。对于任何需要在 Zapier 上上传图片的人,这里是:

let FormData = require('form-data');

const makeDownloadStream = (z, bundle) => 
    var imageRequest = 
        'url': bundle.inputData.attachment,
        'method': 'GET',
        'raw': true
    ;
    const promise = z.request(imageRequest);
    return promise.then(async (response) => 
        var buffer = await response.buffer();
        return 
            'content-type': response.headers.get('content-type'),
            'content': buffer,
            'filename': response.headers.get('content-disposition').replace('attachment; filename="', '').replace('"', '')
        
    );


const addAttachment = async (z, bundle) => 
    const form = new FormData();
    const content = await makeDownloadStream(z, bundle);
    form.append(`field_$bundle.inputData.field_id`, Buffer.from(content.content.toString('binary'), 'binary'), 
        filename: content.filename
    );
    const request = 
        'url': `$bundle.inputData.url/api/records/$bundle.inputData.record_id`,
        'method': 'PUT',
        'headers': 
            'Content-Type': `multipart/form-data; boundary=$form.getBoundary()`,
            'Content-Length': form.getLengthSync()
        ,
        'body': form
    ;
    const promise = z.request(request);
    return promise.then((response) => 
        return response.data;
    );

【讨论】:

以上是关于下载然后上传图像而不存储它的主要内容,如果未能解决你的问题,请参考以下文章

IONIC - 如何显示来自firebase存储的图像而无需下载它[关闭]

上传图像然后共享而不使用 sharedObject 或任何东西将文件保存在 Flex 上?

如何从firebase下载图像?云火库?福布斯存储?

成功将图像上传到 Firebase 存储后,Firebase 获取下载 URL

如何将多个图像上传到 Firebase 存储并返回多个下载 URL

使用 filerImageField 上传图片