10 examples of 's3 getobject nodejs' in JavaScript

Every line of 's3 getobject nodejs' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
43export function getObject (s3, params) {
44 return s3.getObject(params).promise();
45};
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

11public async getObject(key: string, bucket: string) {
12 const params: S3.Types.GetObjectRequest = {
13 Bucket: bucket,
14 Key: key,
15 };
16 return await this.s3
17 .getObject(params)
18 .promise()
19 .then((data: S3.Types.GetObjectOutput) => data)
20 .catch<null>(err => {
21 Utils.logError('S3 Get Object', err, params);
22 return null;
23 });
24}
275function retrieveObject(bucketName, keyName, callback) {
276 var s3 = new AWS.S3();
277 var objectBody = "";
278
279 var objectParams = {
280 Bucket: bucketName,
281 Key: keyName
282 };
283
284 async.waterfall([
285 function loadObject(next) {
286 console.log("Checking for object ", keyName, " in bucket ", bucketName);
287 s3.getObject(objectParams, next);
288 }, function handleObject(data, next) {
289 console.log("Got object");
290 objectBody = data.Body.toString('utf8').trim();
291 next(null);
292 }
293 ], function (err, result) {
294 if (err) {
295 console.log("Error looking for object");
296 console.log(err, err.stack);
297 callback(new Error("cannot load object"), null);
298 } else if (objectBody === '') {
299 callback(new Error("cannot load object"), null);
300 } else {
301 callback(null, objectBody);
302 }
303 });
304}
25function getObject(minioClient, bucketName, objectName) {
26 return new Promise((resolve, reject) =>
27 minioClient.getObject(bucketName, objectName, (err, dataStream) => {
28 if (err) {
29 reject(err);
30 return;
31 }
32
33 const chunks = [];
34 dataStream.on("data", chunk => chunks.push(chunk));
35 dataStream.on("error", reject);
36 dataStream.on("end", () => {
37 resolve(Buffer.concat(chunks).toString("utf8"));
38 });
39 })
40 );
41}
22putObject (region, bucketName, objectName, body, mimeType = undefined, cacheControl = undefined) {
23 const awsS3 = new AWS.S3({ region })
24 return new Promise((resolve, reject) => {
25 if (mimeType === undefined) {
26 mimeType = mime.lookup(objectName)
27 }
28 if (cacheControl === undefined) {
29 cacheControl = getCacheControl(mimeType)
30 }
31 const params = {
32 Bucket: bucketName,
33 Body: body,
34 Key: objectName,
35 ContentType: mimeType,
36 CacheControl: cacheControl
37 }
38 awsS3.putObject(params, (err, result) => {
39 if (err) {
40 return reject(err)
41 }
42 resolve()
43 })
44 })
45}
23function getS3(key) {
24 return new Promise((resolve, reject) => {
25 s3.getObject({ Bucket: process.env.BUCKET, Key: key }, (err, data) => {
26 if (err && err.code == 'NotFound') return reject(Errors.NOT_FOUND);
27 else if (err) {
28 const e = Object.assign({}, Errors.SOMETHING_WRONG, { err });
29 return reject(e);
30 }
31 const content_type = data.ContentType;
32 const image = new Buffer(data.Body).toString('base64');
33 return resolve({
34 statusCode: 200,
35 headers: { 'Content-Type': content_type },
36 body: image,
37 isBase64Encoded: true,
38 });
39 });
40 });
41}
149it('should get an object', function getObject(done) {
150 const params = { Bucket: bucket, Key: 'key' };
151 const url = s3.getSignedUrl('getObject', params);
152 provideRawOutput(['-verbose', '-o', 'download', url], (httpCode) => {
153 assert.strictEqual(httpCode, '200 OK');
154 done();
155 });
156});
45async getObject (key) {
46 // see https://github.com/NodeRedis/node_redis#promises
47 const result = await promisify(this.client.get).bind(this.client)(key);
48 if (result) {
49 return JSON.parse(result);
50 }
51 return result;
52}
349function headObjects(contents, bucket, callback) {
350 var keys = [];
351
352 // Get each object in parallel
353 async.each(contents, function(object, cb) {
354 s3.headObject({
355 Bucket: bucket,
356 Key: object.Key
357 }, function(err, data) {
358 if (err) {
359 cb(err);
360 } else {
361 // Add the key attribute
362 data.Key = object.Key;
363 keys.push(data);
364 cb(null);
365 }
366 });
367 },
368 function(err) {
369 if (err) {
370 callback(err);
371 } else {
372 callback(null, keys);
373 }
374 });
375}
39async function deleteS3Object(bucket, key) {
40 await s3
41 .deleteObject({
42 Bucket: bucket,
43 Key: key
44 })
45 .promise()
46}

Related snippets