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

后端 未结 5 1433
离开以前
离开以前 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:40

    Alternatively you could use minio-js client library get-object.js

    var Minio = require('minio')
    
    var s3Client = new Minio({
      endPoint: 's3.amazonaws.com',
      accessKey: 'YOUR-ACCESSKEYID',
      secretKey: 'YOUR-SECRETACCESSKEY'
    })
    
    var size = 0
    // Get a full object.
    s3Client.getObject('my-bucketname', 'my-objectname', function(e, dataStream) {
      if (e) {
        return console.log(e)
      }
      dataStream.on('data', function(chunk) {
        size += chunk.length
      })
      dataStream.on('end', function() {
        console.log("End. Total size = " + size)
      })
      dataStream.on('error', function(e) {
        console.log(e)
      })
    })
    

    Disclaimer: I work for Minio Its open source, S3 compatible object storage written in golang with client libraries available in Java, Python, Js, golang.

    0 讨论(0)
  • 2020-12-01 04:42

    When doing a getObject() from the S3 API, per the docs the contents of your file are located in the Body property, which you can see from your sample output. You should have code that looks something like the following

    const aws = require('aws-sdk');
    const s3 = new aws.S3(); // Pass in opts to S3 if necessary
    
    var getParams = {
        Bucket: 'abc', // your bucket name,
        Key: 'abc.txt' // path to the object you're looking for
    }
    
    s3.getObject(getParams, function(err, data) {
        // Handle any error and exit
        if (err)
            return err;
    
      // No error happened
      // Convert Body from a Buffer to a String
    
      let objectData = data.Body.toString('utf-8'); // Use the encoding necessary
    });
    

    You may not need to create a new buffer from the data.Body object but if you need you can use the sample above to achieve that.

    0 讨论(0)
  • 2020-12-01 04:45

    For someone looking for a NEST JS TYPESCRIPT version of the above:

        /**
         * to fetch a signed URL of a file
         * @param key key of the file to be fetched
         * @param bucket name of the bucket containing the file
         */
        public getFileUrl(key: string, bucket?: string): Promise<string> {
            var scopeBucket: string = bucket ? bucket : this.defaultBucket;
            var params: any = {
                Bucket: scopeBucket,
                Key: key,
                Expires: signatureTimeout  // const value: 30
            };
            return this.account.getSignedUrlPromise(getSignedUrlObject, params);
        }
    
        /**
         * to get the downloadable file buffer of the file
         * @param key key of the file to be fetched
         * @param bucket name of the bucket containing the file
         */
        public async getFileBuffer(key: string, bucket?: string): Promise<Buffer> {
            var scopeBucket: string = bucket ? bucket : this.defaultBucket;
            var params: GetObjectRequest = {
                Bucket: scopeBucket,
                Key: key
            };
            var fileObject: GetObjectOutput = await this.account.getObject(params).promise();
            return Buffer.from(fileObject.Body.toString());
        }
    
        /**
         * to upload a file stream onto AWS S3
         * @param stream file buffer to be uploaded
         * @param key key of the file to be uploaded
         * @param bucket name of the bucket 
         */
        public async saveFile(file: Buffer, key: string, bucket?: string): Promise<any> {
            var scopeBucket: string = bucket ? bucket : this.defaultBucket;
            var params: any = {
                Body: file,
                Bucket: scopeBucket,
                Key: key,
                ACL: 'private'
            };
            var uploaded: any = await this.account.upload(params).promise();
            if (uploaded && uploaded.Location && uploaded.Bucket === scopeBucket && uploaded.Key === key)
                return uploaded;
            else {
                throw new HttpException("Error occurred while uploading a file stream", HttpStatus.BAD_REQUEST);
            }
        }
    
    0 讨论(0)
  • 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: "<rss><data>hello Fred</data></rss>",
        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());
            });
        }
    });
    
    0 讨论(0)
  • 2020-12-01 04:54

    Based on the answer by @peteb, but using Promises and Async/Await:

    const AWS = require('aws-sdk');
    
    const s3 = new AWS.S3();
    
    async function getObject (bucket, objectKey) {
      try {
        const params = {
          Bucket: bucket,
          Key: objectKey 
        }
    
        const data = await s3.getObject(params).promise();
    
        return data.Body.toString('utf-8');
      } catch (e) {
        throw new Error(`Could not retrieve file from S3: ${e.message}`)
      }
    }
    
    // To retrieve you need to use `await getObject()` or `getObject().then()`
    getObject('my-bucket', 'path/to/the/object.txt').then(...);
    
    0 讨论(0)
提交回复
热议问题