简体   繁体   中英

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

I'm trying to upload an image to my AWS S3 bucket after downloading the image from another URL using Node (using request-promise-native & aws-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}`); });
} 

The upload succeeds when I test it out, however after trying to redownload it from my bucket the image is unable to display. Additionally, I notice after uploading the file with my function, the file listed in the bucket is much larger in filesize than the originally uploaded image. I'm trying to figure out where I've been going wrong but cannot find where. Any help is appreciated.

Try to open the faulty file with a text editor, you will see some errors written in it. You can try using s3.upload instead of putObject, it works better with streams.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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