Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

s3.getobject nodejs example

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()`
const myObject = await getObject('my-bucket', 'path/to/the/object.txt');
Comment

s3.getobject nodejs example

const { GetObjectCommand, S3Client } = require('@aws-sdk/client-s3')
const client = new S3Client() // Pass in opts to S3 if necessary

function getObject (Bucket, Key) {
  return new Promise(async (resolve, reject) => {
    const getObjectCommand = new GetObjectCommand({ Bucket, Key })

    try {
      const response = await client.send(getObjectCommand)
  
      // Store all of data chunks returned from the response data stream 
      // into an array then use Array#join() to use the returned contents as a String
      let responseDataChunks = []

      // Handle an error while streaming the response body
      response.Body.once('error', err => reject(err))
  
      // Attach a 'data' listener to add the chunks of data to our array
      // Each chunk is a Buffer instance
      response.Body.on('data', chunk => responseDataChunks.push(chunk))
  
      // Once the stream has no more data, join the chunks into a string and return the string
      response.Body.once('end', () => resolve(responseDataChunks.join('')))
    } catch (err) {
      // Handle the error or throw
      return reject(err)
    } 
  })
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: electron in webpack 
Javascript :: how to add eventlister to multiple variable 
Javascript :: updatedAt mongoose stop 
Javascript :: passport userlogin post method 
Javascript :: react-beforeunload react navive 
Javascript :: node js install aws-sdk 
Javascript :: react composition 
Javascript :: js alert with multiple buttons 
Javascript :: js variable to string 
Javascript :: what are devtools 
Javascript :: Each then() should return a value or throw 
Javascript :: react hook form 
Javascript :: set input type file value empty in react 
Javascript :: add int to string javascirpt 
Javascript :: vuejs get data fromo ajax 
Javascript :: bundle 
Javascript :: leaflet update marker icon 
Javascript :: jquery class 
Javascript :: javascript array find case insensitive 
Javascript :: how to craete an array in js 
Javascript :: react hook form validation controller 
Javascript :: ejs layout 
Javascript :: js arrow anonymous function 
Javascript :: javascript force view to focus on a div 
Javascript :: sort array of objects javascript by properties value 
Javascript :: olx clone react 
Javascript :: sequilze REACTJS 
Javascript :: check if the collection exists in mongodb database mongoose 
Javascript :: how to change data value in jquery 
Javascript :: console log on html 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =