1.1.0 • Published 5 years ago

cobranzas-s3 v1.1.0

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

Cobranzas S3 Client

Wrapper para aws-sdk

aws-sdk documentation http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html

Install

npm i cobranzas-s3

HowTo

const S3 = new AWS.S3(/* options */)
const s3 = require('cobranzas-s3')(S3)
const bucket = s3.bucket('cobranzas')

Get a file

// s3.get(<Bucket>, <Key>)
s3.get('cobranzas', 'Test/file.txt')
// o siendo bucket = s3.bucket('cobranzas')
bucket.get('Test/file.txt')
    .then(buffer => buffer.toString())
    .then(file => {
        // file es el contenido del archivo
    })

Check if file exists

s3.exists('cobranzas', 'Test/file.txt')
// o usando un bucket
bucket.exists('Test/file.txt').then(bool => {
    // bool es true o false
})

List bucket files

s3.list('cobranzas', 'Pdfs/DAI').then(data => {
    data.keys // lista de keys encontradas
    /**
        [
            'Pdfs/DAI/DAI1234C34251004.pdf,
            'Pdfs/DAI/DAI1235C34251004.pdf
        ]
     */
    data.isTruncated // true: la lista continúa
    if (data.isTruncated) {
        return s3.list('cobranzas', 'Pdfs/DAI', data.nextParams).then(data => {/** ... */})
    }
})

Upload a file

/**
* @param key {string}
* @param body {string|ReadableStream|Buffer|TypedArray|Blob}
* @param options {undefined|object}
*/
bucket.upload('Test/file.txt', 'contenido del archivo.')

Create a stream from Amazon

// Usando un servidor Express.
// Evita descargar todo el archivo a servidor para luego mandarselo al cliente.
app.get('/file', (req, res, next) => {
    const fileStream = bucket.stream('Test/file.txt')
    fileStream.pipe(res)
})