How to get response from S3 getObject in Node.js?

后端 未结 5 1436
离开以前
离开以前 2020-12-01 04:20

In a Node.js project I am attempting to get data back from S3.

When I use getSignedURL, everything works:

aws.getSignedUrl(\'getObject         


        
5条回答
  •  悲哀的现实
    2020-12-01 04:48

    At first glance it doesn't look like you are doing anything wrong but you don't show all your code. The following worked for me when I was first checking out S3 and Node:

    var AWS = require('aws-sdk');
    
    if (typeof process.env.API_KEY == 'undefined') {
        var config = require('./config.json');
        for (var key in config) {
            if (config.hasOwnProperty(key)) process.env[key] = config[key];
        }
    }
    
    var s3 = new AWS.S3({accessKeyId: process.env.AWS_ID, secretAccessKey:process.env.AWS_KEY});
    var objectPath = process.env.AWS_S3_FOLDER +'/test.xml';
    s3.putObject({
        Bucket: process.env.AWS_S3_BUCKET, 
        Key: objectPath,
        Body: "hello Fred",
        ACL:'public-read'
    }, function(err, data){
        if (err) console.log(err, err.stack); // an error occurred
        else {
            console.log(data);           // successful response
            s3.getObject({
                Bucket: process.env.AWS_S3_BUCKET, 
                Key: objectPath
            }, function(err, data){
                console.log(data.Body.toString());
            });
        }
    });
    

提交回复
热议问题