繁体   English   中英

GET请求映像后上传AWS S3,但上传不正确

[英]AWS S3 Upload after GET Request to Image, Not Uploading Correctly

在使用Node从另一个URL下载图像后,我试图将图像上传到我的AWS S3存储桶(使用request-promise-nativeaws-sdk ):

'use strict';

const config = require('../../../configs');
const AWS = require('aws-sdk');
const request = require('request-promise-native');

AWS.config.update(config.aws);
let s3 = new AWS.S3();

function uploadFile(req, res) {

    function getContentTypeByFile(fileName) {
        var rc = 'application/octet-stream';
        var fn = fileName.toLowerCase();

        if (fn.indexOf('.png') >= 0) rc = 'image/png';
        else if (fn.indexOf('.jpg') >= 0) rc = 'image/jpg';

        return rc;
    }

    let body = req.body,
        params = {
            "ACL": "bucket-owner-full-control",
            "Bucket": 'testing-bucket',
            "Content-Type": null,
            "Key": null, // Name of the file
            "Body": null // File body
        };

    // Grabs the filename from a URL
    params.Key = body.url.substring(body.url.lastIndexOf('/') + 1);

    // Setting the content type
    params.ContentType = getContentTypeByFile(params.Key);

    request.get(body.url)
    .then(response => {
        params.Body = response;
        s3.putObject(params, (err, data) => {
            if (err) { console.log(`Error uploading to S3 - ${err}`); }
            if (data) { console.log("Success - Uploaded to S3: " + data.toString()); }
        });
    })
    .catch(err => { console.log(`Error encountered: ${err}`); });
} 

当我对其进行测试时,上传成功,但是尝试从存储桶中重新下载后,该图像将无法显示。 此外,我注意到使用函数上传文件后,存储区中列出的文件的文件大小比原始上传的图像大得多。 我试图弄清楚哪里出了问题,但找不到地方。 任何帮助表示赞赏。

尝试使用文本编辑器打开错误的文件,您将看到其中写入的一些错误。 您可以尝试使用s3.upload而不是putObject,它与流效果更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM